Consulting

Results 1 to 5 of 5

Thread: Find/replace formatting problem (bold spills over from previous line)

  1. #1
    VBAX Regular
    Joined
    Jan 2018
    Posts
    34
    Location

    Find/replace formatting problem (bold spills over from previous line)

    I have situations in a Word doc where I have this:

    Name
    [unclear]

    I have a macro that looks for paragraph marks, the backwards capital P, and changes the '[u' to '[U'. The only problem is it then becomes:

    Name
    [U
    nclear]

    How do I stop the bold formatting on the '[U' bit?

    This is the VBA:

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
    .Text = "^p[u"
    .Replacement.Text = "^p[U"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll


    Many thanks,
    Helen

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    That is because you are replacing the found range with the formatted paragraph mark preceding the text. Try:

    Sub ScratchMacro()
    'A basic Word macro coded by Greg Maxey
    Dim oRng As Range
      Selection.Find.ClearFormatting
      Selection.Find.Replacement.ClearFormatting
      Set oRng = ActiveDocument.Range
      With oRng.Find
        .Text = "^p[u"
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        While .Execute
          oRng.Characters.Last = "U"
          oRng.Collapse wdCollapseEnd
        Wend
      End With
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Regular
    Joined
    Jan 2018
    Posts
    34
    Location
    Sorry, Greg, for some reason that didn't work.
    As it will be part of collection of subs in one macro to do different things to the document in one go then an easy workaround might be to have the last sub go through the document, find all instances of '[U' and just make them not bold. I'll see what I can find out there to do this. Thanks.
    Last edited by Helen269; 02-27-2019 at 11:37 AM.

  4. #4
    VBAX Regular
    Joined
    Jan 2018
    Posts
    34
    Location
    Aaaaand adapting a recorded macro works. Thanks, Greg. Case closed. :-)

  5. #5
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    Good. Puzzled that it didn't work though. Worked here.
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

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