Back in the Bad Old Days when all the developers developed in the same sandbox, we manually highlighted our programming changes in what we called a “QCSub” document.  Not all differences would be your changes — changes made in the same files by anyone else would also show up in the diff document — so you’d look over each change and highlight it if it was yours.  Usually, most all the changes were yours, but it took some eyestrain to look over it all — and it was too easy to miss highlighting a change.

Thinking it would be a lot easier to unhighlight things you didn’t want than to highlight things you did want, I decided to see if I could learn enough of the Word object model to create a VBA macro to do the highlighting automatically.  So it was that the QCSubDocUtils macro was born.

Thankfully, nowadays, individual CVS sandboxes mean not having to deal with other random changes in your code when it’s up for review… but here for posterity is QCSubDocUtils.

‘Set of macros written by Daniel Meyer

‘Change Log:
’07/27/2005 – Daniel Meyer – Added QCSubDocNoHighlighting macro
’04/06/2006 – Daniel Meyer – Added SaveItAsText
’05/18/2006 – Daniel Meyer – Cleaned up Public/Private sub declarations.

Public Sub QCSubDoc()
End Sub

Private Sub DoQCHighlighting()

‘ Macro written 6/28/2005 by danielm

    Dim p As Paragraph
    For Each p In ActiveDocument.Paragraphs
      If p.Range.Characters.Count > 1 Then
        Select Case p.Range.Characters(1)
          Case “+”
            If p.Range.Characters.Count < 3 Then               p.Range.HighlightColorIndex = wdYellow             ElseIf Not (p.Range.Characters(2) = "+" And p.Range.Characters(3) = "+") Then 'Skip +++ lines               p.Range.HighlightColorIndex = wdYellow             End If           Case "-"             If p.Range.Characters.Count < 3 Then               p.Range.HighlightColorIndex = wdBrightGreen             ElseIf Not (p.Range.Characters(2) = "-" And p.Range.Characters(3) = "-") Then 'Skip --- lines               p.Range.HighlightColorIndex = wdBrightGreen             End If         End Select       End If     Next p End Sub Private Sub SwitchPageSettings() ' ' Macro written 6/29/2005 by danielm ' 7/14/2005 - danielm - change margins to zero all around.     With ActiveDocument.PageSetup         .Orientation = wdOrientLandscape         .PageWidth = InchesToPoints(20)         .PageHeight = InchesToPoints(8.5)         .TopMargin = InchesToPoints(0)         .BottomMargin = InchesToPoints(0)         .LeftMargin = InchesToPoints(0)         .RightMargin = InchesToPoints(0)     End With     If ActiveWindow.View.SplitSpecial = wdPaneNone Then         ActiveWindow.ActivePane.View.Type = wdNormalView     Else         ActiveWindow.View.Type = wdNormalView     End If End Sub Private Sub SetupKeyboardShortcuts()     CustomizationContext = NormalTemplate     KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyQ, wdKeyShift, wdKeyAlt), _         KeyCategory:=wdKeyCategoryMacro, Command:="QCSubDoc"     CustomizationContext = NormalTemplate     KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyM, wdKeyShift, wdKeyAlt), _         KeyCategory:=wdKeyCategoryMacro, Command:="QCSubDocNoHighlighting"     CustomizationContext = NormalTemplate     KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyU, wdKeyShift, wdKeyAlt), _         KeyCategory:=wdKeyCategoryMacro, Command:="UnhighlightText"     CustomizationContext = NormalTemplate     KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyO, wdKeyShift, wdKeyAlt), _         KeyCategory:=wdKeyCategoryMacro, Command:="HighlightTextGreen"     CustomizationContext = NormalTemplate     KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyN, wdKeyShift, wdKeyAlt), _         KeyCategory:=wdKeyCategoryMacro, Command:="HighlightTextYellow" End Sub Private Sub SaveAsWordDoc()   ActiveDocument.SaveAs FileFormat:=wdFormatDocument End Sub Public Sub UnhighlightText()   Selection.Range.HighlightColorIndex = wdAuto   Selection.Start = Selection.End End Sub Public Sub HighlightTextYellow() ' Macro written 6/27/2005 by danielm '   Selection.Range.HighlightColorIndex = wdYellow   Selection.Start = Selection.End End Sub Public Sub HighlightTextGreen() ' Macro written 6/27/2005 by danielm '   Selection.Range.HighlightColorIndex = wdBrightGreen   Selection.Start = Selection.End End Sub Public Sub SaveItAsText()   ChangeFileOpenDirectory "C:\temp\"  'Change this to your preferred temp directory   Dim docname As String   Dim pos As Integer   'Find position of extension in filename   docname = ActiveDocument.Name   pos = InStrRev(docname, ".")   If pos = 0 Then       'If the document has not yet been saved       'Ask the user to provide a filename       docname = InputBox("Please enter the name " & _           "of your document.")   Else       'Strip off extension and add ".txt" extension       docname = Left(docname, pos - 1)       docname = docname & ".txt"   End If   'Save file with new extension   ActiveDocument.SaveAs FileName:=docname, FileFormat:=wdFormatText End Sub [/sourcecode]


  1. QCSubDoc is the main entry point, but I bound UnhighlightText, HighlightTextYellow and HighlightTextGreen to keyboard shortcuts for manual alteration afterwards.
  2. The most sophisticated thing here (at least the one that took the most thinking and research) is the logic in DoQCHighlighting that loops through each logical line (“paragraph”) and examines the first few characters looking for the cases where the line should be highlighted.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s