Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I detect if find and replace found nothing? 1

Status
Not open for further replies.

stduc

Programmer
Nov 26, 2002
1,903
GB
I need a word macro that finds and replaces repeatedly until there is nothing left to replace. I have searched the forums but can't find any examples.

Basically I am trying to write a macro that amongst other things finds and replaces multiple spaces with a single space. What I can't figure out is how to make it loop until there are no more multiple spaces to replace.
i.e
Psuedo code

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " "
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Do until not found
Selection.Find.Execute Replace:=wdReplaceAll
loop

It's the actual code for Do until not found I am seeking.

Or is this not possible?
 

To answer your question, there isn't any straightforward way to pick up how many replacements have been made. What you need to do is check first whether any will be made by doing a Find without replacing so your 'not found' condition becomes ...
Code:
    Do While Selection.Find.Execute
        Selection.Find.Execute Replace:=wdReplaceAll
    Loop

However, in this case there is an easier way, using wildcards ...
Code:
[blue]    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = [red]"( )@"[/red]
        .Replacement.Text = " "
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = [red]True[/red]
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll[/blue]

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
TonyJollans
Brilliant. Just what I needed. Many thanks. I needed the first example. I knew how to do the second - but looks like I gave a too simple example. I am actually searching for superfluous paragraph marks and other weird stuff in an imported file.

BTW - as a supplemental question.

Do you know why I am being left with several paragraph marks at the end of the document (after running the macro) that can't be selected for and removed? {The original import has about a 100 paragraph marks at the end! These get reduced to just a few.

Here's the relevant code chunk.

Selection.HomeKey Unit:=wdStory
With Selection.Find
.Text = "^p^p"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Do While Selection.Find.Execute
Selection.Find.Execute Replace:=wdReplaceAll
Loop
 

If most are being removed it sounds like the process is working. Perhaps there are some other characters (section breaks, empty fields?) between the paragraph marks. If just two are left at the end that would be expected as you can't replace the last paragraph mark in a document, and you would need some special case code to deal with it.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
I looked at one of the import files in a binary editor. The file has random strings of <cr> and <lf> characters at the end. Not the <cr><lf> sequences I presume word expects.

It's no big problem though. My finished macro does 95% of the work re-formatting the text to something presentable.

Again, many thanks for your help.
 
A follow up to this:

I have a similar issue, but in my case I am trying to find italic formatting and replace with <i> tags. It works, but inconsistently, usually missing one of the occurrences that Word would find if I just used the Find dialog. Here is the code:


Sub FindItalic()
With Selection.Find
Do While Selection.Find.Execute
Selection.Find.ClearFormatting
Selection.Find.Font.Italic = True
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
Selection.Find.Execute
Selection.Font.Italic = wdToggle
Selection.Text = "<i>" & Selection.Text & "<\i>"
Loop
End With
End
End Sub

Any ideas greatly appreciated.

Thanks,

Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top