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

时间:2015-07-16 23:38:22

标签: excel vba excel-vba sharepoint sharepoint-2010

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

谢谢!

1 个答案:

答案 0 :(得分:0)

如果只有一个文件,您可以使用此方法:

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函数处理该文件夹。