Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 在SharePoint文档库上的文件夹中打开最近的工作簿_Excel_Vba_Sharepoint - Fatal编程技术网

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

Excel 在SharePoint文档库上的文件夹中打开最近的工作簿,excel,vba,sharepoint,Excel,Vba,Sharepoint,我想在SharePoint文档库中打开最新的工作簿。该文件保存为我需要访问的归档文件,以便从中提取信息 我尝试了一点VBA脚本,它允许我通过CAF访问该文件,但由于我的所有文件夹都已联机移动到365,因此我需要一种新方法来访问文件夹中最新的存档工作簿 'Force the explicit delcaration of variables Option Explicit Sub OpenLatestFile() 'Declare the variables Dim MyP

我想在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
“强制变量的显式增量
选项显式
子OpenLatestFile()
'声明变量
将MyPath设置为字符串
将MyFile设置为字符串
将最新文件设置为字符串
将LatestDate变为Date
Dim LMD作为日期
'指定文件夹的路径
MyPath=“sharepoint上的文件位置”
'确保路径以反斜杠结束
如果正确(MyPath,1)“\”则MyPath=MyPath&“\”
'从文件夹中获取第一个Excel文件
MyFile=Dir(MyPath&“*.xlsm”)
'如果未找到任何文件,请退出子目录
如果Len(MyFile)=0,则
MsgBox“未找到任何文件…”,请使用感叹号
出口接头
如果结束
'循环浏览文件夹中的每个Excel文件
当Len(MyFile)>0时执行
'将当前文件的日期/时间分配给变量
LMD=FileDateTime(MyPath&MyFile)
'如果当前文件的日期/时间大于最新日期/时间
'记录日期,将其文件名和日期/时间分配给变量
如果LMD>最晚日期,则
LatestFile=MyFile
LatestDate=LMD
如果结束
'从文件夹中获取下一个Excel文件
MyFile=Dir
环
'打开最新的文件
工作簿。打开MyPath和LatestFile

您仍然可以使用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)

对我有用。

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

谢谢你的回答。我修改了代码,但仍然出现错误f=Dir(PATH&“*.xlsm”)运行时错误52错误的文件名或编号。Dir是否与http:\\有关?您不需要http