Excel VBA Up- / Download desde múltiples carpetas de SharePoint
Encontré código de muestra en Internet para descargar archivos de una carpeta de SharePoint usando VBA (abrir en el Explorador, asignar a la letra de unidad, etc.)
Por lo tanto, escribí el siguiente código:
Dim sharepointFolder As String
Dim colDisks As Variant
Dim objWMIService As Object
Dim objDisk As Variant
Dim driveLetter As String
'Create FSO and network object
Set objNet = CreateObject("WScript.Network")
Set fs = CreateObject("Scripting.FileSystemObject")
'Get all used Drive-Letters
Set objWMIService = GetObject("winmgmts:\\" & "." & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery("Select * from Win32_LogicalDisk")
'Loop through used Drive-Letters
For Each objDisk In colDisks
For i = 65 To 90
'If letter is in use exit loop and remember letter.
If i = Asc(objDisk.DeviceID) Then
j = i
Exit For
'letters which are not checked yet are possible only
ElseIf i > j Then
driveLetter = Chr(i) & ":"
Exit For
End If
Next i
'If a Drive-Letter is found exit the loop
If driveLetter <> "" Then
Exit For
End If
Next
'define path to SharePoint
sharepointFolder = "https://spFolder/Sector Reports/"
'Map the sharePoint folder to the free Drive-Letter
objNet.MapNetworkDrive driveLetter, sharepointFolder
'set the folder to the mapped SharePoint-Path
Set folder = fs.GetFolder(driveLetter)
En esta etapa, puedo subir archivos a la carpeta:
https://spFolder/Sector Reports/
Sin embargo, también quiero subir archivos a la carpeta, por ejemplo:
https://spFolder/Documents/
y también eliminé la letra de unidad anterior usando la función:
removeDriveLetter "Letter"
Ahora tengo el problema, que si asigno una nueva carpeta a una letra:
mapDriveLetter "A:\", sharepointFolder
y desea guardar algo en esta carta, siempre se guardará en la ruta anterior. Por ejemplo:
mapDriveLetter "A:\", sharePointFolder1
removeDriveLetter "A:\"
mapDriveLetter "A:\", sharePointFolder2
workbook.saveas "A:\" & workbookName
En este caso, el libro siempre se guarda en la ruta dada en "sharePointFolder1" y no en "sharePointFolder2".