简体   繁体   English

如何检查当前用户是否已从共享工作簿中删除?

[英]How to check whether the current user has been removed from the shared workbook?

I have a shared document which runs various commands upon close. 我有一个共享文档,该文档在关闭时会运行各种命令。 This includes saving normally (if document is still shared) and saving as shared (if document was unshared). 这包括正常保存(如果仍在共享文档)和另存为共享(如果未共享文档)。

The problem arises when a person left the document open for a while then close it. 当有人将文档打开一段时间然后关闭时,就会出现问题。 The document automatically overrides the current document (if the document was made unshared, or it gives the option to overide when the .save command runs) and the data which was entered meanwhile is potentially lost. 该文档会自动覆盖当前文档(如果使文档成为非共享文档,或者它在运行.save命令时提供了覆盖选项),并且同时输入的数据可能会丢失。

How do I check if the user is removed from the document, so that the saving section can be skipped if that's the case? 如何检查是否从文档中删除了用户,以便在这种情况下可以跳过保存部分?

Private Sub Workbook_BeforeClose(Cancel As Boolean)

    For i = 1 To Sheets.Count     
        If Sheets(i).ProtectContents = False Then
            Call Protect_Sheets 'Protects all the sheets
        End If
    Next i

    If ActiveWorkbook.ProtectStructure = False Then
        Call Protect_Workbook 'Protects the workbook
    End If

    If ActiveWorkbook.MultiUserEditing = False Then
        Call SaveAsShared 'Saves the workbook as shared (overrides)
    Else
        ActiveWorkbook.Save 'Only saves as normal when the document was shared upon close (but defaults the current document name (which will override when no attention is paid))
    End If

End Sub

If possible, I want another 'If' (just before calling the SaveAsShared function), which terminates the Sub when the user has been removed from the workbook. 如果可能的话,我想要另一个“ If”(在调用SaveAsShared函数之前),当从工作簿中删除用户时,它将终止Sub。 Any help will be much appreciated! 任何帮助都感激不尽! Thanks in advance! 提前致谢!

Lou

I have come across a temporary fix. 我遇到了一个临时修复程序。 I use a command (ShowConflictHistory) which returns an error when you have been kicked out of the document, then I use error handling techniques to save a unique copy; 我使用了一个命令(ShowConflictHistory),当您将其踢出文档后,该命令将返回错误,然后使用错误处理技术来保存唯一的副本。

On Error GoTo Errhandler:
If ActiveWorkbook.ShowConflictHistory = False Or True Then 'This returns Err 1004 if the person has been kicked from the workbook, so it can be handled accordingly in ErrHandler.
End If

Continue1: 
On Error Resume Next

'''''''''
'Main code section
'''''''''

Exit Sub

Errhandler:

Select Case Err
     Case 1004: 'The error which results if you've been kicked out of the document.
        Call SaveCopyOfShared
        Exit Sub    
     Case Else:
        GoTo Continue1:
End Select

End Sub

If someone comes up with a more conventional solution. 如果有人提出了更常规的解决方案。 Please let me know. 请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM