Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / February 2005

Tip: Looking for answers? Try searching our database.

looping from a given point in a document

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Lighthouse - 14 Feb 2005 16:35 GMT
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
Dave Lett - 14 Feb 2005 17:56 GMT
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

> 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
[quoted text clipped - 6 lines]
> before that starting point. I'm hoping someone can point me in the
> right direction for this. TIA
Lighthouse - 14 Feb 2005 20:27 GMT
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.
Dave Lett - 15 Feb 2005 11:45 GMT
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

> 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.
[quoted text clipped - 11 lines]
> "with" doesn't show up in my VB help file. Sorry, I'm kind of dense
> with VB.
Lighthouse - 15 Feb 2005 13:02 GMT
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.

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.