• Find and Replace Macro

    Author
    Topic
    #467063

    I need a macro to find ^m where the font is Verdana and the replace is ^m with the font Arial.

    I can set up the Find and Replace to do that but when I record that action as a macro I get:

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
    .Text = “^m”
    .Replacement.Text = “^m”
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll

    As you can see there is no reference to the fonts. Could someone please advise me what amendment is needed to the code?

    In case it is relevant, I will be following that up with a second part to the macro so that after the above has been processed, the following find and replace will run.

    replace ^m^p with ^m

    Viewing 5 reply threads
    Author
    Replies
    • #1211539

      Do you want to have “^m” in Arial or the whole line?

      cheers, Paul

    • #1211543

      I need a macro to find ^m where the font is Verdana and the replace is ^m with the font Arial.

      I can set up the Find and Replace to do that but when I record that action as a macro I get…

      As you can see there is no reference to the fonts.

      This is a weakness in the Macro Recorder. For further discussion on that, scroll down to the “Fixing broken Replace macros” section in the article How to Modify a Recorded Macro on the MVPs.org site.

    • #1211569

      I have to ask – hope this isn’t a stupid question but…

      ^m is a manually inserted page break. How does it have a font attribute?

      • #1211621

        I have to ask – hope this isn’t a stupid question but…

        ^m is a manually inserted page break. How does it have a font attribute?

        Not a stupid question at all. Actually, page breaks do have font properties, inasmuch as they do have a style applied to them (and they could even pick up font formatting from any direct formatting in effect in the range where it was inserted). Maybe another way of putting the question is: why would you need to change the font attribute associated with a page break, per se?

        Gary

    • #1211662

      Sorry to come back late, Woody’s Lounge used to email re replies so looks like I need to change something here.

      First, thanks for the link, I’ll take a look.

      Second the why I need this. I have a document produced from some other software. Often a heading on a new page comes out one line down for no good reason. Someone discovered that if you run two find and replace operations, it fixes the problem so that the heading is on the first line of the page. The find and replace operations are:

      1. replace ^m verdana with ^m arial
      2. replace ^m^p with ^m

      Now it’s my turn. Why would that be?

      • #1211668

        Second the why I need this. I have a document produced from some other software. Often a heading on a new page comes out one line down for no good reason. Someone discovered that if you run two find and replace operations, it fixes the problem so that the heading is on the first line of the page. The find and replace operations are:

        1. replace ^m verdana with ^m arial
        2. replace ^m^p with ^m

        Now it’s my turn. Why would that be?

        Because the first replace operation probably isn’t necessary?:

        If you’ve got a page break and then a heading one line down, then what you’ve got to begin with is a manual page break, followed by an empty paragraph, followed by your heading.
        The second replace should take care this, by replacing “manual page break plus empty paragraph” with “manual page break” – i.e., delete any empty paragraphs that follow page breaks.

        Why the font replace is getting involved, don’t know. What happens if you just do the second replace, without the first one?

        Gary

    • #1211666

      Thanks Jefferson.

      This does the job. I am very grateful.

      Sub RemoveBlankLines()

      ‘ Macro created 01/03/2010 by Peter Grainge

      Selection.Find.ClearFormatting
      Selection.Find.Replacement.ClearFormatting
      With Selection.Find
      .Text = “^m”
      .Replacement.Text = “^m”
      .Font.Name = “Verdana”
      .Replacement.Font.Name = “Arial”
      .Forward = True
      .Wrap = wdFindContinue
      .Format = True
      .MatchCase = False
      .MatchWholeWord = False
      .MatchWildcards = False
      .MatchSoundsLike = False
      .MatchAllWordForms = False
      End With
      Selection.Find.Execute Replace:=wdReplaceAll

      Selection.Find.ClearFormatting
      Selection.Find.Replacement.ClearFormatting
      With Selection.Find
      .Text = “^m^p”
      .Replacement.Text = “^m”
      .Forward = True
      .Wrap = wdFindContinue
      .Format = True
      .MatchCase = False
      .MatchWholeWord = False
      .MatchWildcards = False
      .MatchSoundsLike = False
      .MatchAllWordForms = False
      End With
      Selection.Find.Execute Replace:=wdReplaceAll

      MsgBox “Blank lines removed”

      End Sub

    • #1211880

      Hi Peter,

      At most you should need:

      Code:
      Sub RemoveBlankLines()
      With ActiveDocument.Range.Find
        .ClearFormatting
        .Text = "(^m)[^13]{1,}"
        .Replacement.Text = "1"
        .Replacement.ClearFormatting
        .Replacement.Font.Name = "Arial"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = True
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute Replace:=wdReplaceAll
      End With
      MsgBox "Blank lines removed"
      End Sub

      With this code, it doesn’t matter how many empty paragraphs appear after a manual page break, or what font they’re in – they’ll all be deleted and replaced with an Arial-formatted manual page break. I too doubt, though, that setting the page break font to Arial is necessary.

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    Viewing 5 reply threads
    Reply To: Find and Replace Macro

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: