在SharePoint文档库上的文件夹中打开最新的工作簿

时间:2019-11-07 16:15:43

标签: excel vba sharepoint

我想在SharePoint文档库中打开最新的工作簿。该文件另存为档案文件,我需要从档案文件中提取信息。

我尝试了一些VBA脚本,该脚本允许我通过CAF访问文件,但是由于我的所有文件夹都已在线移动到365,因此我需要一种新的方法来访问该文件夹中的最新存档工作簿。

'Force the explicit delcaration of variables
Option Explicit

Sub OpenLatestFile()

     'Declare the variables
     Dim MyPath  As String
     Dim MyFile  As String
     Dim LatestFile  As String
     Dim LatestDate  As Date
     Dim LMD  As Date

     'Specify the path to the folder
     MyPath = "file location on sharepoint"

     'Make sure that the path ends in a backslash
     If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"

     'Get the first Excel file from the folder
     MyFile = Dir(MyPath & "*.xlsm")

     'If no files were found, exit the sub
     If Len(MyFile) = 0 Then
         MsgBox "No files were found...", vbExclamation
         Exit Sub
     End If

     'Loop through each Excel file in the folder
     Do While Len(MyFile) > 0

         'Assign the date/time of the current file to a variable
         LMD = FileDateTime(MyPath & MyFile)

         'If the date/time of the current file is greater than the latest
         'recorded date, assign its filename and date/time to variables
         If LMD > LatestDate Then
             LatestFile = MyFile
             LatestDate = LMD
         End If

         'Get the next Excel file from the folder
         MyFile = Dir

     Loop

     'Open the latest file
     Workbooks.Open MyPath & LatestFile

2 个答案:

答案 0 :(得分:0)

您仍然应该可以使用Dir:

Const PATH As String = "\\companyHere.sharepoint.com\Departments\Blah\Stuff\"

Dim f

f = Dir(PATH & "*.xlsx")

Debug.Print f
Debug.Print FileDateTime(PATH & f)

为我工作。

答案 1 :(得分:0)

解决了这个问题。我必须使用URL路径。