【发布时间】:2020-05-16 04:00:37
【问题描述】:
第一次在这里发帖,如果我不遵守任何准则,我深表歉意。
这是我的挑战:我有一个保存到 SharePoint 的状态跟踪文件。宏将打开此状态跟踪器,记录一些信息,保存并关闭文件。我正在尝试包含一些代码来检测另一个用户是否打开了该状态文件,否则宏会在它看到它无法保存更改时爆炸。我知道这不是一个非常优雅的系统,但现在可以了!
下面的代码用于检测文件是否打开,但仅适用于本地文件(例如保存到 C:\ 驱动器)。我无法让它适用于保存到 SharePoint 的文件。我知道我的 SharePoint 文件路径是正确的,我可以使用“Workbooks.Open”操作打开文件。当我尝试运行 SharePoint 文件的代码时,它总是返回该文件未被其他用户打开,即使它是。
我宁愿不使用 SharePoint 的签出功能并将其禁用。我的团队不太勤奋地重新检查。
非常感谢!
'**********Function to check if workbook is open**********
Function IsWorkBookOpen(strFileName As String)
On Error Resume Next
' If the file is already opened by another process,
' and the specified type of access is not allowed,
' the Open operation fails and an error occurs.
Open strFileName For Binary Access Read Write Lock Read Write As #1
Close #1
'If no error, file is not open.
If Err.Number = 0 Then
IsWorkBookOpen = False
End If
'Error #70 is another user has the file open in edit mode.
If Err.Number = 70 Then
IsWorkBookOpen = True
End If
'Error #75 is another user has the file open in read only mode.
If Err.Number = 75 Then
IsWorkBookOpen = False
End If
End Function
'**********Running the actual code**********
Sub Button1_Click()
'Go into Status Sheet if it's not open. Otherwise skip it.
If IsWorkBookOpen("\\source.yadda.com\Top_Secret_File_Path\BCR Status Sheet.xlsm") Then
MsgBox ("'BCR Status Sheet.xlsm' is open.")
Else: MsgBox ("Open it up, do a bunch of stuff.")
End If
Workbooks.Open ("\\source.yadda.com\Top_Secret_File_Path\BCR Status Sheet.xlsm")
MsgBox ("Cruzin' along with rest of macro.")
End Sub
【问题讨论】:
-
大家有什么想法吗?有什么我可以澄清的吗?
标签: vba sharepoint