首先在这里发帖,如果我不遵守任何准则,我道歉。
以下是我的挑战:我有一个状态跟踪文件,该文件已保存到SharePoint。宏将打开此状态跟踪器,记录一些信息,保存并关闭文件。我试图包含一些代码来检测另一个用户是否打开了该状态文件,否则宏会在看到它无法保存更改时爆炸。我知道这不是一个非常优雅的系统,但它现在会做!
以下代码用于检测文件是否已打开,但仅适用于本地文件(例如保存到C:\驱动器)。我无法让它适用于保存到SharePoint的文件。我知道我的SharePoint文件路径是正确的,我可以使用“Workbooks.Open”操作打开该文件。当我尝试运行SharePoint文件的代码时,它总是返回该文件未被其他用户打开,即使它是。
我不想使用SharePoint的Checked Out功能并禁用它。我的团队不太勤于检查。
非常感谢!
'**********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
答案 0 :(得分:0)
在工作8小时后解决这个问题后,我发现了一个快速而肮脏的解决方案。它不是最好的,但经过大量的研究,到目前为止唯一合适的。这是我的代码:
“检测其他用户是否打开了SharePoint文件,如果是,则打开文件,如果没有关闭它”
Sub accessWorkbook()
Dim url As String
url = "mySharePointURL"
MsgBox workbookOpen(url)
End Sub
Function workbookOpen(url As String) As Boolean
'return false if file is not locked by another user
workbookOpen = False
'open the workbook in read.only mode, so does no message is displyed when the file is use
Set wb = Workbooks.Open(url, False, True)
'change file access to ReadWrite without notifying if the file is locked by another user
On Error Resume Next
wb.ChangeFileAccess xlReadWrite, False, False
'if the file is locked, this will return "true"
workbookOpen = wb.ReadOnly
'if the file is locked, it wil lbe closed without no changes
If read.only = True Then
wb.Close
End If
End Function
答案 1 :(得分:-1)
我今天遇到了同样的问题并解决了它,如下所述。
SharePoint上的文件:当有人打开此文件时,让它自动打开某个位置上的其他文件,然后您可以检查该文件是否已打开。
从我的主程序中截取的代码
'path to file on SharePoint
pathDatabank = "http://...sharepoint/sites/.... .xlsm"
'path to my temp file (this file is opened by my file on SharePoint)
pathTempfile = "\\ location you can check the file\....\temp.xlsx"
'check if the temp file is open - with your function
If IsWorkBookOpen(pathTempfile) Then
' Display a message stating the file is in use.
MsgBox "Data bank is in use by an other user."
Exit Sub
End If`
'before I close and save my file on SharePoint, I close the temp file
Workbooks("temp.xlsx").Close SaveChanges:=False
我的SharePoint文件中的代码打开临时文件 - ThisWorkbook
Private Sub Workbook_Open()
Workbooks.Open ("path to your temp file ... \temp.xlsx")
ThisWorkbook.Activate
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next 'because I closed the file in my main programme
Workbooks("temp.xlsx").Close SaveChanges:=False
End Sub