Monday, September 14, 2009

Microsoft Word 2003 Visual Basic macros to remove leading, trailing, or internal whitespace

http://25yearsofprogramming.com/msword/msword2003/whitespace2003.htm
In some situations you can deal with whitespace more simply than by using a macro. In the Find and Replace dialog box, click the More button and then the Special button. With some experimenting you'll discover that there are codes you can enter in the "Find what:" area to search for whitespace:

^p = paragraph mark
^t = tab
^w = any whitespace
If you check the "Use wildcards" box, the Special button brings up a different set of options. ^p and ^w will no longer be allowed as search expressions, but ^t is still ok, and the new options such as search for a particular number of occurrences can be useful.

If you have tabs and spaces all intermingled, you can do two-step transformations, such as changing all tabs to spaces and then searching for consecutive spaces, or changing every set of 5 spaces to a tab and searching for consecutive tabs.

There are some earlier versions of these macros for Word 6.0 WordBasic that are somewhat more clear if you're trying to understand the code, because every statement doesn't have "WordBasic." in front of it.


Trim Leading Whitespace
Attribute VB_Name = "TrimLeadingWhitespace"

Public Sub MAIN()
Attribute MAIN.VB_Description = "Remove leading spaces and tabs from each line in selection or from current line if no selection.\r\n"
Attribute MAIN.VB_ProcData.VB_Invoke_Func = "TemplateProject.TrimLeadingWhitespace.MAIN"
Dim leaveloop
Rem TRIM ALL LEADING WHITESPACE FROM EACH LINE IN SELECTION.
Rem TO DO WHOLE FILE, SELECT ALL FIRST.
Rem Copyright (C)2000 Steven Whitney.
Rem Published under GNU GPL (General Public License) Version 3, with ABSOLUTELY NO WARRANTY.
Rem 11-24-99
Rem MODIFIED FROM TABOUT
Rem ----------------------------------------------------------------------------
Rem IF NO SELECTION, DELETE THE WHITESPACE, IF ANY, AT START OF CURRENT LINE
If Len(WordBasic.[Selection$]()) <= 1 Then
WordBasic.StartOfLine 'ALSO CANCELS SELECTION, IF ANY
Rem VERY WEIRD BUG: IF LINE STARTS WITH A SINGLE SPACE AND A "(",
Rem (SOME OTHER CHARS BESIDES "(" CAUSE SAME PROBLEM)
Rem THEN THE MACRO HANGS UP. INSERT A DOUBLE SPACE SO THAT IF THERE
Rem WAS ONLY A SINGLE OR NONE, IT IS NOW A DOUBLE.
WordBasic.Insert " "
WordBasic.StartOfLine
While WordBasic.[Selection$]() = Chr(9) Or WordBasic.[Selection$]() = Chr(32)
WordBasic.WW6_EditClear
Wend
GoTo Bye
End If
Rem ----------------------------------------------------------------------------
Rem THERE WAS A SELECTION. PROCESS ALL LINES IN IT.
Rem #ERROR: I THINK THIS IS WHERE SPACES ARE STILL BEING LEFT INSIDE THE TEXT:
Rem IF YOU PROCESS FROM TOP TO BOTTOM, THE LINE ENDS CHANGE,
Rem SO IT SHOULD GO FROM BOTTOM UP. But a simple pattern-matching
Rem search may replace this anyway.

WordBasic.CopyBookmark "\Sel", "xxTabTemp"
WordBasic.SelType 1
leaveloop = 0
While WordBasic.CmpBookmarks("\Sel", "xxTabTemp") = 8 _
Or WordBasic.CmpBookmarks("\Sel", "xxTabTemp") = 6 _
And leaveloop <> 1
WordBasic.StartOfLine
Rem SEE ABOVE FOR WHY THIS IS NECESSARY
WordBasic.Insert " "
WordBasic.StartOfLine
While WordBasic.[Selection$]() = Chr(9) Or WordBasic.[Selection$]() = Chr(32)
WordBasic.WW6_EditClear
Wend
WordBasic.EndOfLine
If WordBasic.CmpBookmarks("\Sel", "\EndOfDoc") = 0 Then leaveloop = 1
WordBasic.CharRight
Wend
WordBasic.WW7_EditGoTo "xxTabTemp"
WordBasic.EditBookmark "xxTabTemp", Delete:=1

Bye:

End Sub
--------------------------------------------------------------------------------

Trim Trailing Whitespace
Attribute VB_Name = "TrimTrailingWhitespace"

Public Sub MAIN()
Attribute MAIN.VB_Description = "Trim all whitespace, in selection or document, that immediately precedes paragraph marks."
Attribute MAIN.VB_ProcData.VB_Invoke_Func = "TemplateProject.TrimTrailingWhitespace.MAIN"
Rem Copyright (C)2000 Steven Whitney.
Rem Published under GNU GPL (General Public License) Version 3, with ABSOLUTELY NO WARRANTY.
Rem 11/1/00
Rem Trims all whitespace that precedes paragraph marks (thus useless),
Rem and which messes up RemoveHardCRs macro.
Rem Should be enhanced.
WordBasic.StartOfDocument
WordBasic.EditReplace Find:="^w^p", Replace:="^p", Direction:=0, MatchCase:=0, WholeWord:=0, PatternMatch:=0, SoundsLike:=0, ReplaceAll:=1, Format:=0, Wrap:=0
End Sub
--------------------------------------------------------------------------------

Trim Internal Whitespace
Attribute VB_Name = "TrimInternalWhitespace"

Public Sub MAIN()
Rem COMPRESS ALL internal WHITESPACE IN THE SELECTION DOWN TO 1 SPACE.
Rem Copyright (C)2000 Steven Whitney.
Rem Published under GNU GPL (General Public License) Version 3, with ABSOLUTELY NO WARRANTY.
Rem 11-17-00, 4/26/04
Rem ----------------------------------------------------------------------------

If Len(WordBasic.[Selection$]()) <= 1 Then
WordBasic.MsgBox "You must select a block of text before calling this macro.", "Cannot Continue", 48
GoTo Bye
End If

Rem change all whitespace to a single space
WordBasic.EditReplace Find:="^w", Replace:=" ", Direction:=0, MatchCase:=0, WholeWord:=0, PatternMatch:=0, SoundsLike:=0, ReplaceAll:=1, Format:=0, Wrap:=0

Rem restore single spaces following periods to doubles
WordBasic.EditReplace Find:=". ", Replace:=". ", Direction:=0, MatchCase:=0, WholeWord:=0, PatternMatch:=0, SoundsLike:=0, ReplaceAll:=1, Format:=0, Wrap:=0

Bye:

End Sub

No comments:

Post a Comment