Home

How to Write Document Cleanup Macros in Word 2016

|
Updated:  
2016-11-16 3:53:22
|
Word 2010 For Dummies
Explore Book
Buy On Amazon
Before that final save, or any time you're working on a large document in Word 2016, consider doing some document cleanup. It's a process that involves searching for rogue characters and other problematic text.

A document cleanup routine involves looking for trailing spaces at the end of paragraphs, double spaces, double tabs, and double Enter keys (empty paragraphs). These are all items to be avoided, but they end up in long documents anyway.

The process of eliminating these unwanted elements involves using the Find and Replace dialog box. You need to use the Special button to input special characters, such as Space, Tab, and Enter.

The macro created to perform the document cleanup chore recorded the keystrokes used to search and replace for the various characters. Then the Visual Basic Editor was used to remove some of the redundant code. Here is the result:

Sub document_cleanup() ' ' document_cleanup Macro ' Remove trailing spaces and double spaces, tabs, and Enter keys ' Selection.HomeKey Unit:=wdStory Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting

' Remove trailing spaces from a paragraph With Selection.Find

.Text = "^w^v" .Replacement.Text = "^v" .Forward = True

End With

Selection.Find.Execute Replace:=wdReplaceAll ' Remove double spaces

With Selection.Find

>.Text = " " .Replacement.Text = " " End With

Selection.Find.Execute Replace:=wdReplaceAll

' Remove double tabs

With Selection.Find .Text = "^t^t"

.Replacement.Text = " ^t"

End With Selection.Find.Execute Replace:=wdReplaceAll

' Remove double Enter keys (blank paragraphs)

With Selection.Find .Text = "^v^v"

.Replacement.Text = "^v"

End With

Selection.Find.Execute Replace:=wdReplaceAll End Sub

The first search-and-replace operation removes trailing spaces. The search text is ^w^v, which looks for any white space (^w) characters before the Enter key (^v). These whitespace characters — space, tab, and so on — are replaced with the Enter key, which removes the trailing spaces.

The second search-and-replace removes double spaces. press the spacebar twice for the search text and pressed the spacebar a single time for the replacement text.

The third search-and-replace removes double tabs. The ^t represents tab characters in the Find and Replace dialog box.

The final search-and-replace removes empty paragraphs. The ^v characters represent the Enter key, so replacing ^v^v with ^v removes any empty paragraphs.

This macro works okay, but it could be better. For example, it doesn't handle triple spaces or triple tabs. You'd have to run the macro a second time for that. If you provide the programming talent, the macro's code can address those issues.

About This Article

This article is from the book: 

About the book author:

Dan Gookin has been writing about technology for 20 years. He has contributed articles to numerous high-tech magazines and written more than 90 books about personal computing technology, many of them accurate.
He combines his love of writing with his interest in technology to create books that are informative and entertaining, but not boring. Having sold more than 14 million titles translated into more than 30 languages, Dan can attest that his method of crafting computer tomes does seem to work.
Perhaps Dan’s most famous title is the original DOS For Dummies, published in 1991. It became the world’s fastest-selling computer book, at one time moving more copies per week than the New York Times number-one best seller (although, because it’s a reference book, it could not be listed on the NYT best seller list). That book spawned the entire line of For Dummies books, which remains a publishing phenomenon to this day.
Dan’s most recent titles include PCs For Dummies, 9th Edition; Buying a Computer For Dummies, 2005 Edition; Troubleshooting Your PC For Dummies; Dan Gookin’s Naked Windows XP; and Dan Gookin’s Naked Office. He publishes a free weekly computer newsletter, “Weekly Wambooli Salad,” and also maintains the vast and helpful Web site www.wambooli.com.