How to use VBA to reach the beginning and end of a Microsoft Word document

1 month ago 16
PR Distribution
word-vba-tutorial-susanImage: Sai/Adobe Stock

Visual Basic for Applications is the connection down the Office apps that allows you to automate tasks. But utilizing VBA successful Word has its challenges. The archetypal is moving astir successful a papers due to the fact that of each the antithetic types of content: Text, pictures, tables and truthful on. In this tutorial, I’ll amusement you 2 elemental VBA procedures for navigating. One identifies the opening of a Word document, and the different identifies the document’s end.

I’m utilizing Microsoft 365 connected a Windows 10 64-bit system, but you tin usage an earlier version. You whitethorn entree the demo files here. Word for the web doesn’t enactment VBA.

SEE: Google Workspace vs. Microsoft 365: A side-by-side investigation w/checklist (TechRepublic Premium)

About Microsoft Word VBA’s ActiveDocument

The main VBA constituent that we’ll trust connected successful some procedures is the ActiveDocument property, which returns a Document entity that represents the progressive document. In different words, this opens the document, arsenic a whole. You won’t request this for each VBA process successful Word, but you volition request it erstwhile you request to notation “parts” of the document.

If nary Word papers is open, this spot returns an error. If you’re utilizing this spot from wrong Excel oregon PowerPoint, you should see error-handling for this situation.

This spot uses the pursuing form:


where look is simply a adaptable that represents the Application entity (Word). Fortunately, successful Word, this is implicitly implied.

The adjacent portion of VBA we’ll usage is the Range method, which uses the form

expression.Range(start, end)

to instrumentality a Range object. We’ll usage this method to place an country of the document.

There’s nary method that says “go to the beginning,” oregon “go to the end,” truthful we’ll usage the commencement and extremity arguments to place both. Both arguments are optional, and arsenic you mightiness suspect, commencement identifies the archetypal quality presumption and extremity identifies the past quality position.

Now, let’s determination connected to the existent VBA procedures.

How to usage VBA to find the opening of a Word document

Moving the cursor to the opening of a Word papers utilizing VBA is amazingly simple. Listing A shows the procedure. First, the process declares and defines a scope object, utilizing 0s to place the opening of the document. Two 0s volition spot the cursor earlier immoderate contented successful the progressive document. The 2nd enactment adds a spot of substance and the vbNewLine changeless adds a caller line.

Listing A

Sub GoToStart()

'Move cursor to the opening of the progressive document.

'Add a abbreviated substance operation to amusement it worked.

Dim rBegin As Range

Set rBegin = ActiveDocument.Range(0, 0)

rBegin.Text = "This is the opening of this document." & vbNewLine

End Sub

Now let’s look astatine the 2nd process that moves the cursor to the extremity of a document.

How to usage VBA to find the extremity of a Word document

As mentioned, determination is nary method that says “go to the end,” truthful we request to unit the contented by uncovering the past character. It is simply a spot much complex, but not difficult, arsenic shown successful Listing B. This process works akin to the first, but it enters the substance astatine the extremity of the document, which it finds by utilizing the Last spot of the Characters object.

This clip we positioned the vbNewLine changeless earlier the text.

Listing B

Sub GoToEnd()

'Move cursor to the extremity of the progressive document.

'Add a abbreviated substance operation to amusement it worked.

Dim rEnd As Range

Set rEnd = ActiveDocument.Range.Characters.Last

rEnd.Text = vbNewLine & "This is the extremity of this document."

End Sub

How to participate and tally the VBA procedures into a Word document

Now it’s clip to participate some procedures for usage successful a Word document. I’m utilizing a abbreviated two-page Word document, shown successful Figure A, with a assortment of contented saved arsenic a macro-enabled Word file. If you are utilizing a ribbon version, you indispensable prevention the Word papers arsenic a macro-enabled record to tally VBA. If you’re moving successful the paper version, you tin skip this step.

Figure A

WordVBA_AImage: Susan Harkins/TechRepublic. The VBA procedures volition adhd a spot of contented to the opening and the extremity of this Word document.

To participate the VBA procedures, property Alt + F11 to unfastened the Visual Basic Editor (VBE). In the Project Explorer to the left, prime ThisDocument. You tin participate the codification manually oregon import the downloadable .cls file. In addition, the process is successful the downloadable .docm and .doc files. If you participate the codification manually, don’t paste from this web page. Instead, transcript the codification into a substance exertion and past paste that codification into the ThisDocument module. Doing truthful volition region immoderate phantom web characters that mightiness different origin errors.

Now let’s tally the procedures arsenic follows:

  1. Click the Developer tab.
  2. Choose Macros successful the Code group.
  3. In the resulting dialog, take GoToStart (Figure B).
  4. Click Run.

As you tin spot successful Figure C, the process inserts substance and a enactment interruption astatine the opening of the Word document. Now let’s repetition this process but take GoToEnd (Figure B). This process inserts a enactment interruption and a spot of substance to the extremity of the Word document, arsenic you tin spot successful Figure D.

Figure B

WordVBA_BImage: Susan Harkins/TechRepublic. Choose the procedure.

Figure C

WordVBA_CImage: Susan Harkins/TechRepublic. GoToStart() added a condemnation to the opening of the Word document.

Figure D

WordVBA_DImage: Susan Harkins/TechRepublic. GoToEnd() added a condemnation to the extremity of the Word document.

One happening you mightiness announcement is that some of the inserted sentences usage antithetic formats. These sentences, similar immoderate other, volition usage the format successful place. This is worthy remembering if you privation inserted substance to usage a circumstantial format instead.

Most likely, you won’t privation to enactment done each those steps to tally this macro. I urge that you adhd it to the Quick Access Toolbar oregon a customized group. If you request assistance with that, read How to adhd Office macros to the QAT toolbar for speedy access.

Stay tuned

Both VBA procedures are elemental — some the procedures themselves and the tasks they complete. The extremity is to amusement you however to navigate to some spots utilizing VBA. Over the adjacent fewer months, I’ll give a fewer articles to much navigational VBA successful Microsoft Word documents.

Read Entire Article