Visual Basic for Applications is the language behind the Office apps that help you automate tasks. But using VBA in Word has its challenges. The first is moving around in a document because of all the different types of content: text, images, tables and so on. In this tutorial, I’ll show you two easy VBA procedures to navigate. One identifies the beginning of a Word document and the other identifies the end of the document.
I’m using Microsoft 365 on a Windows 10 64-bit system, but you can use an earlier version. Word for the web does not support VBA.
SEE: Google Workspace versus Microsoft 365: a side-by-side analysis with checklist (Tech Republic Premium)
About ActiveDocument from Microsoft Word VBA
The main VBA component that we will rely on in both procedures is the ActiveDocument property, which returns a Document object that represents the active document. In other words, this will open the document as a whole. You don’t need this for every VBA procedure in Word, but you do need it if you need to refer to “parts” of the document.
If no Word document is open, this property returns an error. If you are using this property from within Excel or PowerPoint, you must include error handling for this situation.
This property uses the following form:
expression.ActiveDocument
true expression
is a variable that represents the Application object (Word). Fortunately, this is implicitly implied in Word.
The next piece of VBA we will use is the Range method, which uses the form
expression.Range(start, end)
to return a Range object. We use this method to identify an area of the document.
There is no method that says “go to the beginning” or “go to the end”, so we will use the start and end arguments to identify both. Both arguments are optional, and as you might guess, start
identifies the position of the first character and end
identifies the last character position.
Now let’s move on to the actual VBA procedures.
Using VBA to find the beginning of a Word document
Moving the cursor to the beginning of a Word document with VBA is amazingly easy. Advertisement A shows the procedure. First, the procedure declares and defines a scope object, using zeros to identify the beginning of the document. Two zeros places the cursor in front of content in the active document. The second line adds a little text and the vbNewLine
constant adds a newline.
Advertisement A
Sub GoToStart()
'Move cursor to the beginning of the active document.
'Add a short text phrase to show it worked.
Dim rBegin As Range
Set rBegin = ActiveDocument.Range(0, 0)
rBegin.Text = "This is the beginning of this document." & vbNewLine
End Sub
Now let’s look at the second procedure that moves the cursor to the end of a document.
Using VBA to find the end of a Word document
As mentioned before, there is no method that says “go to the end”, so we have to force the problem by finding the last character. It’s a little more complicated, but not difficult, as can be seen in Advertisement B† This procedure works similar to the first, but enters the text at the end of the document, which is found by using the Last property of the Characters object.
This time we have the vbNewLine
constant for the text.
Advertisement B
Sub GoToEnd()
'Move cursor to the end of the active document.
'Add a short text phrase to show it worked.
Dim rEnd As Range
Set rEnd = ActiveDocument.Range.Characters.Last
rEnd.Text = vbNewLine & "This is the end of this document."
End Sub
How to enter and run the VBA procedures in a Word document?
Now it’s time to introduce both procedures for use in a Word document. I am using a short two page Word document shown in Image A, with a variety of content saved as a macro-enabled Word file. If you are using a ribbon version, you need to save the Word document as a macro file to run VBA. If you are working in the menu version, you can skip this step.
Image A
To enter the VBA procedures, press Alt + F11 to open the Visual Basic Editor (VBE). Select This Document in the Project Explorer on the left. You can enter the code manually or import the downloadable .cls file. In addition, the procedure is contained in the downloadable .docm and .doc files. If you enter the code manually, do not paste it from this web page. Instead, copy the code into a text editor and then paste that code into the ThisDocument module. Doing this will remove any ghost web characters that might otherwise cause errors.
Now let’s run the procedures as follows:
- Click the Developer tab.
- In the Code group, choose Macros.
- In the resulting dialog box, choose GoToStart (Figure B†
- Click Run.
As you can see in Figure C, the procedure inserts text and a line break at the beginning of the Word document. Now let’s repeat this process, but choose GoToEnd (Figure B† This procedure inserts a line break and a bit of text at the end of the Word document, as shown in Figure D†
Figure B
Figure C
Figure D
One thing you may notice is that both insert sentences use different formats. These sentences, like all others, will use the existing format. This is worth remembering if you want inserted text to use a specific format instead.
Most likely, you don’t want to go through all those steps to run this macro. I recommend adding it to the Quick Access Toolbar or a custom group. If you need help with that, read Add Office macros to the QAT toolbar for quick access†
Stay tuned
Both VBA procedures are simple – both the procedures themselves and the tasks they complete. Its purpose is to show you how to navigate to both places with VBA. In the coming months I will devote a few articles to more navigational VBA in Microsoft Word documents.