Search & Replace

C

Clueless

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!
 
E

Edward Thrashcort

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
 
C

Clueless

Thanks very much!
I wil try it

Edward Thrashcort said:
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
 
C

Clueless

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
 
E

Edward Thrashcort

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
 
C

Clueless

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!!
 
E

Edward Thrashcort

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
 
C

Clueless

Thanks sincerely for all your help
I will proceed as you have suggested
Cheers
 

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