繁体   English   中英

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

[英]How to open a file in a Sharepoint site using VBA

我正在尝试打开一个文件名每周更改的文件。 这意味着文件名上的日期部分是不同的。 此外,此文件是文件夹内的唯一文件。 但它的文件名正在改变。 我正在使用下面的代码,但它抛出了错误,“运行时间 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)

之后,您可以使用文件系统对象函数处理该文件夹。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM