Please Note: This article is written for users of the following Microsoft Word versions: 97, 2000, 2002, and 2003. If you are using a later version (Word 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Word, click here: Accessing Paragraphs in a Macro.

Accessing Paragraphs in a Macro

by Allen Wyatt
(last updated October 6, 2012)

6

One of the nifty things about programming VBA macros is that the language is object-oriented. This means that you can access every part of your document using objects and collections of objects. In other words, you can manipulate paragraphs without ever needing to select them.

For instance, let's say you wanted to access each paragraph of a document, in turn, and do some processing on the text in that paragraph. Since each paragraph is a distinct object in the document, this is relatively easy. All of the paragraph objects are accessible as part of the Paragraphs collection. The following code will do the trick:

iParCount = ActiveDocument.Paragraphs.Count
For J = 1 To iParCount
    sMyPar = ActiveDocument.Paragraphs(J).Range.Text
    [Add processing comments to manipulate sMyPar]
    ActiveDocument.Paragraphs(J).Range.Text = sMyPar
Next J

The first line of the code sets iParCount equal to the number of paragraphs in the current document. The loop starting in the second line then does the main work in the macro. The third line set the sMyPar string equal to the text within the specified paragraph. (When J is equal to 1, you are working with the first paragraph. When J is equal to 2, it is the second paragraph—and so on.)

After the processing of sMyPar is complete, then the next line sets the document text equal to the modified text in the sMyPar string.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (823) applies to Microsoft Word 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Word (Word 2007 and later) here: Accessing Paragraphs in a Macro.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Searching for Floating Graphics

Graphics can be added to a document so that they are either inline with the text or floating over the text. You can use ...

Discover More

Unique Date Displays

Need to print an elapsed date in a strange format? It's easier to do than may appear at first glance. Here's a discussion on ...

Discover More

Adding Circles around Letters or Numbers

Want to add some handy circles around text in your document? Believe it or not, Word provides three ways you can accomplish ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More WordTips (menu)

Counting Characters in a Selection with VBA

Need to figure out the number of characters in a range of selected text? Here's how to do it in VBA.

Discover More

Determining if Num Lock is On

Need to know if the Num Lock key is on or off? You can use a short bit of macro code to figure out the state of the key.

Discover More

Selecting a Bookmark in a Macro

Bookmarks can be very handy in a document. Word provides a VBA command you can use to easily select any of those bookmarks.

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 2 + 1?

2017-09-06 09:13:39

Allen

Steve: It actually depends on how you define "object-oriented." This is a debate that has been raging for quite some time. The VBA language has been around for 20 years, and when it was released it met was was (at the time) the full definition of being object oriented. In the last 20 years the mainstream OOP world has progressed quite a bit, but VBA has not progressed nearly as much. This is especially true when it comes to one of the fundamental aspects of OOP--inheritance, which VBA does not support.

Even with that acknowledgement, the general consensus I've seen is that it is more OOP than not and is, thus, considered an object-oriented programming language. There's a great discussion on this which you might find interesting over on the StackOverflow site.

-Allen


2017-09-06 04:48:54

Steve Vink

Just a point on your terminology. The VBA language is not object-oriented. It uses an Object Model, but this is a very different thing. Object-Oriented languages would, as a simple example, allow you to take a class such as "Paragraph" and extend it with additional properties of your own, or implement additional events that it could respond to.


2017-05-08 06:25:43

Ashgaroth

Doesn't work.


2016-06-04 02:31:22

Vimarsh

website is really useful to beginner


2016-03-17 08:23:30

Joel

I have hundreds of pages of a technical document with lots of tables in them. Reoccurring throughout is a five row/two column table that has a label in the first row that always begins with "Process diagram" . I am looking for code that will go through the doc and delete each of these tables. Can anyone help?


2015-05-14 07:22:54

Qasim

Really Helpfull


This Site

Got a version of Word that uses the menu interface (Word 97, Word 2000, Word 2002, or Word 2003)? This site is for you! If you use a later version of Word, visit our WordTips site focusing on the ribbon interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.