Attribute VB_Name = "SvnProperties4MSOffice" ' Integrating SVN Properties into MS Office Documents ' ---------------------------------------------------- ' $Author:: oetiker $ ' $Date:: 2010-04-27 13:38:03 +0200 $ ' $Rev:: 277 $ ' ' Task: ' Make subversion revision and keywords available as ' field values an Office Document. ' ' Idea: ' By embedding fixed-length svn keyword $Id:: $ into ' a custom document property and enabeling keyword substitution ' on word documents, we get all the information we need ' made available inside the document after svn commit ' ' Solution: ' This macro module adds AutoNew and AutoUpdate ' macros to your document or template. They make ' sure that $Id:: $ property is created. And once ' it is filled they add additional properties in human ' readable format. ' ' Usage: ' Adding the Macros to a document/template ' * Extras -> Macros -> Visual Basic Editor ' * Import ' ' Using the fields in your document ' * Insert -> Field -> DocProperty ' * Pick one of the svn* properties ' ' Configuring Subversion/Tortoise ' * In the Properties dialog of the document add ' svn:keywords property value "Id" ' * Get automatic properties by adding this ' line to the tortoise config file ' *.doc = svn:keywords = Id ' ' Enjoy ' Tobi Oetiker tobi at oetiker ch ' http://tobi.oetiker.ch ' 2007-04-28 ' Option Base 0 Option Explicit ' System time structure Public Type SYSTEMTIME wYear As Integer wMonth As Integer wDayOfWeek As Integer wDay As Integer wHour As Integer wMinute As Integer wSecond As Integer wMilliseconds As Integer End Type ' Time zone information. Note that this one is defined wrong in API viewer. Private Type TIME_ZONE_INFORMATION Bias As Long StandardName(0 To 31) As Integer StandardDate As SYSTEMTIME StandardBias As Long DaylightName(0 To 31) As Integer DaylightDate As SYSTEMTIME DaylightBias As Long End Type Private Declare Function SystemTimeToTzSpecificLocalTime Lib "kernel32" (lpTimeZoneInformation As TIME_ZONE_INFORMATION, lpUniversalTime As SYSTEMTIME, lpLocalTime As SYSTEMTIME) As Long Private Declare Function GetTimeZoneInformation Lib "kernel32" (lpTimeZoneInformation As TIME_ZONE_INFORMATION) As Long Sub AutoNew() Attribute AutoNew.VB_Description = "Makro erstellt am 4/28/2007 von Tobias Oetiker" Attribute AutoNew.VB_ProcData.VB_Invoke_Func = "Normal.NewMacros.AutoOpen" Dim sProp As String Dim sSvnId As String sSvnId = "zzz_svn_id" If Not IsProp(sSvnId) Then CreateProp sSvnId UpdateProp sSvnId, "$Id:: $" End If UpdateSvnProps GetProp(sSvnId) End Sub Sub AutoOpen() AutoNew UpdateFields End Sub ' Create Custom Document Properties for the SVN Keywords Private Sub UpdateSvnProps(sId As String) Dim lStart As Long Dim lEnd As Long Dim i As Integer Dim aProp As Variant Dim sProp As String Dim dDate As Date Dim dTime As Date Dim sValue As String aProp = Array("svnFile", "svnRev", "date", "time", "svnAuthor") lStart = InStr(1, sId, " ") + 1 lEnd = InStr(lStart, sId, " ") For i = 0 To 4 sProp = aProp(i) sValue = Mid(sId, lStart, lEnd - lStart) If (sValue <> "") Then Select Case sProp Case "time" 'time ends in Z sValue = Mid(sId, lStart, lEnd - lStart - 1) dTime = TimeValue(sValue) Case "date" dDate = DateValue(sValue) Case Else If Not IsProp(sProp) Then CreateProp sProp UpdateProp sProp, sValue End Select End If lStart = lEnd + 1 lEnd = InStr(lStart, sId, " ") Next i If Not IsProp("svnDateLocal") Then CreateProp "svnDateLocal" UpdateProp "svnDateLocal", Format(UTCtoLocal(dDate + dTime), "dd.MM.yyyy HH:mm:ss") If Not IsProp("svnDateUTC") Then CreateProp "svnDateUTC" UpdateProp "svnDateUTC", Format(dDate + dTime, "dd.MM.yyyy HH:mm:ss") End Sub ' As suggested by MS KB Private Sub UpdateFields() Dim aStory As Range Dim aField As Field For Each aStory In ActiveDocument.StoryRanges For Each aField In aStory.Fields aField.Update Next aField Next aStory End Sub ' Convert UTC time to local time for current time zone ' Function Created by jens.cameron(at)dilab.se Private Function UTCtoLocal(ByVal tDate As Date) As Date Dim tzi As TIME_ZONE_INFORMATION Dim stUTC As SYSTEMTIME Dim stLocal As SYSTEMTIME Dim lRes As Long lRes = GetTimeZoneInformation(tzi) stUTC.wYear = Year(tDate) stUTC.wMonth = Month(tDate) stUTC.wDay = Day(tDate) stUTC.wHour = Hour(tDate) stUTC.wMinute = Minute(tDate) stUTC.wSecond = Second(tDate) stUTC.wMilliseconds = 0 lRes = SystemTimeToTzSpecificLocalTime(tzi, stUTC, stLocal) UTCtoLocal = DateSerial(stLocal.wYear, stLocal.wMonth, stLocal.wDay) + TimeSerial(stLocal.wHour, stLocal.wMinute, stLocal.wSecond) End Function Private Function IsProp(sPropName As String) As Boolean Dim sDummy As Variant On Error GoTo NoProp sDummy = ActiveDocument.CustomDocumentProperties(sPropName).Value IsProp = True Exit Function NoProp: Err.Clear IsProp = False End Function Private Function GetProp(sPropName As String) As String If IsProp(sPropName) Then GetProp = ActiveDocument.CustomDocumentProperties(sPropName).Value Else GetProp = "" End If End Function Private Sub CreateProp(sPropName As String) ActiveDocument.CustomDocumentProperties.Add Name:=sPropName, _ LinkToContent:=False, Type:=msoPropertyTypeString, Value:="" End Sub Private Sub UpdateProp(sPropName As String, sValue As Variant) If Not IsProp(sPropName) Then CreateProp (sPropName) End If ActiveDocument.CustomDocumentProperties(sPropName).Value = sValue End Sub