我在SharePoint 365中有一个文档库,我使用本地Excel宏文件通过VBA在线打开SharePoint中的工作簿。这适用于像
这样的代码location = "https://mycompany.sharepoint.com/sites/ABC/LibraryA/Book1.xlsx"
Set wbk = Workbooks.Open(location)
但是,当用户在Excel Online中编辑工作簿时,工作簿将被锁定/正在使用,然后代码将运行到提示以保存文件的本地副本。我想避免这种情况,并分支到处理不同的代码,如果工作簿正在使用中。
如果工作簿存储在我公司的文件服务器上,那么事情会很简单。我可以使用Microsoft支持网站提供的Macro code to check whether a file is already open:
Sub TestFileOpened()
' Test to see if the file is open.
If IsFileOpen("c:\Book2.xls") Then
' Display a message stating the file in use.
MsgBox "File already in use!"
'
' Add code here to handle case where file is open by another
' user.
'
Else
' Display a message stating the file is not in use.
MsgBox "File not in use!"
' Open the file in Microsoft Excel.
Workbooks.Open "c:\Book2.xls"
'
' Add code here to handle case where file is NOT open by another
' user.
'
End If
End Sub
' This function checks to see if a file is open or not. If the file is
' already open, it returns True. If the file is not open, it returns
' False. Otherwise, a run-time error occurs because there is
' some other problem accessing the file.
Function IsFileOpen(filename As String)
Dim filenum As Integer, errnum As Integer
On Error Resume Next ' Turn error checking off.
filenum = FreeFile() ' Get a free file number.
' Attempt to open the file and lock it.
Open filename For Input Lock Read As #filenum
Close filenum ' Close the file.
errnum = Err ' Save the error number that occurred.
On Error GoTo 0 ' Turn error checking back on.
' Check to see which error occurred.
Select Case errnum
' No error occurred.
' File is NOT already open by another user.
Case 0
IsFileOpen = False
' Error number for "Permission Denied."
' File is already opened by another user.
Case 70
IsFileOpen = True
' Another error occurred.
Case Else
Error errnum
End Select
End Function
问题是,当我使用SharePoint URL运行此代码时,无论文件是否正在使用,返回的错误代码为75(Path/File access error (Error 75)。
将SharePoint 365位置映射为网络驱动器(WebDav)不是一种选择。根据{{3}}中的描述,维护的内容太多了。对于最终用户来说,这样做太过分了。
我需要找到一种方法来检查VBA,如果存储在SharePoint Online中的文件打开使用,同时使用URL访问该文件。
欢迎提示。
编辑:注释建议的方法以只读方式打开工作簿,然后更改工作簿属性,或打开工作簿并评估.ReadOnly
属性。此方法的问题是,如果将工作簿从“只读”更改为“编辑”,则会弹出此对话框:
即使我点击打开只读副本,结果也是错误的。如果我可以抑制此对话框,而是评估导致对话框出现的触发器,我可以从那里获取它。
答案 0 :(得分:0)
我还使用SharePoint&Office 365。 下面的代码为我工作。在其中,pickListFilePath是SharePoint 365上文件的路径。
'Open the workbook. If it is already open (opens as read only),close it and loop to retry until it opens as editable.
TryOpenUntilOpenToEdit:
Dim objExcel : objExcel = CreateObject("Excel.Application")
objExcel.DisplayAlerts = False
objExcel.ScreenUpdating = False
On Error Resume Next
Dim objPickListWB : objPickListWB = objExcel.Workbooks.Open(pickListFilePath, Notify:=False)
If Err().Number <> 0 Or objPickListWB.ReadOnly Then
objPickListWB.Close
objExcel.Quit
Err.Clear()
GoTo TryOpenUntilOpenToEdit
End If