looping from a given point in a document

L

Lighthouse

I need to go to a certain point in a document, marked by a style. Then
I want to set up a "while" loop (I think) to exchange all Heading 1's
*after* that point for another style. Then I need to perform a similar
exchange for all Heading 2's, then Heading 3's from the same point.
I'm not sure of the most efficient way to do this.

The exchange will always start from the same point for all Headings and
go to the end of the document. I don't want to do anything to the doc
before that starting point. I'm hoping someone can point me in the
right direction for this. TIA
 
D

Dave Lett

Hi Lighthouse,

The important thing here is 1) go to the starting style and 2) remember to
set the .wrap property to wdFindStop. The following works in my test
environment:

With Selection
.HomeKey Unit:=wdStory
With .Find
.ClearFormatting
.Text = ""
.Style = "body text"
.Replacement.Text = ""
.Execute
End With
.MoveRight Unit:=wdCharacter, Count:=1
With .Find
.Style = "Heading 1"
.Wrap = wdFindStop
.Replacement.Style = "Body Text"
.Execute Replace:=wdReplaceAll

.Style = "Heading 2"
.Wrap = wdFindStop
.Replacement.Style = "Body Text"
.Execute Replace:=wdReplaceAll

.Style = "Heading 3"
.Wrap = wdFindStop
.Replacement.Style = "Body Text"
.Execute Replace:=wdReplaceAll
End With
End With

HTH
 
L

Lighthouse

Thanks Dave, I'll look that over. Since I made my original post I've
been looking over "Range" stuff. I wonder if that's what I need to use.
in your code, i'm not sure what "wdStory" is. i looked it up in VBA
help but that wasn't much [help]

It looks like the first part of the code (up to the first "end with")
is finding my magic point in the document. Let's call it "styleX"
instead of "body text". Here's some more info about that: there may be
multiple occurences of "styleX" but I am only concerned w/ finding the
FIRST occurence and performing my style replacement between that point
and the end of the document.

I"m not sure I understand how the "With .Find" thing works. Again,
"with" doesn't show up in my VB help file. Sorry, I'm kind of dense
with VB.
 
D

Dave Lett

Hi Lighthouse,
You can use the Range object, but the routine I wrote (using the Selection
object) will work just fine. wdStory is a constant. Basically, anything in
Word VBA that starts with "wd" is a constant.If you want to know what a
constant does, then look in the help not for the name of the constant, but
the name of the method or property associated with the constant. In this
case, you would look for "HomeKey". In short
With Selection
.HomeKey Unit:=wdStory

sends the cursor to the beginning of the document. If you're only interested
in finding the first occurrence of "styleX", then send the cursor to the
beginning of the document (shown above) and then search for the style from
there (shown below). You can find "With Statement" in the VBA help file.
Take the following example:

With Selection
With .Find
.ClearFormatting
.Text = ""
.Style = "body text"
.Replacement.Text = ""
.Execute
End With
End With

Pretty easy to read, isn't it? Well, you can accomplish the same thing with
Selection.Find.ClearFormatting
Selection.Find.Text = ""
Selection.Find.Style = "body text"
Selection.Find.Replacement.Text = ""
Selection.Find.Execute

As I hope you can see, it's a little more difficult to read. BTW, it also
takes a little longer to execute. The With statement, then, does at least
two things, it makes your code a little more readable (and, therefore,
easier to maintain), and it makes your routine (especially longer ones) more
efficient in terms of execution time because "fewer dots" in a line of code
execute faster.

HTH,
Dave
 
L

Lighthouse

Thanks again, Dave. That's just what I was looking for: a little help
mixed w/ a little explanation. The help is a bit maddening to a
newcomer. Who'da thunk you get nothing by searching on "with" but find
it by searching on "with statement"? Thanks also for that info about
"wd". I've seen that, but wasn't aware that it meant a constant.
 

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