Do While Search condition is true

M

Marceepoo

In the macro below, I'm trying to tell Word VBA (Word 2003 running in Xp) to
do a search and replace while the search text is true.

Alas, I can't get Word to see that the condition is true, i.e., that the
search text is in the document.

Obviously I'm doing something wrong. But I can't figure out what it is, or
where to find info about how to fix it.

any help would be much appreciated.

Thanks,
marceepoo




Sub RemovSmithDeliveryReportTest()
'
' mbh February 10, 2008
'

'---------------------------------------
Dim sTemp As String
Dim sDoWhileSearchConditn As String
sDoWhileSearchConditn = "findText:='Smith Delivery Summary Report for',
Forward:=True, " _
& "MatchWildcards:=True, MatchCase:=false, Wrap:=wdFindStop)"
sDoWhileSearchConditn = Replace(sDoWhileSearchConditn, "'", Chr(34))

sTemp = sDoWhileSearchConditn

sTemp = sDoWhileSearchConditn
Selection.HomeKey wdStory
' Selection.Find '.ClearFormatting
With Selection.Find
'
Do While .Execute(sDoWhileSearchConditn) = True
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Smith Delivery Summary Report for"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Extend
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Smith and their affiliates."
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Delete Unit:=wdCharacter, Count:=1

'Insert line separating the Smith documents
Selection.TypeParagraph
Selection.TypeText Text:= _
"____________________________________________________________"
Selection.TypeText Text:="_______________"
Selection.TypeParagraph
Selection.TypeParagraph

Loop
End With


End Sub
 
H

Helmut Weber

Hi Marceepoo,

your code looks very odd to me.

Do you mean something like that:

Sub Test()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "Smith Delivery Summary Report for"
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "Smith and their affiliates."
.Replacement.Text = String(60, "_") & "^p^p^p"
.Execute Replace:=wdReplaceAll
End With
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
M

Marceepoo

Dear Helmut:

My code finds the first few words ("Smith Delivery Summary Report for") in a
big block of text;
then (using F8 to hold that text as the beginning of the block) my
code searches for the last few words in the block ("Smith and their
affiliates."), and
then my code deletes the blocked text.

1. I wish I knew how to do that using the Range object (which I'm sure is
the more elegant/efficient/reliable way of making a block of the text to be
deleted), but that deficit in my knowledge is less "urgent" right now.

2. The Urgent Need: I need to repeat the Find, Block text, Delete Routine
as many times as the block of text exists in the document, i.e., to test if
the beginning string ("Smith and their affiliates.") exists, and if True,
then to run the Find, Block text, Delete Routine routine again.

3. While writing this, it occured to me that another way to accomplish my
objective would be to find a way to count the instances of the beginning
string ("Smith and their affiliates.") in the document. (I don't know how
to do that either.)

Thanks again for your generous and always insightful help.

Gratefully, and with Kindest regards,

Marc
 
H

Helmut Weber

Hi Marceepoo,

couldn't get it to work without "goto", however:

Sub Test()
Dim x As Long
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
start:
With rDcm.Find
rDcm.HighlightColorIndex = wdNoHighlight
.Text = "Smith Delivery Summary Report for"
While .Execute
x = rDcm.start
rDcm.End = ActiveDocument.Range.End
With rDcm.Find
.Text = "Smith and their affiliates."
If .Execute Then
rDcm.start = x
rDcm.HighlightColorIndex = wdYellow ' for testing
End If
End With
rDcm.Text = String(60, "_")
rDcm.Collapse Direction:=wdCollapseEnd
rDcm.End = ActiveDocument.Range.End
GoTo start
Wend
End With
End Sub


--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 

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