O'Reilly logo

Writing Word Macros, Second Edition by Steven Roman PhD

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Example: Looking at Lists

By way of example, let me share with you an issue I had to deal with recently concerning the 450-page manuscript I mentioned earlier. After sending the manuscript to the publisher, the editor requested that I add a half-line spacing between each bulleted item to improve readability. This was easy enough to do—I just changed the List Bullet style that I used to create bulleted lists. However, having worked with computers for some time, I knew enough to check each list visually to make sure everything came out as expected.

Now, it is easy to iterate through each list paragraph in the document. The following code will do the job:

	Dim para As Paragraph
	For Each para in ActiveDocument.ListParagraphs
	   para.Range.Select
	Next para

The problem here is that the list paragraphs will go flying by without allowing me to check each paragraph.

The solution is to write a macro that uses a static variable that remembers the index of the current list paragraph. (Recall that a static variable retains its value as long as the document to which the code is attached is open or as long as there is an open document that uses the template to which the code is attached.) Then, when the macro is executed repeatedly, it goes from one list to the next. By assigning a hotkey or a toolbar button to this macro, it is easy to check each list paragraph. The code is shown in Example 17-2.

Example 17-2. Iterating List Paragraphs

Sub FindNextList() Dim i As Integer Static Idx As Integer ' Idx is ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required