Learn Word VBA
VBA in general
See also:
- Learn VBA
- This page in German, French, Spanish, Portuguese
What are objects, properties, and methods?
Word VBA Conceptual
How do I refer to the active element (for example, paragraph, table, field)?
A common task when using Visual Basic is to specify an area in a document and then do something with it, such as insert text or apply formatting. A Range object refers to a contiguous area in a document.
Using Word VBA
Working with Document Objects
- Creating a new document
- Opening a document
- Saving an existing document
- Closing documents
- Activating a document
- Determining if a document is open
- Referring to the active document
Insert text in a document
Use the InsertBefore method or the InsertAfter method of the Selection object or the Range object to insert text before or after a selection or range of text. The following example inserts text at the end of the active document.
Edit text
- Determining whether text is selected
- Collapsing a selection or range
- Extending a selection or range
- Redefining a selection or range
- Changing text
Apply formatting to text
- Applying formatting to a range
- Inserting text and applying character and paragraph formatting
- Switching the space before a paragraph between 12 points and none
- Increasing the left margin by 0.5 inch
Modify a portion of a document
Modifying a group of document elements
Working with Range Objects
- Using the Range method
- Using the Range property
- Redefining a Range object
- Assign ranges - Set and Duplicate
Using Range objects is to be preferred over working with the Selection Object.
Find and replace text or formatting
Finding and replacing is exposed by the Find and Replacement objects. The Find object is available from the Selection object and the Range object.
Loop through a collection
There are several different ways you can loop on the elements of a collection. However, the recommended method for looping on a collection is to use the For Each...Next loop. In this structure, Visual Basic repeats a block of statements for each object in a collection. The following example displays the name of each document in the Documents collection.
Common problems
Requested Member of the Collection Does Not Exist
The "requested member of the collection does not exist" error occurs when you try to access an object that does not exist.
Object Doesn't Support this Property or Method
The "object doesn't support this property or method" error occurs when you try to use a method or property that the specified object does not support.
User Interaction
To interact with users, you can use the standard dialogs MsgBox and InputBox, Display built-in Word dialog boxes, or custom made dialogs or forms (synonyms: UserForms. MSForms).
Note: having Code VBA installed adds the following tools for user interaction: Userform Builder, SelectionBox and DatePicker
Display a custom dialog box
To display a dialog box from Visual Basic, use the Show method. The following example displays the dialog box named UserForm1.
Initialize control properties
You can initialize controls at run time by using Visual Basic code in a macro. For example, you could fill a list box, set text values, or set option buttons.
Control and dialog box events
After you have added controls to your dialog box or document, you add event procedures to determine how the controls respond to user actions.
Working with tables
- Creating a table, inserting text, and applying formatting
- Inserting text into a table cell
- Returning text from a table cell without returning the end of cell marker
- Converting existing text to a table
- Returning the contents of each table cell
- Copying all tables in the active document into a new document
- Error accessing a table row or column
Miscellaneous tasks
- Changing the view
- Setting options
- Changing the document layout
- Looping through paragraphs in a document
Select text in a document
Auto macros
Automate common Word tasks
Communicate with other applications
Macro Recorder
- Find out which property or method to use
- Recording a Macro to Generate Code
- Revise recorded Visual Basic macros
Specialist features
- Document Creation using Microsoft Access
- Using the UndoRecord Object
- Working with building blocks
- Working with Content Controls
- Using Charts
- Bibliographies
Frequently asked Visual Basic questions
Word VBA object model class reference
Other supported languages: Spanish, German, Portuguese and French.