Slang's Anti-Singularity
  • Slanghome
  • Slangblog
  • Slangpuzzles
    • CurrentPuzzles
    • ScholarPuzzle
    • PastPuzzles

MS Word and SVN: A Nice Match - Part 2

4/10/2012

0 Comments

 
Part 1 of this two-part series focused on creating the SVN Repository itself. Part 2 is delving into the specifics of hooking the save action on a document in that folder to automatically add/commit the changed document to the SVN repository. This involves creating an application level event, and code to fire it and the actual SVN code based on your document's location.

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:
Picture
In addition to the code you have in the App_DocumentBeforeSave Sub, you will need to declare a couple of public variable that can access it. At the top of the module in your declaration area, declare your public variables:

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
    ActiveDocument.Save
    'hook the SVN actions     
   Call SVNCalls.SVNAddCommit(ActiveDocument.FullName)
End Sub
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
Close #FileNumber
'execute the bat file
Shell strBatFile
'All is well, set the function to true and go
SVNAddCommit = True

Done:

    Exit Function

trap:

    Err.Raise (Err.Number)
    GoTo Done
 End Function

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.
0 Comments



Leave a Reply.

    RSS Feed

    Author

    Just a guy out exploring the world. Former world-class never-was endurance runner.

    ​Hit me up, and we'll catch a beer or coffee in your town.


    Follow @slang4201

    Archives

    August 2022
    July 2022
    June 2022
    May 2022
    April 2022
    March 2022
    February 2022
    January 2022
    December 2021
    November 2021
    October 2021
    September 2021
    August 2021
    May 2021
    October 2020
    September 2020
    December 2019
    November 2019
    October 2019
    July 2019
    October 2018
    September 2018
    May 2018
    April 2018
    November 2017
    October 2017
    July 2017
    June 2017
    June 2015
    August 2014
    January 2014
    October 2013
    September 2013
    June 2013
    May 2013
    April 2013
    March 2013
    February 2013
    January 2013
    December 2012
    November 2012
    October 2012
    September 2012
    August 2012
    July 2012
    June 2012
    May 2012
    April 2012
    March 2012
    February 2012
    January 2012
    December 2011
    November 2011
    October 2011
    August 2011
    March 2011
    October 2010
    July 2010
    January 2009
    December 2008
    October 2008

    Categories

    All
    2011
    Alfama
    Alternate Energy
    Android
    Angela Sullivan
    Animals
    Antiques
    Apple
    Ash Canyon
    Astronomy
    AT&T
    Australia 2022
    Bailout
    Battery
    Bicycling
    Biometrics
    Books
    Brisbane
    Cairns
    Canary Islands
    Carrier Iq
    Carson City
    Cascais
    Centennial
    C Hill
    C-Hill
    Christmas
    Climate
    Clothing
    Coding
    Colorado
    Columbus
    Cramps
    Curiosity
    Dad
    Dardanelles Lake
    Dell
    Dick's Lake
    Dilbert
    Diving
    Eagle Lake
    Earworms
    Eating Problems
    Eclipse
    Economy
    Ecuador
    Education
    Eldorado Canyon
    El Valle
    Energy
    Errors
    Espionage
    Europe 2019
    Evi
    Fallon
    Family
    Fontanillis Lake
    Food
    Gamboa
    Garmin
    Geocaching
    Goals
    Google
    Google Earth
    Grouse Lake
    Hiking
    Inov8
    Investing
    Ipad
    Iphone
    Iron Mountain
    Lanzarote
    Legislatures
    Lisbon
    Mac
    Market
    Market Drop
    Mars
    Mctarnahan
    Medicine
    Microsoft Word
    Motivation
    Mountain Biking
    Moving Minutes
    Music
    Nevada Day
    Nfc
    Ohio
    Olympics
    Openoffice
    Opportunity
    Panama 2018
    Panama 2022
    Paper Airplane
    People
    Playa Coronado
    Politics
    Portugal
    Prison Hill
    Privacy
    Puts
    Puzzles
    Quito
    Race
    Rant
    Reno
    Retrospective
    Roosevelt
    Running
    Running Dynamics
    Saddest Cities
    Safe & Sober
    Science
    Scuba
    Shoes
    Shopping
    Sicily
    Sierra
    Sierra Canyon
    Sintra
    Slangsploration
    Snl
    Soccer
    Software
    Spasms
    Spirit
    Sullivan Canyon
    Svn
    Tahoe
    Tahoe Rim Trail
    Taormina
    Taxes
    Technology
    Transit
    Travel
    Trees
    Vba
    Velma Lakes
    Venus
    Verizon
    Violin
    Watches
    Weather
    Wolframalpha
    Words
    Wrestling
    Writing
    Xkcd
    Yawbe
    Yoga

This is ALL MINE, I tell you! copyright 2010-2022