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

时间:2014-11-05 09:32:02

标签: excel vba share

我有一个共享文档,它会在关闭时运行各种命令。这包括正常保存(如果文档仍然共享)并保存为共享(如果文档未共享)。

当一个人将文档打开一段时间然后关闭它时,就会出现问题。该文档会自动覆盖当前文档(如果文档未被共享,或者在.save命令运行时它提供了覆盖选项),并且同时输入的数据可能会丢失。

如何检查用户是否已从文档中删除,以便在遇到这种情况时可以跳过保存部分?

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

如果可能的话,我想要另一个'如果' (在调用SaveAsShared函数之前),当用户从工作簿中删除时,它终止Sub。 任何帮助都感激不尽! 提前谢谢!

1 个答案:

答案 0 :(得分:1)

我遇到过临时修复。我使用一个命令(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

如果有人提出更传统的解决方案。请告诉我。