我在互联网上找到了使用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在这种情况下,工作簿始终保存到"sharePointFolder1“中指定的路径,而不是"sharePointFolder2”中的路径。
发布于 2015-07-29 13:43:08
我解决这个问题的方法如下:
在将包含所请求文件的每个文件夹连接到网络驱动器号的情况下,我将所有文件夹放入相同的库中,并仅将库的根文件夹连接到网络驱动器号。
在分别使用connection https://spFolder/Sector Reports/和https://spFolder/Documents/的情况下,我将只连接https://spFolder/,并使用文件系统对象浏览子目录。
https://stackoverflow.com/questions/30927462
复制相似问题