Learn Word VBA

VBA in general

See also:

What are objects, properties, and methods?

Word VBA Conceptual

How do I refer to the active element (for example, paragraph, table, field)?

What is a Range object?

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

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

Apply formatting to text

Modify a portion of a document

Modifying a group of document elements

Working with Range Objects

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

Miscellaneous tasks

Select text in a document

Auto macros

Automate common Word tasks

Communicate with other applications

Macro Recorder

Specialist features

Frequently asked Visual Basic questions

Word VBA object model class reference

Other supported languages: Spanish, German, Portuguese and French.