For each bit of document do ?

C

Cindy Meister

Hi Marc

In a general sort of way, you can

For each para in ActiveDocument paras
'Your code here
Next

To "walk" the paragraphs in a document "Story". Use
para.Range.Information(wdWithinTable) to determine whether you're in a
table.

Look up "StoryRanges" and all related/linked in the VBA Help if you need
to deal with anything besides the main body of the document (or look it
up anyway, to get an understanding of what it means).

-- Cindy

-----Original Message-----
From: Marc Hillman [mailto:[email protected]]
Posted At: 24 June 2006 15:35
Posted To: microsoft.public.word.vba.general
Conversation: For each bit of document do ?
Subject: Re: For each bit of document do ?


I'm trying to check that certain elements appear in a certain order, for
instance that every table is preceded or followed immediately by text of
style "Caption". I feel I should be able to iterate through something
and hit a table and then a paragraph (and check its style).

It causes me to wonder, if I wanted to render a word document in some
strange way (eg I wanted to write my own HTML exporter) how could you do
it?
I don't understand how I could find all the different pieces
sequentially and export them.

(Delete REMOVE from my e-mail to reply direct)
 
J

Jay Freedman

Hi Marc,

For the specific job you mentioned, checking the paragraphs around
tables for required styles, your macro will probably run faster if you
iterate through the Tables collection (assuming there are many fewer
tables than paragraphs in the average document). You can use a Range
object to look at the surrounding text. Here's a sample that does half
the job, looking only at the preceding paragraph. The other half is
"left as an exercise for the interested student." :)

Sub foo()
Dim oTbl As Table
Dim oRg As Range

For Each oTbl In ActiveDocument.Tables
Set oRg = oTbl.Range
With oRg
.Collapse wdCollapseStart
.Move unit:=wdParagraph, Count:=-1
If LCase(.Style) <> "caption" Then
' do something with oRg, for example...
.Paragraphs(1).Range.Select
MsgBox "This should have Caption style."
End If
End With
Next oTbl
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 

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