Re: Search-and-replace macro never works

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



There is very little wrong with the capability of VBA in Word. What it
lacks however is a proper macro recorder and trying to record Seach and
Replace Operations is one area where it is really deficient.

The following macro will do what you want:

Dim drange As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="Your Name", MatchWildcards:=False,
Wrap:=wdFindStop, Forward:=True) = True
Set drange = Selection.Range
If drange.ParagraphFormat.Alignment = wdAlignParagraphCenter Then
drange.Paragraphs(1).Range.Delete
End If
Loop
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

"Liam Gibbs" <liamgibbs@xxxxxxxxxxxx> wrote in message
news:1178049020.305062.270770@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hello everyone,

I'm well aware that MS Word is not good with macros, but I never
realized to what extent until now. Here's the situation:

I'm trying to do a search-and-replace of my name in a certain style.
That's it.

So, when recording the macro, I do a search-and-replace, enter my name
plus a carraige return in the style "Centered". I enter nothing for
the replace field, as in I want to eliminate my name and leave
nothing. I do the search-and-replace, and it gets rid of them. End the
recording.

However, when I play the macro, it does nothing. I even get rid of the
^p, so that it's only looking for my name in the Centered style. No
love. Here's the thing. When I open up the search-and-replace dialog
box, instead of it looking for my name in Centered (i.e. I'm looking
at the criteria for the previous search), it was looking for Centered
with Border: Top (Single Solid Line, Auto, etc. etc.). Well, that's
not what I asked for. Where did it get the extra stuff? I looked at
the macro code, but I'm no expert and couldn't find anything out of
the ordinary.

What am I doing wrong? Or is this another of Microsoft's "features"?



.



Relevant Pages

  • Re: Why on earth did MS take away "Record Macro???"
    ... Now I want to resize the cropped image to fill the slide again. ... End With ' PictureFormat ... Excel, recording a macro while I cropped-and-resized, but it didn't record ...
    (microsoft.public.powerpoint)
  • Re: Why on earth did MS take away "Record Macro???"
    ... Now I want to resize the cropped image to fill the slide again. ... Excel, recording a macro while I cropped-and-resized, but it didn't record ...
    (microsoft.public.powerpoint)
  • Re: Word 2003 Macro problem
    ... No, Word 2003 changed the default way it handles inserted pictures, which (I ... suspect) interferes with the recording of a macro. ... I suspect that your insertion and modifications didn't ...
    (microsoft.public.word.vba.beginners)
  • Re: Cant find styles in VBA that were created during import of .htm file to Word
    ... Word VBA. ... When I use Find in the Word app to find paragraphs with one of the style ... and then tried running the same macro recorded ... Stepping through the following macro in VBA, which is just a recording of ...
    (microsoft.public.word.vba.general)
  • RE: looping through series
    ... I tested the macro recording on xl2007 and I see what you mean. ... Sub new_demo ... leave well enough alone ...
    (microsoft.public.excel.charting)