Excel VBA上传/从多个SharePoint文件夹下载

我在Internet上find示例代码,使用VBA从SharePoint文件夹下载文件(在资源pipe理器中打开,映射到驱动器盘符等)

所以我写了下面的代码:

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) 

在这个阶段,我可以将file upload到文件夹中:

 https://spFolder/Sector Reports/ 

不过,我也想上传文件到文件夹,例如:

 https://spFolder/Documents/ 

我也使用该函数删除了以前的驱动器号:

 removeDriveLetter "Letter" 

现在我有这个问题,如果我映射一个新的文件夹到一个字母:

 mapDriveLetter "A:\", sharepointFolder 

并且希望在这个字母上保存一些东西,它总是保存在前一个path上。 例如:

 mapDriveLetter "A:\", sharePointFolder1 removeDriveLetter "A:\" mapDriveLetter "A:\", sharePointFolder2 workbook.saveas "A:\" & workbookName 

在这种情况下,工作簿始终保存为“sharePointFolder1”中给出的path,而不是“sharePointFolder2”中的path。

我解决这个问题如下:

在将每个包含请求文件的文件夹连接到networking驱动器盘符的情况下,我将所有文件夹放入同一个库中,并将库的根文件夹连接到networking驱动器盘符。

如果连接https://spFolder/Sector Reports/分别https://spFolder/Documents/我将连接https://spFolder/ only并使用文件系统对象浏览子目录。