VBA 遍历 Sharepoint 文档文件夹以刷新没有 TEMP/cache 文件错误的查询
VBA loop through Sharepoint documents folder to refresh queries without TEMP/cache file error
目标:
创建 VBA 例程以刷新 SharePoint Online 站点文档文件夹中所有工作簿中的查询。
文件夹设置:
SharePoint 站点 -> 文档 -> 主文件夹 -> 10 个子文件夹 -> 每个 Excel 个文件
当前代码:
Sub RefreshFW()
Dim fso, oFolder, oSubfolder, oFile, queue As Collection
Dim lCnt As Long
Set fso = CreateObject("Scripting.FileSystemObject")
Set queue = New Collection
queue.Add fso.GetFolder("C:\Users\user1\organization\main_folder") 'from Sharepoint sync
Do While queue.Count > 0
Set oFolder = queue(1)
queue.Remove 1 'dequeue
For Each oSubfolder In oFolder.SubFolders
queue.Add oSubfolder 'enqueue
Next oSubfolder
For Each oFile In oFolder.Files
Workbooks.Open fileName:=oFile
ActiveWorkbook.Unprotect Password:="wb"
With ActiveWorkbook
For lCnt = 1 To .Connections.Count
If .Connections(lCnt).Type = xlConnectionTypeOLEDB Then
.Connections(lCnt).OLEDBConnection.BackgroundQuery = False
End If
Next lCnt
End With
ActiveWorkbook.RefreshAll
ActiveWorkbook.Protect Password:="wb"
ActiveWorkbook.Close SaveChanges:=True
Next oFile
Loop
End Sub
问题:
运行时 1004 错误
“Microsoft 无法访问文件'C:\Users\user1\organization\folder\.123A1234-D756*******'文件名或路径不存在或文件正被另一个程序使用" 在 Workbooks.Open fileName:=oFile
期间
我尝试过的事情:
指向本地文件夹而不是同步的“本地”SharePoint 文件夹 - 这有效。
使用 DavWWWRoot 将 SharePoint 路径更改为 UNC 格式 - 无效* 可能搞砸了,但找不到路径
将主文件夹网址添加到映射的网络驱动器,而不是使用 SharePoint 在线上的“同步”按钮,然后改为指向网络驱动器 - 缺乏权限(不是受信任的站点,需要联系内部资源获得批准)
问题:
这个 .123A1234-D756**** 文件是主文件夹中的 cache/temp 文件吗?
如何调整我的代码以在遍历文件夹时忽略此 cache/temp 文件?
还有其他推荐吗?
谢谢
Sub RefreshFW()
Application.ScreenUpdating = False
Dim fso, oFolder, oSubfolder, oFile, queue As Collection
Dim lCnt As Long
Set fso = CreateObject("Scripting.FileSystemObject")
Set queue = New Collection
queue.Add fso.GetFolder("C:\Users\user1\organization\main_folder")
Do While queue.Count > 0
Set oFolder = queue(1)
queue.Remove 1 'dequeue
For Each oSubfolder In oFolder.SubFolders
queue.Add oSubfolder 'enqueue
Next oSubfolder
For Each oFile In oFolder.Files
If oFile.Name Like "*xls*" Then
Workbooks.Open fileName:=oFile
ActiveWorkbook.Unprotect Password:="wb"
With ActiveWorkbook
For lCnt = 1 To .Connections.Count
If .Connections(lCnt).Type = xlConnectionTypeOLEDB Then
.Connections(lCnt).OLEDBConnection.BackgroundQuery = False
End If
Next lCnt
End With
ActiveWorkbook.RefreshAll
ActiveWorkbook.Protect Password:="wb"
ActiveWorkbook.Close SaveChanges:=True
End If
Next oFile
Loop
Application.ScreenUpdating = True
End Sub
添加了 If oFile.Name Like "*xls*" Then
和 End If
以仅检查 excel 扩展。
目标:
创建 VBA 例程以刷新 SharePoint Online 站点文档文件夹中所有工作簿中的查询。
文件夹设置:
SharePoint 站点 -> 文档 -> 主文件夹 -> 10 个子文件夹 -> 每个 Excel 个文件
当前代码:
Sub RefreshFW()
Dim fso, oFolder, oSubfolder, oFile, queue As Collection
Dim lCnt As Long
Set fso = CreateObject("Scripting.FileSystemObject")
Set queue = New Collection
queue.Add fso.GetFolder("C:\Users\user1\organization\main_folder") 'from Sharepoint sync
Do While queue.Count > 0
Set oFolder = queue(1)
queue.Remove 1 'dequeue
For Each oSubfolder In oFolder.SubFolders
queue.Add oSubfolder 'enqueue
Next oSubfolder
For Each oFile In oFolder.Files
Workbooks.Open fileName:=oFile
ActiveWorkbook.Unprotect Password:="wb"
With ActiveWorkbook
For lCnt = 1 To .Connections.Count
If .Connections(lCnt).Type = xlConnectionTypeOLEDB Then
.Connections(lCnt).OLEDBConnection.BackgroundQuery = False
End If
Next lCnt
End With
ActiveWorkbook.RefreshAll
ActiveWorkbook.Protect Password:="wb"
ActiveWorkbook.Close SaveChanges:=True
Next oFile
Loop
End Sub
问题:
运行时 1004 错误
“Microsoft 无法访问文件'C:\Users\user1\organization\folder\.123A1234-D756*******'文件名或路径不存在或文件正被另一个程序使用" 在 Workbooks.Open fileName:=oFile
我尝试过的事情:
指向本地文件夹而不是同步的“本地”SharePoint 文件夹 - 这有效。
使用 DavWWWRoot 将 SharePoint 路径更改为 UNC 格式 - 无效* 可能搞砸了,但找不到路径
将主文件夹网址添加到映射的网络驱动器,而不是使用 SharePoint 在线上的“同步”按钮,然后改为指向网络驱动器 - 缺乏权限(不是受信任的站点,需要联系内部资源获得批准)
问题:
这个 .123A1234-D756**** 文件是主文件夹中的 cache/temp 文件吗?
如何调整我的代码以在遍历文件夹时忽略此 cache/temp 文件?
还有其他推荐吗?
谢谢
Sub RefreshFW()
Application.ScreenUpdating = False
Dim fso, oFolder, oSubfolder, oFile, queue As Collection
Dim lCnt As Long
Set fso = CreateObject("Scripting.FileSystemObject")
Set queue = New Collection
queue.Add fso.GetFolder("C:\Users\user1\organization\main_folder")
Do While queue.Count > 0
Set oFolder = queue(1)
queue.Remove 1 'dequeue
For Each oSubfolder In oFolder.SubFolders
queue.Add oSubfolder 'enqueue
Next oSubfolder
For Each oFile In oFolder.Files
If oFile.Name Like "*xls*" Then
Workbooks.Open fileName:=oFile
ActiveWorkbook.Unprotect Password:="wb"
With ActiveWorkbook
For lCnt = 1 To .Connections.Count
If .Connections(lCnt).Type = xlConnectionTypeOLEDB Then
.Connections(lCnt).OLEDBConnection.BackgroundQuery = False
End If
Next lCnt
End With
ActiveWorkbook.RefreshAll
ActiveWorkbook.Protect Password:="wb"
ActiveWorkbook.Close SaveChanges:=True
End If
Next oFile
Loop
Application.ScreenUpdating = True
End Sub
添加了 If oFile.Name Like "*xls*" Then
和 End If
以仅检查 excel 扩展。