第一篇文章在这里,我很抱歉,如果我在任何指导方针。检测SharePoint文件是否打开
这是我的挑战:我有一个状态跟踪文件保存到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
任何想法乡亲?我可以澄清的任何事情? – 2014-09-15 12:38:52
http://stackoverflow.com/questions/9373082/detect-whether-excel-workbook-is-already-open – R3uK 2016-06-09 17:26:29