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 / March 2005

Tip: Looking for answers? Try searching our database.

Help get me out of a Loop

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Greg - 22 Mar 2005 14:41 GMT
I have a question about refining a Find and Replace Loop.

I am working on a VBA word indexer where I need to 1) find the word 2)
First Cap and bold the word 3) Index the word.

Simple example:

Word to index "Apples"

Sample text:  Apples, apples, apples,

Code:
Dim rngstory As Word.Range
Set rngstory = ActiveDocument.Range
With rngstory.Find
 .ClearFormatting
 .Replacement.ClearFormatting
 .MatchWildcards = False
 .MatchWholeWord = True
 .Wrap = wdFindStop
 .Text = "Apples"
 .Replacement.Text = .Text
 .Replacement.Font.Bold = True
 Do
   .Execute Replace:=wdReplaceOne
   rngstory.Collapse Direction:=wdCollapseEnd
   If .Found Then
     ActiveDocument.Indexes.MarkEntry _
     Range:=rngstory, Entry:=.Text
   End If
 Loop While .Found
End With
End Sub

Without the IF statement the last instance of apples in the sample text
is indexed twice.  I figured out why that is and used the IF statement
to resolve it.  My question:  Is there a more appropriate method for
defining a Do Loop for this application such that the Loop is executed
only IF the item is found?  I thought something like:

.Found = True
Do While .Found
...

But that doesn't work.  Thanks.
Helmut Weber - 22 Mar 2005 21:38 GMT
Hi Greg,
i am not familiar with indices,
but "loop while .found" at the end of the loop
repeats search and following actions included in the loop
after the last found.

I use
with range.find
  while .execute ' if search or next search is successfull
  ...
  wend
end with

Compare my answers to the postings of Singeredel.

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
Greg Maxey - 22 Mar 2005 23:05 GMT
Helmut,

Yes I like that.  After I posted I crawled my way through and found these
alternative methods

'Don't bother with .Replacement method just redefine the found range
 While .Execute
     rngstory.Text = .Text
     rngstory.Font.Bold = True
     rngstory.Collapse Direction:=wdCollapseEnd
     ActiveDocument.Indexes.MarkEntry Range:=rngstory, Entry:=.Text
  Wend

(Which resembles your method)

and

 'Find once then enter the loop and perform while found
 .Execute Replace:=wdReplaceOne
 Do While .Found
   rngstory.Collapse Direction:=wdCollapseEnd
   ActiveDocument.Indexes.MarkEntry _
     Range:=rngstory, Entry:=.Text
   .Execute Replace:=wdReplaceOne
 Loop

Did you get my e-mail note?

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> Hi Greg,
> i am not familiar with indices,
[quoted text clipped - 17 lines]
> Word XP, Win 98
> http://word.mvps.org/ 
 
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.