问题
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.
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. Any help will be much appreciated! Thanks in advance!
Lou
回答1:
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;
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.
来源:https://stackoverflow.com/questions/26753634/how-to-check-whether-the-current-user-has-been-removed-from-the-shared-workbook