Skip to content

Instantly share code, notes, and snippets.

@devietti
Last active December 20, 2015 03:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devietti/6067922 to your computer and use it in GitHub Desktop.
Save devietti/6067922 to your computer and use it in GitHub Desktop.
A Microsoft Word macro that looks through a document finding various styling errors. I used this to help make the formatting in my dissertation more consistent. For more info see http://linuxforlovers.wordpress.com/2013/07/23/hack-your-word-documents-with-vba/. Tested in Word 2011 (for Mac).
'Option Explicit
' Copyright (c) 2013, Joseph Devietti
' All rights reserved.
' Redistribution and use in source and binary forms, with or without
' modification, are permitted provided that the following conditions are met:
' * Redistributions of source code must retain the above copyright
' notice, this list of conditions and the following disclaimer.
' * Redistributions in binary form must reproduce the above copyright
' notice, this list of conditions and the following disclaimer in the
' documentation and/or other materials provided with the distribution.
' * Neither the name of the <organization> nor the
' names of its contributors may be used to endorse or promote products
' derived from this software without specific prior written permission.
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
' ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
' WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
' DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
' DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
' (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
' LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
' ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
' (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
' SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Sub LintWarning(msg As String)
Dim continue As String
continue = MsgBox(msg & vbCrLf & "Continue?", vbYesNo)
If continue = vbNo Then
End
End If
End Sub
Sub LintSearch(ByVal needle As String, msg As String, wildcards As Boolean)
' Generate a warning message for each hit
' start searching at beginning
Selection.GoTo What:=wdGoToPage, Which:=wdGoToFirst
With Selection.Find
.ClearFormatting
.Forward = True
.MatchWildcards = wildcards
.matchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.Wrap = wdFindStop
.Text = needle
End With
Do While Selection.Find.Execute
LintWarning "'" & Selection.Text & "' " & msg
Loop
End Sub
Sub LintAssertStyle(ByVal needle As String, msg As String, _
wildcards As Boolean, matchCase As Boolean, style As String)
' Generate a warning message for each hit that doesn't have the specified style
' start searching at beginning
Selection.GoTo What:=wdGoToPage, Which:=wdGoToFirst
With Selection.Find
.ClearFormatting
.Forward = True
.MatchWildcards = wildcards
.matchCase = matchCase
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.Wrap = wdFindStop
.Text = needle
End With
Do While Selection.Find.Execute
If Not Selection.style = style Then
LintWarning "'" & Selection.Text & "' " & msg
End If
Loop
End Sub
Sub Lint()
' Lint Macro
' check for frames with borders
For Each aFrame In ActiveDocument.Frames
If aFrame.Borders.Enable Then
aFrame.Select
CurPage = Selection.Information(wdActiveEndPageNumber)
LintWarning "Found a frame with a border on page " & CurPage
End If
Next aFrame
' check for TextBoxes
For Each aShape In ActiveDocument.Shapes
If aShape.Type = msoTextBox Then
aShape.Select
CurPage = Selection.Information(wdActiveEndPageNumber)
LintWarning "Found a TextBox on page " & CurPage
End If
Next aShape
' check for ref fields that aren't hyperlinked
For Each aField In ActiveDocument.Fields
If aField.Type = wdFieldRef And InStr(aField.Code.Text, "\h") = 0 Then
aField.Select
LintWarning "'" & aField.Result.Text & "' isn't a hyperlink."
End If
Next aField
' Build list of ref fields to check for text that looks like a ref (e.g., "Figure 2") but isn't.
' Fields are larger than the actual "Figure 2" text, so build up a list of ref fields and then
' check that each piece of text is within some field.
Dim allRefs As New Collection
For Each aField In ActiveDocument.Fields
' collect ref fields as well as TOC fields (list of figures, tables, headings, etc.) so we don't
' raise warnings for text in the latter
If aField.Type = wdFieldRef Or aField.Type = wdFieldTOC Then
allRefs.Add aField
End If
'LintWarning aField.Type & " " & aField.Result.Text
Next aField
MsgBox "Found # refs: " & allRefs.Count
' text that should be ref fields
Dim searchKeys As New Collection
searchKeys.Add "Figure [0-9]{1,3}"
searchKeys.Add "Section [0-9]{1,3}"
searchKeys.Add "Table [0-9]{1,3}"
For Each searchKey In searchKeys
' start searching from the beginning
Selection.GoTo What:=wdGoToPage, Which:=wdGoToFirst
With Selection.Find
.ClearFormatting
.Forward = True
.MatchWildcards = True
.matchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.Wrap = wdFindStop
.Text = searchKey
End With
Do While Selection.Find.Execute
If (Not Selection.style = wdStyleCaption) Then
Dim found As Boolean
found = False
For Each aRef In allRefs
If Selection.InRange(aRef.Result) Then
found = True
Exit For
End If
Next aRef
If Not found Then
LintWarning "'" & Selection.Text & "' is a fake reference. "
End If
End If
Loop
Next searchKey
' check for deprecated scheme names
Dim badTerms As New Collection
badTerms.Add "Hw-DMP"
badTerms.Add "Sw-DMP"
badTerms.Add "RCDC-DMP"
badTerms.Add "DMP-O"
badTerms.Add "DMP-B"
badTerms.Add "DMP-PB"
For Each bad In badTerms
LintSearch bad, "is a deprecated term.", False
Next bad
' check for LaTeX artifacts
LintSearch "-^w", "may be LaTeX hyphenation", False
LintSearch "—", "is a LaTeX dash", False
' e.g. and i.e. need commas
'LintSearch "e.g.[!,]", "needs a comma", True
'LintSearch "i.e.[!,]", "needs a comma", True
' check for proper styling
' schemes
Dim schemes As New Collection
schemes.Add "Det-Serial"
schemes.Add "Det-ShTab"
schemes.Add "Det-TM"
schemes.Add "Det-TMFwd"
schemes.Add "DMP-Serial"
schemes.Add "DMP-ShTab"
schemes.Add "DMP-TMFwd"
schemes.Add "CoreDet-ShTab"
schemes.Add "Det-TSO"
schemes.Add "Det-HB"
schemes.Add "CoreDet-TSO"
schemes.Add "CoreDet-HB"
For Each sch In schemes
LintAssertStyle sch, "should be styled as a scheme", False, False, "scheme"
Next sch
' insn/function names
Dim codes As New Collection
codes.Add "BufferedStore"
codes.Add "StartInsnCount"
codes.Add "StopInsnCount"
codes.Add "ReadInsnCount"
codes.Add "SaveBufferedLines"
codes.Add "RestoreBufferedLines"
codes.Add "BufferFull"
codes.Add "QuantumReached"
codes.Add "end_quantum"
codes.Add "sync_acquire"
codes.Add "deterministic_lock"
codes.Add "deterministic_unlock"
codes.Add "sync_release"
codes.Add "wait_for_turn"
For Each cod In codes
LintAssertStyle cod, "should be styled as code", False, False, "code inline"
Next cod
LintAssertStyle "Commit", "should be styled as code", False, True, "code inline"
' e.g. and i.e.
'LintAssertStyle "e.g.", "should be emphasized", False, False, "Emphasized"
'LintAssertStyle "i.e.", "should be emphasized", False, False, "Emphasized"
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment