2014-09-04 87 views
1

第一篇文章在这里,我很抱歉,如果我在任何指导方针。检测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 
+0

任何想法乡亲?我可以澄清的任何事情? – 2014-09-15 12:38:52

+0

http://stackoverflow.com/questions/9373082/detect-whether-excel-workbook-is-already-open – R3uK 2016-06-09 17:26:29

回答

-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 
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 
+0

我得到自动化错误,而不是与更改访问,但是当我做了wb.ReadOnly测试后,实际上,任何对wb对象的访问都会给我一个自动化错误。 (excel 2013)。 – 2016-03-14 15:18:16