Results 1 to 9 of 9
  1. #1
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    UK
    Posts
    239
    Thanks
    4
    Thanked 1 Time in 1 Post
    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
    Attached Images Attached Images

  2. #2
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,191
    Thanks
    48
    Thanked 985 Times in 915 Posts
    Do you want to have "^m" in Arial or the whole line?

    cheers, Paul

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    Quote Originally Posted by Peter Grainge View Post
    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.

  4. #4
    4 Star Lounger
    Join Date
    Jan 2010
    Location
    Chichester, UK
    Posts
    401
    Thanks
    1
    Thanked 10 Times in 9 Posts
    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?

  5. #5
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts
    Quote Originally Posted by Terry Farrell View Post
    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

  6. #6
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    UK
    Posts
    239
    Thanks
    4
    Thanked 1 Time in 1 Post
    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?

  7. #7
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    UK
    Posts
    239
    Thanks
    4
    Thanked 1 Time in 1 Post
    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

  8. #8
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts
    Quote Originally Posted by Peter Grainge View Post
    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

  9. #9
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    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
    [MS MVP - Word]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •