VBA
Resources For MS Word
Christopher Rath
2013-04-04
MS Word
These are MS Word-specific VBA tips:
- Compress empty lines—this MS Word snippet does a search and
replace, exchanging two consecutive end of paragraph markers with a
single end of paragraph mark; thus removing blank lines.
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
- Convert DeltaView to Tracked
Changes—Upon occasion, I am sent an MS Word document showing the
delta between two documents as produced by DeltaView (or Compare-Pro,
one of its competitors) and I need to accept/reject the edits. To
simplify that process, I use this macro to convert the document into
standard MS Word tracked changes. This code is offered as a
Word 2007 add-in.
- ExtendShading—Often when constructing tables in MS Word, one wants to add shading
to various rows of the table. Especially when a document has been
circulated to various contributors as part of its construction,
document formatting (including table shading) is lost. To make it
easier to get a table's shading reapplied, this macro allows the user
to mark up a single column and have that column's shading extended
across the other columns. Download this macro as an MS Word Add-In (with a toolbar icon).
- Move comments to text—When working collaboratively with MS Word
documents, other contributors sometimes insert MS Word comments into the
document. Since these comments are not managed by MS Word
tracked-changes, it is not possible to monitor change to those comments.
As a result, I always move those comments out into the text of the
document (placing them inside square brackets). This
MS Word Add-In finds each
embedded comment and moves it into the text of the document: for
Word 2003 and earlier; or,
for MS Word 2007 and later.
- Remove trailing whitespace—this snippet uses search and
replace to remove any whitespace from the end of each paragraph in a
document.
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
- Retrieve Values From MS Word
Tables—VBA provides limited ability to store and retrieve values.
While the registry can be used for this purpose, it is not easily
accessible by naive users. The purpose of these routines is to allow a
user to type values in normal MS Word tables, where those values are
later retrieved and used by VBA. It is even possible to retrieve values from MS Word template files
(located in the users Startup folder); allowing a relatively straightforward
method for users to configure VBA distributed via a .dot file located in the
Startup folder; which is what this code does.
- StatusBar freeze-ups—When VBA
code is making frequent and voluminous updates to an MS Word document,
and the code is also posting messages to the StatusBar, the StatusBar
will eventually stop updating. This page describes how to work
around that problem.
- Display Table
Information—When processing MS Word tables using VBA, one
immediately encounters situation wherein VBA utterly refuses to
perform simple tasks one is easily able to perform through
MS Word's menus. A common culprit in such situations is a subset
of cells that have a different width than the other cells in
that cell's column. To assist in diagnosis of such situations, I
wrote this Table Info.dot
Add-In.
- Multireplace—replace a string in multiple files. This
Add-In will prompt you for strings and a folder, find all of the
MS Word files in that folder and its subfolders, and then open
each MS Word document and perform and Find-Replace of those
strings in each file.
- VBacs (Visual Basic Emacs)—make
MS Word work more like Emacs.
Other Resources
See the page that linked to this one for other
VBA resources I have found helpful.
©Copyright 2005–2013, Christopher Rath
Telephone: 613-824-4584
Address: 1371 Major Rd., Ottawa, ON, Canada K1E 1H3
Last updated:
2013/04/04 @ 16:43:21 (
)