Excel VBA Up- /从多个SharePoint文件夹下载

时间:2015-06-18 23:22:09

标签: excel vba excel-vba sharepoint

我在Internet上找到了使用VBA从SharePoint文件夹下载文件的示例代码(在资源管理器中打开,映射到驱动器号等)

因此,我写了以下代码:

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)

在此阶段,我可以将文件上传到文件夹:

https://spFolder/Sector Reports/

但是,我还想将文件上传到文件夹,例如:

https://spFolder/Documents/

我还使用以下功能删除了之前的驱动器号:

removeDriveLetter "Letter" 

现在我遇到了问题,如果我将一个新文件夹映射到一个字母:

mapDriveLetter "A:\", sharepointFolder

并希望在此信件上保存一些内容,它将始终保存在上一个路径中。例如:

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

在这种情况下,工作簿始终保存在&#34; sharePointFolder1&#34;中给出的路径中。而不是在&#34; sharePointFolder2&#34;。

1 个答案:

答案 0 :(得分:1)

我按照以下方式解决了这个问题:

如果将包含所请求文件的每个文件夹连接到网络驱动器号,我将所有文件夹放在同一个库中,并将库的根文件夹连接到网络驱动器号。

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