Created
May 24, 2013 15:17
-
-
Save roelle/5644244 to your computer and use it in GitHub Desktop.
Short script which uses a token to lock a Word Document which is shared (e.g. in a Dropbox folder) to one user so that other users do make changes simultaneously.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' Spec: | |
' On open... | |
' Get the file name for the current Word document. | |
' Check for an existing token file. | |
' If there is a token, warn the user and ask for permission to continue (by default, exit). | |
' Move the token to token.overwritten so that the user who got screwed may have some chance to recover. | |
' If there is no existing token, establish one for this user. | |
' Get the user name and current date and time. | |
' Create a token file with pertinent information. | |
' Store the token information in the open file to compare when closure is attempted. | |
' Turn on change tracking | |
' On save... | |
' Check the token information stored at file opening against the file token to make sure another user did not override this users token. | |
' If the token matches, save the document (update the token?). | |
' If the token does not match, warn the user. Prompt the user to save the file with a new name until the differences with the other user can be resolved. | |
' On close... | |
' Delete the token | |
' Close the document | |
Dim savedToken As String | |
Const fileExtension As String = ".lockbox" | |
Sub AutoOpen() | |
' | |
' Generate token data and if the token isn't held, claim it (or steal it). | |
' | |
' Create a new token. | |
savedToken = ActiveDocument.BuiltInDocumentProperties("Author") _ | |
& " (" & Environ("USERNAME") & "); " _ | |
& Date & " " & Time & "; " _ | |
& ActiveDocument.Path & Application.PathSeparator & ActiveDocument.Name | |
' If there is no token, grab it (write this token to the file). | |
If TokenExists() Then | |
' Let the checking function handle the UI | |
TokenMatches (savedToken) | |
Else | |
WriteToken (savedToken) | |
End If | |
' Turn change-tracking on in the document. | |
ActiveDocument.TrackRevisions = True | |
End Sub | |
Private Function tokenPath() As String | |
tokenPath = ActiveDocument.Path & Application.PathSeparator & ThisDocument.Name & fileExtension | |
End Function | |
Sub FileSave() | |
' | |
' This VBA script checks a saved token versus the file to makes sure this user is still the token owner. If they are, the file is saved and the token released (token file is deleted). Otherwise, the user is prompted to save the file. | |
' | |
' If you can update the token, then save the document. | |
If TokenExists() Then | |
If TokenMatches(savedToken) Then | |
ActiveDocument.Save | |
End If | |
Else | |
If TokenClaimed(savedToken) Then | |
ActiveDocument.Save | |
End If | |
End If | |
End Sub | |
Sub FileClose() | |
' When the file is closed, reference the auto-close subroutine. | |
ActiveDocument.Close (Word.WdSaveOptions.wdDoNotSaveChanges) | |
End Sub | |
Sub AutoClose() | |
' | |
' This VBA script creates a token file to indicate the document has been opened so that other Dropbox users do not unwittingly attempt to change it until the first user has closed it. The token is created on file open and deleted on file closure. | |
' | |
If Not ThisDocument.Saved Then | |
Select Case MsgBox("Do you want to save the changes you made to " & ActiveDocument.Name & "?", _ | |
vbQuestion + vbYesNoCancel) | |
Case vbYes | |
FileSave | |
Case vbCancel | |
Exit Sub | |
End Select | |
End If | |
On Error Resume Next | |
DeleteToken (savedToken) | |
ActiveDocument.Close (Word.WdSaveOptions.wdDoNotSaveChanges) | |
End Sub | |
Private Function WriteToken(token As String) As String | |
' | |
' Write the token file. Overwrite if it exists. | |
' | |
fnum = FreeFile() | |
Open tokenPath() For Output As fnum | |
Print #fnum, token | |
Close #fnum | |
End Function | |
Private Function DeleteToken(token As String) As Boolean | |
' | |
' Delete the token (if the file token matches the stored token information from creation). | |
' | |
DeleteToken = False | |
If TokenMatches(token) Then | |
Kill ActiveDocument.Path & Application.PathSeparator & ThisDocument.Name & fileExtension | |
DeleteToken = True | |
End If | |
End Function | |
Private Function TokenMatches(token As String) As Boolean | |
' | |
' Check the token file against the one saved for this file-open cycle. | |
' | |
' Assume the file exists | |
Dim fso As Scripting.FileSystemObject | |
Set fso = New Scripting.FileSystemObject | |
Set tokenFileStream = fso.OpenTextFile(tokenPath(), ForReading, True) | |
currentToken = tokenFileStream.ReadLine | |
If currentToken = token Then | |
TokenMatches = True | |
Else ' Steal the token | |
TokenClaimed (token) | |
End If | |
End Function | |
Private Function TokenClaimed(token As String) As Boolean | |
' | |
' Claim the token if has been ganked by a nefarious co-worker | |
' | |
TokenClaimed = False | |
Response = MsgBox("Someone else is using this file. Do you want to override and steal the file?", _ | |
vbYesNo, "WARNING!", "", 1000) | |
If Response = vbYes Then | |
WriteToken (token) | |
If TokenMatches(token) Then | |
TokenClaimed = True | |
End If | |
End If | |
End Function | |
Private Function TokenExists() As Boolean | |
' | |
' Check if the token exists | |
' | |
TokenExists = False | |
Dim fso As Scripting.FileSystemObject | |
Set fso = New Scripting.FileSystemObject | |
If fso.FileExists(tokenPath()) Then | |
TokenExists = True | |
End If | |
End Function | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment