【问题标题】:Detect if SharePoint File is Open检测 SharePoint 文件是否打开
【发布时间】: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


【解决方案1】:

在工作超过 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

【讨论】:

  • 我收到了自动化错误,而不是更改访问权限,但是当我之后进行 wb.ReadOnly 测试时,事实上,对 wb 对象的任何访问都会给我一个自动化错误。 (Excel 2013)。
【解决方案2】:

我今天遇到了同样的问题,并按照下面的说明解决了。

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

【讨论】:

    猜你喜欢
    • 2015-08-30
    • 1970-01-01
    • 1970-01-01
    • 2011-10-21
    • 2021-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    相关资源
    最近更新 更多