First time in VBA for Word, how to loop through document

T

Toxicdistortion

Windows XP Pro SP2
Word 2002 SP3

I am new to VBA for Word, but I've programmed extensively with VBA for
Excel, so I'm pretty familiar with the VBE and how VBA works in general,
it's just that Word presents an object model I'm not familiar with.
Anyway...

I have a 360 page Word document. There are five Section breaks in the
document (Insert/Break/Section Break Type/Next Page), and each section has
multiple Page Breaks (Insert/Break/Break types/Page break). I've been
looking at the Word VBA Help and I'm very interested in making use of the
Range or Section objects. As a training exercise, I'm trying to write a
Macro with a nested loop that will loop through each section, and within
each section loop through each "sub-section" (the page breaks within each
section). I guess I'm asking if the breaks that I've inserted in my
document are sufficient to be used as objects to loop through in my
document. Another thing about this document is that I've used the Outline
toolbar a lot to assign Headings to the various levels of my document. The
first line in each section is assigned an Outline level of 1, while the
first line in the sub-sections in each main section are assigned an Outline
level 2. Is this helpful at all for looping in a Macro?

I hope the above makes some sort of sense. Basically what I want to know is
1) what should I do to the document so that 2) I can access various parts of
it via macros.

Any and all help or suggestions appreciated. Thanks!!

-gk-

========================================================================
"The creative act is not the province of remote oracles or rarefied
geniuses but a transparent process that is open to everyone."
-Greg Kot in Wilco Learning How To Die-
 
D

Doug Robbins - Word MVP

Better to know really what it is that you want to do.

Quite often, it is best to your the Find function as in the following
example:

Dim i As Long, myrange As Range
i = 1
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="[0-9]{2}:[0-9]{2}:[0-9]{2}", _
MatchWildcards:=True, Wrap:=wdFindStop, Forward:=True) = True
Set myrange = Selection.Range
Selection.Collapse wdCollapseEnd
Selection.MoveRight wdCharacter, 1
myrange.Text = Left(myrange.Text, 6) & Format(i, "0#")
i = i + 1
Loop
End With

You can of course interate through the ranges of a document

Dim i as Long
With ActiveDocument
For i = 1 to .Sections.Count
Sections(i) 'Do something with Section(i)
Next i
End With

Pages are not so convenient to work with as to Word, pages may depend on the
active printer so you cannot alwasys be sure just what a page will contain.
However, you can do things with pages and the built in bookmark "\page" can
be very useful as in the following

' splitter Macro

' Macro created 16-08-98 by Doug Robbins to save each page of a document

' as a separate file with the name Page#.DOC

'

Dim Counter As Long, Source As Document, Target As Document

Set Source = ActiveDocument

Selection.HomeKey Unit:=wdStory

Pages = Source.BuiltInDocumentProperties(wdPropertyPages)

Counter = 0

While Counter < Pages

Counter = Counter + 1

DocName = "Page" & Format(Counter)

Source.Bookmarks("\Page").Range.Cut

Set Target = Documents.Add

Target.Range.Paste

Target.SaveAs FileName:=DocName

Target.Close

Wend





--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
T

Toxicdistortion

Doug Robbins - Word MVP said:
Better to know really what it is that you want to do.

How about this. I want to check the entire first section (Section(1)) for
the word "Chapter" that's a) bold, b) red, and c) 18 pt font. If found, I
want to change the font color to blue and italicize it.

-gk-
 
G

Greg Maxey

Sub ScratchMacro()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Sections(1).Range
With oRng.Find
.MatchCase = True
.Text = "Chapter"
With .Font
.Color = wdColorRed
.Bold = True
.Size = 18
End With
With .Replacement.Font
.Color = wdColorBlue
.Italic = True
End With
.Execute Replace:=wdReplaceAll
End With
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top