• About
  • Disclaimer
  • Privacy Policy
  • Contact Us
Wednesday, September 27, 2023
Tech Fashion Web
  • Home
  • Fashion
    • Footwear
    • Culture
  • Technology
    • Tech Solution
    • Website Design
    • Cyber Security
    • Software
  • Business
  • Digital Marketing
    • SEO
    • Social Media
  • Travel
  • Entertainment
    • Music
    • Celebrity
  • Health
    • Food
  • lifestyle
    • Home
  • More
    • Gaming
    • Gadgets
    • Education
    • Electronics
    • Gadgets
    • Reviews
    • Law
No Result
View All Result
  • Home
  • Fashion
    • Footwear
    • Culture
  • Technology
    • Tech Solution
    • Website Design
    • Cyber Security
    • Software
  • Business
  • Digital Marketing
    • SEO
    • Social Media
  • Travel
  • Entertainment
    • Music
    • Celebrity
  • Health
    • Food
  • lifestyle
    • Home
  • More
    • Gaming
    • Gadgets
    • Education
    • Electronics
    • Gadgets
    • Reviews
    • Law
No Result
View All Result
Tech Fashion web
No Result
View All Result
How to use VBA to reach the beginning and end of a Microsoft Word document

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

by Tech Fashion
May 19, 2022
in Technology
0
325
SHARES
2.5k
VIEWS
Share on FacebookShare on Twitter


Image: Sai/Adobe Stock

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

Must-Read Windows Coverage

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

WordVBA_A
Image: Susan Harkins/TechRepublic. The VBA procedures will add some content to the beginning and end of this Word document.

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:

  1. Click the Developer tab.
  2. In the Code group, choose Macros.
  3. In the resulting dialog box, choose GoToStart (Figure B†
  4. 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

WordVBA_B
Image: Susan Harkins/TechRepublic. Choose the method.

Figure C

WordVBA_C
Image: Susan Harkins/TechRepublic. GoToStart() added a sentence to the beginning of the Word document.

Figure D

WordVBA_D
Image: Susan Harkins/TechRepublic. GoToEnd() added a sentence to the end of the Word document.

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.



Source link

Share130Tweet81Share33
Previous Post

Fortinet vs Palo Alto | EDR Software Comparison

Next Post

2 ways to display negative numbers in red in Microsoft Excel

Tech Fashion

Tech Fashion

Related Posts

Which tool is best for your business?
Technology

Which tool is best for your business?

by Tech Fashion
March 24, 2023
LG Will Spend $5.5 Billion on a Battery Factory in Arizona
Technology

LG Will Spend $5.5 Billion on a Battery Factory in Arizona

by Tech Fashion
March 24, 2023
Intel Announces New vPro Platform Running on 13th Generation Core
Technology

Intel Announces New vPro Platform Running on 13th Generation Core

by Tech Fashion
March 24, 2023
Beat the roaming charges with this eSIM
Technology

Beat the roaming charges with this eSIM

by Tech Fashion
March 23, 2023
Next Post
2 ways to display negative numbers in red in Microsoft Excel

2 ways to display negative numbers in red in Microsoft Excel

WhatsApp Adds Commercial Services as Meta Seeks More Revenue

WhatsApp Adds Commercial Services as Meta Seeks More Revenue

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

No Result
View All Result

Categories

  • Business (2)
  • Celebrity (10)
  • Culture (8)
  • Education (8)
  • Entertainment (3)
  • Fashion (12)
  • Food (7)
  • Footwear (7)
  • Health (6)
  • Lifestyle (14)
  • Music (6)
  • Social Media (2)
  • Software (4)
  • Tech Solution (1)
  • Technology (1,884)
  • Travel (12)
  • Website Design (2)

Recent.

Which tool is best for your business?

Which tool is best for your business?

March 24, 2023
LG Will Spend $5.5 Billion on a Battery Factory in Arizona

LG Will Spend $5.5 Billion on a Battery Factory in Arizona

March 24, 2023
Intel Announces New vPro Platform Running on 13th Generation Core

Intel Announces New vPro Platform Running on 13th Generation Core

March 24, 2023
Tech Fashion Web

We bring you the best Premium WordPress Themes that perfect for news, magazine, personal blog, etc. Check our landing page for details.

Category

  • Business
  • Celebrity
  • Culture
  • Education
  • Entertainment
  • Fashion
  • Food
  • Footwear
  • Health
  • Lifestyle
  • Music
  • Social Media
  • Software
  • Tech Solution
  • Technology
  • Travel
  • Website Design

Recent Posts

  • Which tool is best for your business? March 24, 2023
  • LG Will Spend $5.5 Billion on a Battery Factory in Arizona March 24, 2023
  • Intel Announces New vPro Platform Running on 13th Generation Core March 24, 2023

Contact Us

    © 2021 techfashionweb.com . All rights reserved.

    No Result
    View All Result
    • Home
    • Fashion
      • Footwear
      • Culture
    • Technology
      • Tech Solution
      • Website Design
      • Cyber Security
      • Software
    • Business
    • Digital Marketing
      • SEO
      • Social Media
    • Travel
    • Entertainment
      • Music
      • Celebrity
    • Health
      • Food
    • lifestyle
      • Home
    • More
      • Gaming
      • Gadgets
      • Education
      • Electronics
      • Gadgets
      • Reviews
      • Law

    © 2021 techfashionweb.com . All rights reserved.