VBA Resources For MS Word

Christopher Rath

2014-04-10

MS Word

These are MS Word-specific VBA tips:

Sub CompressEmptyLines()
'
' CompressEmptyLines()
' This macro is a short-cut method to find and replace all occurrences of two side-by-side paragraph
' markers with a single paragraph marker.  The macro operates on whatever text is currently selected.
'
    With Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "^p^p"
        .Replacement.Text = "^p"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute Replace:=wdReplaceAll
    End With
End Sub
Sub RemoveTrailingWhitespace()
'
' RemoveTrailingWhitespace()
' This macro uses find and replace to remove trailing whitespace from the end of all the
' paragraphs in a document.  The macro operates on whatever text is currently selected.
'
    With Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "^w^p"
        .Replacement.Text = "^p"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute Replace:=wdReplaceAll
    End With
End Sub

Other Resources

See the page that linked to this one for other VBA resources I have found helpful.


©Copyright 2005–2014, Christopher Rath
Telephone: 613-824-4584
Address: 1371 Major Rd., Ottawa, ON, Canada K1E 1H3
Last updated: 2014/04/10 @ 07:55:16 ( )