Step 1: Create a class module in a dotm that loads at startup.
There needs to be an application hook that is always available. The easiest way to do this is to create a class module in a macro-enabled template residing in the startup folder for MS Word that has application-level events. F0r 2010 that location is typically C:\Program Files\Microsoft Office\Office14\STARTUP.
Create a macro enabled template and as part of that creation, create a class module. I called mine ApplicationEvents. Code the app event DocumentBeforeSave as it appears below:
Public WithEvents App As Word.Application
Public BSave As New <yourclassmodule dotm name>.ApplicationEvents
Here is the code in the Sub itself for copy and paste purposes:
Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI as Boolean, Cancel As Boolean)
'Exit this process if the document has never been saved before
If InStr(ActiveDocument.FullName, "\") = 0 Then Exit Sub
'save the document
'hook the SVN actions
Now you need to write the SVN call itself. You can put this code anywhere you want, as long as it is somewhere the App_DocumentBeforeSave Sub can see it. So if it is not in the same dotm as the class module, where you do put it has to be referenced by the dotm where the class module lives. For simplicity purposes, you are better off keeping it all together.
Step 2: Code to invoke SVN actions.
The simplest way to do this is to create a bat file on the fly and use the Shell command to tell SVN to execute the bat file. The code below is what is called from the Application event we set up in the startup dotm above:
Function SVNAddCommit(strFullName As String) As Boolean
Const cUpdateFileName as String = "svn_update.bat"Const cClean As String = "svn cleanup"
Const gkMyDrafts as String = "C:\Work_Documents\"
Dim strMsg As String
Dim strSVNString As String
Dim strDraftPath As String
Dim strCommit As String
Dim strAddDelete As String
Dim strBatFile As String
Dim FileNumber As String
Dim strCleanupString as string
On Error GoTo trap
'if the global variable is not instantiated, do so
If BSave.App Is Nothing Then Set BSave.App = Word.Application
'create a cleanup string to ensure the repo is always good to go
strCleanupString = "cd " & Chr(34) & gkMyDrafts & Chr(34) & vbCrLf & cClean & vbCrLf
'create the batfile
strBatFile =gkMyDrafts & cUpdateFileName
'test to see if the document is where the svn is located. If the file being saved is not in my work folder and so not under version control, bail out.
If InStr(strFullName, gkMyDrafts) = 0 Then GoTo Done
'Create add string. This string will ensure any newly added files to the directory are added to svn. it's ignored if
'the file is already added
strAddDelete = "svn add " & strFullName & vbCrLf
strMsg = "Saved " & Now()
strCommit = "svn commit --force-log -m "
strSVNString = strCleanupString & strAddDelete & strCommit & Chr(34) & strMsg & Chr(34) & Chr(32) & strFullName
'create a bat file to execute
FileNumber = FreeFile()
Open strBatFile For Output As #FileNumber
Print #FileNumber, strSVNString
'execute the bat file
'All is well, set the function to true and go
SVNAddCommit = True
That's it. Every time you save a file, it will check to see if it is in the Work_Documents folder, and if it is, it will commit the saved document to the SVN repository, creating a micro-version environment for you.