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 2007

Tip: Looking for answers? Try searching our database.

Search & Replace

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Clueless - 27 Feb 2007 13:12 GMT
I am trying to write code that will search for a word (psychosis for example)
and pause each time the word is found to display a form that will allow the
user to delete the word, sentence or paragraph in which the word resides.

I know how to find the word and I have built the form (not shown) which will
expand the selection and delete as much as the user requests

With Selection.Find
       .Text = "psychosis"
       .Replacement.Text = ""
       .Forward = True
       .Format = False
       .MatchCase = False
       .MatchWholeWord = False
       .MatchWildcards = False
       .MatchSoundsLike = False
       .MatchAllWordForms = False
   End With
   Selection.Find.Execute Replace:=wdReplaceAll

But I don't know how to step through the document and pause at each
occurrence of psychosis. An earlier post on this site suggested different
code to find a word but I can't figure out how to use it.

Sub Test()
Dim oRng As Word.Range
Dim oRngDup As Word.Range
Dim oDoc As Document
Set oDoc = ActiveDocument
Set oRng = oDoc.Range
oRng.Start = oDoc.Words(1).Start
oRng.End = oDoc.Words(1).End
Set oRngDup = oRng.Duplicate
With oRng.Find
 .Text = oDoc.Words(1).Text
 While .Execute
   If oRng.End < oRngDup.End Then
     oRng.Select
   Else
     MsgBox "Text not found."
   End If
 Wend
End With
End Sub

any help is much appreciated!
Edward Thrashcort - 27 Feb 2007 13:48 GMT
How about this pseudo code...

With Selection.Find
  .Text = "psychosis"
  .Replacement.Text = ""
  .Forward = True
  .Format = False
  .MatchCase = False
  .MatchWholeWord = False
  .MatchWildcards = False
  .MatchSoundsLike = False
  .MatchAllWordForms = False
  .Execute
  While .Found
     show myform
     (select the choice from myform and execute it)
     .Execute
  Wend
End With

Eddie

> *From:* Clueless<Clueless@discussions.microsoft.com>
> *Date:* Tue, 27 Feb 2007 05:12:03 -0800
[quoted text clipped - 46 lines]
>
> any help is much appreciated!
Clueless - 27 Feb 2007 15:11 GMT
Thanks very much!
I wil try it

> How about this pseudo code...
>
[quoted text clipped - 68 lines]
> >
> > any help is much appreciated!
Clueless - 03 Mar 2007 16:11 GMT
Your pseudocode searches and finds perfectly!

(To further demonstrate my ignorance about the event driven nature of object
oriented programming...)

After I select the choice from myform, how do I get back into the do-while
loop to pick up where I left off?

Many thanks

> How about this pseudo code...
>
[quoted text clipped - 68 lines]
> >
> > any help is much appreciated!
Edward Thrashcort - 04 Mar 2007 12:05 GMT
I'm not quite sure why you are stepping out of the loop
Would it help to make the "deletion code" a function?

Sub MySelector()

 With Selection.Find
       .Text = "psychosis"
       .Replacement.Text = ""
       .Forward = True
       .Format = False
       .MatchCase = False
       .MatchWholeWord = False
       .MatchWildcards = False
       .MatchSoundsLike = False
       .MatchAllWordForms = False
   .Execute
    While .Found
        ChooseToDelete
        .Execute
    Wend
 End With

End Sub

Function ChooseToDelete()
   Show myform
  '(select the choice from myform and execute it)
End Function

Eddie
Clueless - 05 Mar 2007 11:09 GMT
Your function solution worked perfectly! Thank you.

I hope this will be my last question:

I repeat this block of code with different words
As Selection.Find steps through each occurrence of a word sometimes the
document recenters itself on the newly detected word and sometimes not.
Is there a way I can force WORD to recenter or focus on the word that has
just been found?

Many thanks for your help!!

> I'm not quite sure why you are stepping out of the loop
> Would it help to make the "deletion code" a function?
[quoted text clipped - 26 lines]
>
> Eddie
Edward Thrashcort - 06 Mar 2007 00:21 GMT
> sometimes the document recenters itself on the newly detected word
> and sometimes not

It's the "sometimes not" that you need to focus on.  Try stepping through
your code when it reaches a "not" incident and see what's going on.

I can't possibly help you because I frankly can't imagine what the problem
might be.

Eddie

> *From:* Clueless<Clueless@discussions.microsoft.com>
> *Date:* Mon, 5 Mar 2007 03:09:10 -0800
[quoted text clipped - 41 lines]
> >
> > Eddie
Clueless - 06 Mar 2007 17:31 GMT
Thanks sincerely for all your help
I will proceed as you have suggested
Cheers

> > sometimes the document recenters itself on the newly detected word
> > and sometimes not
[quoted text clipped - 52 lines]
> > >
> > > Eddie
 
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.