如何使用VBA在Sharepoint站点中打开文件

我正在尝试打开文件名称每周更改的文件。 这意味着文件名称上的date部分是不同的。 另外,这个文件是文件夹内的唯一文件。 但其文件名称正在改变。 我正在使用下面的代码,但它是抛出错误,运行时间52:错误的文件名称和数字。 我需要你的帮助。

Dim ThePath As String Dim TheFile As String ThePath = "https://ts.company.com/sites/folder1/folder2/folder3/folder4/" TheFile = Dir(ThePath & "MANILA_ShiftRecord_*" & ".xlsx") Workbooks.Open (ThePath & TheFile) 

谢谢!

如果只有一个文件,你可以使用这种方法:

 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) 

之后,您可以使用filesystemobject函数处理文件夹。