Word VBA Tutorial
Word VBA Tutorial
DOCUMENTS
• ACTIVEDOCUMENT
• THISDOCUMENT
• DOCUMENT VARIABLES
• DOCUMENT METHODS
• OPEN DOCUMENT
• CREATE NEW DOCUMENT
• SAVE DOCUMENT
• CLOSE DOCUMENT
• PRINT DOCUMENT
• SELECTION
• MOVE SELECTION
• PARAGRAPHS
• WORD VBA TUTORIAL CONCLUSION
Selection.ParagraphFormat.LeftIndent = Selection.InsertBreak
Left Indent Insert Page Break
Description VBA Code InchesToPoints(3.75) Type:=wdPageBreak
AutoMacro:
VBA Add-in with Hundreds of Ready-To-Use Code Examples, Learn More
Code Generators, and much more!
Word VBA Macro Tutorial
This is a tutorial for using VBA with Microsoft Word. This tutorial will teach you how to write a
simple Macro and interact with Documents, Ranges, Selections, and Paragraphs.
Note: If you’re brand new to Macros / VBA you might also find this article useful: How to write
VBA Macros from Scratch.
VBA is the programming language used to automate Microsoft Office programs including Word,
Excel, Outlook, PowerPoint, and Access.
When you Record a Macro, Word will write VBA code into a Macro, allowing you to repeat your
actions. You can see a list of all available Macros from View > Macros.
After recording a Macro, you will be able to edit the Macro from the Macro List:
AutoMacro:
VBA Add-in with Hundreds of Ready-To-Use Code Examples, Learn More
Code Generators, and much more!
When you click Edit, you open the VBA Editor. Using the VBA Editor you can edit recorded
Macros or write a Word Macro from scratch. To access the VBA Editor use the shortcut ALT + F11
or click Visual Basic from the Developer Ribbon.
Sub WordMacroExample()
‘Write To Doc
Selection.TypeText “www.automateexcel.com”
Selection.TypeParagraph
End Sub
AutoMacro:
VBA Add-in with Hundreds of Ready-To-Use Code Examples, Learn More
Code Generators, and much more!
Word Document Object
When interacting with Microsoft Word in VBA, you will frequently reference Word “Objects”. The
most common objects are:
Application
Application is the “top-level” object. All other objects in Word can be reached through it.
In addition to accessing other Word objects, there are “application-level” settings that can be
applied:
Application.Options.AllowDragAndDrop = True
Application.Windows(1).Selection.Characters.Count
However, the most common Word objects can be accessed directly, without typing the full
hierarchy. So instead, you can (and should) just type:
AutoMacro:
VBA Add-in with Hundreds of Ready-To-Use Code Examples, Learn More
Code Generators, and much more!
Documents
ActiveDocument
Often, you will have two or more documents opened in Word and you will need specify which
specific Word Document to interact with. One way to specify which document is to use
ActiveDocument. For example:
ActiveDocument.PrintOut
…would print the ActiveDocument. The ActiveDocument is the document in Word which “has
focus”
Documents(“Example.docx”).Activate
ThisDocument
Instead of using ActiveDocument to reference the active document, you can use ThisDocument to
reference the document where the macro is stored. ThisDocument will never change.
ThisDocument.PrintOut
Document Variables
However, for more complicated macros, it can be hard to keep track of the Active Document. It
can also be frustrating to switch back and forth between documents.
This macro will assign the ActiveDocument to a variable and then print the document using the
variable:
Sub VarExample()
Dim oDoc As Document
Set oDoc = ActiveDocument
oDoc.PrintOut
End Sub
AutoMacro:
VBA Add-in with Hundreds of Ready-To-Use Code Examples, Learn More
Code Generators, and much more!
Document Methods
Open Document
To Open a Word Document:
Documents.Add
As always, it is useful and huge problem saver to assign document to variable upon creating or
opening:
Save Document
To save a document:
ActiveDocument.Save
or SaveAs:
AutoMacro:
VBA Add-in with Hundreds of Ready-To-Use Code Examples, Learn More
Code Generators, and much more!
Close Document
To close a Document and save changes:
ActiveDocument.Close wdSaveChanges
ActiveDocument.Close wdDoNotSaveChanges
Print Document
This will print the active Document:
ActiveDocument.PrintOut
Range refers to some portion of document, usually, but not necessarily, text.
Selection refers to selected text (or other object like pictures) or, if nothing is selected, an
insertion point.
Paragraphs represent paragraphs in document. Its less important than it sounds, because you
can’t directly access paragraph text (you need to access particular paragraph range to make
modifications).
Range
Range can be any part of document, including entire document:
AutoMacro:
VBA Add-in with Hundreds of Ready-To-Use Code Examples, Learn More
Code Generators, and much more!
Usually, you would want to get range which refers to specific part of document and then modify it.
In the following example we will make the first word of second paragraph bold:
(Tip: Note the space after “Hello”. Because word object includes space after word, with just “hello”
we would get “Hellonext word”)
There are hundreds of things which you can do with ranges. Just a few examples (these assume
you are already made object variable oRange referring to range of interest):
Change font
oRange.Font.Name = “Arial”
MsgBox oRange.Characters.Count
ActiveDocument.Footnotes.Add Range:=oRange, _
Text:=”Read more at automateexcel.com.”
Copy it to clipboard
oRange.Copy
Often you need to change to what is particular range referring. So you can start it’s
start and end
oRange.Start = 5
oRange.End = 50
AutoMacro:
VBA Add-in with Hundreds of Ready-To-Use Code Examples, Learn More
Code Generators, and much more!
After above code, oRange would refer to text starting with fifth and ending with 50th character in
document.
Selection
Selection is even more widely used than Range, because it is easier to work with Selections than
Ranges, IF your macro ONLY interacts with the ActiveDocument.
First select the desired part of your document. For example select the second paragraph in active
document:
ActiveDocument.Paragraphs(2).Range.Select
Then you can use the Selection Object to type some text:
Often, it’s necessary to know if some text is selected or we have just a insertion point:
When working with Selection object we want to place insertion point to particular place, and
issue commands starting from this point.
Beginning of document:
The Extend parameter wdMove moves the insertion point. Instead, you could use wdExtend
which will select all text between the current insertion point.
AutoMacro:
VBA Add-in with Hundreds of Ready-To-Use Code Examples, Learn More
Code Generators, and much more!
Move Selection
The most useful method for changing position of insertion point is Move. To move Selection two
characters forward:
Unit parameter can be wdCharacter, wdWord, wdLine, or more (use Word VBA help to see others).
Selection is easier to work with (compared to ranges) because it is like a robot using Word,
mimicking human user. Where Insertion point is – some action would take place. But, this means
that you must take care where insertion point is! This is not easy after many steps in code.
Otherwise, Word would change text in not desired place.
In the case you need some property or method not available in Selection object you can always
easily obtain range associated with selection:
TIP: Using Selection is often easier than using ranges, but also it’s way slower (important when you deal
with big documents)
Paragraphs
You can’t directly use Paragraphs object to change text:
Above wouldn’t work (actually it will throw an error). You need to first obtain range associated
with particular paragraph:
ActiveDocument.Paragraphs(1).Style = “Normal”
AutoMacro:
VBA Add-in with Hundreds of Ready-To-Use Code Examples, Learn More
Code Generators, and much more!
or change its paragraph level formatting:
ActiveDocument.Paragraphs(1).LeftIndent = 10
or maybe you want to keep this paragraph on the same line with next paragraph:
ActiveDocument.Paragraphs(1).KeepWithNext = True
ActiveDocument.Paragraphs(1).Alignment = wdAlignParagraphCenter
Here is an example where we insert a paragraph above the first paragraph, but we can still
reference the old first paragraph because it was assigned to a variable:
Sub ParagraphExample()
Dim oPara As Paragraph
Set oPara = ActiveDocument.Paragraphs(1)
MsgBox oPara.Range.Text
oPara.Range.InsertParagraphBefore ‘Insert Paragraph
MsgBox oPara.Range.Text
End Sub
Sub LoopThroughParagraphs()
End Sub
AutoMacro:
VBA Add-in with Hundreds of Ready-To-Use Code Examples, Learn More
Code Generators, and much more!
Word VBA Tutorial Conclusion
This tutorial covered the basics of Word VBA. If you’re new to VBA, you should also review our
general VBA Tutorial to learn more about Variables, Loops, MessageBoxes, Settings, Conditional
Logic and much more.
Templates
TextBoxes
SaveAs PDF
Bookmarks
Tables
Open Documents
AutoMacro:
VBA Add-in with Hundreds of Ready-To-Use Code Examples, Learn More
Code Generators, and much more!