25 Years of Programming
An open source source for C, C++, OWL, BASIC, MDB, XLS, DOT, and more...
Home   Projects   Up   Sitemap   Search   Blog   Forum+Chat   About Us   Privacy   Terms of Use   Feedback   FAQ   Images   Services   Payments   Humor   Music

Microsoft Word 6.0 WordBasic macros to remove leading, trailing, or internal whitespace in a document

There are also versions of these for Word 2003 Visual Basic. That page has some tips for searching and replacing whitespace by using the Find and Replace box. It's not always necessary to use a macro.

TrimLeadingWhitespace - remove unwanted whitespace from the starts of lines

Remove leading spaces and tabs from each line in selection or from current line if no selection.

Sub MAIN
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 Initially published by http://25yearsofprogramming.com.
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(Selection$()) <= 1 Then
	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.
	Insert "  "
	StartOfLine
	While Selection$() = Chr$(9) Or Selection$() = Chr$(32)
		EditClear
	Wend
	Goto bye
EndIf
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.
CopyBookmark "\Sel", "xxTabTemp"
SelType 1
leaveloop = 0
While CmpBookmarks("\Sel", "xxTabTemp") = 8 \
		Or CmpBookmarks("\Sel", "xxTabTemp") = 6 \
		And leaveloop <> 1
	StartOfLine
	REM SEE ABOVE FOR WHY THIS IS NECESSARY
	Insert "  "
	StartOfLine
	While Selection$() = Chr$(9) Or Selection$() = Chr$(32)
		EditClear
	Wend
	EndOfLine					
	If CmpBookmarks("\Sel", "\EndOfDoc") = 0 Then leaveloop = 1
	CharRight
Wend
EditGoTo "xxTabTemp"
EditBookmark "xxTabTemp", .Delete
bye:
End Sub

TrimTrailingWhitespace - remove whitespace from the ends of lines

Trim all whitespace, in selection or document, that immediately precedes paragraph marks.

Sub MAIN
REM 11/1/00 
REM Copyright (C)2000 Steven Whitney.
REM Initially published by http://25yearsofprogramming.com.
REM Published under GNU GPL (General Public License) Version 3, with ABSOLUTELY NO WARRANTY.
REM Trims all whitespace that precedes paragraph marks (thus useless),
REM and which messes up RemoveHardCRs macro if present.
REM Should be enhanced.
StartOfDocument
EditReplace .Find = "^w^p", .Replace = "^p", .Direction = 0, .MatchCase = 0, .WholeWord = 0, .PatternMatch = 0, .SoundsLike = 0, .ReplaceAll, .Format = 0, .Wrap = 0
End Sub

TrimInternalWhitespace - compress all consecutive whitespace to one space

Sub MAIN
REM COMPRESS ALL internal WHITESPACE IN THE SELECTION DOWN TO 1 SPACE.
REM Copyright (C)2000 Steven Whitney.
REM Initially published by http://25yearsofprogramming.com.
REM Published under GNU GPL (General Public License) Version 3, with ABSOLUTELY NO WARRANTY.
REM 11-17-00
REM ----------------------------------------------------------------------------
If Len(Selection$()) <= 1 Then
	MsgBox "You must select a block of text before calling this macro.", "Cannot Continue", 48
	Goto bye
EndIf
REM change tabs to spaces
If MsgBox("Should I convert all TABs to spaces?", "TABS to Spaces?", 49) <> 0 Then
	EditReplace .Find = "^t", .Replace = " ", .Direction = 0, .MatchCase = 0, .WholeWord = 0, .PatternMatch = 0, .SoundsLike = 0, .ReplaceAll, .Format = 0, .Wrap = 0
EndIf
REM change multiple spaces to single
EditReplace .Find = " {2,}", .Replace = " ", .Direction = 0, .MatchCase = 0, .WholeWord = 0, .PatternMatch = 1, .SoundsLike = 0, .ReplaceAll, .Format = 0, .Wrap = 0
REM change single spaces following periods to doubles
EditReplace .Find = ". ", .Replace = ".  ", .Direction = 0, .MatchCase = 0, .WholeWord = 0, .PatternMatch = 0, .SoundsLike = 0, .ReplaceAll, .Format = 0, .Wrap = 0
bye:
End Sub

 

Valid HTML 4.01 Transitional Valid CSS
Yahoo! Search
Search the web Search this site
View content labeling at ICRA.