Results 1 to 14 of 14
  1. #1
    2 Star Lounger
    Join Date
    Aug 2011
    Posts
    112
    Thanks
    35
    Thanked 3 Times in 3 Posts

    Find and Replace multiple words with bold

    This is related to some of my previous post with using SEQ fields.

    Maybe two questions here.

    1) The code below works great for replacing the word typed in the InputBox from normal to bold, but vice using the InputBox, how can this loop and look for the three (or more) words in an array?

    When I execute the macro below, it works fine to bold the word input into the InputBox all the way to the last period, (e.g., Table 1.1., Table 1.2., Table 1.3.), but as soon as I replace the last number in the tables with a SEQ field the bold no longer works.

    Any Thoughts?

    Code:
    Sub FixTableFigureParagraph()
        Dim myTxt As String
        Dim oRng As Range
        Set oRng = ActiveDocument.Range
        myTxt = InputBox("Type the text to find:")
        With Selection
            With oRng.Find
                .ClearFormatting
                .Replacement.ClearFormatting
                .Replacement.Font.Bold = True
                If myTxt = "Table" Then
                    .text = "<(Table) [0-9]{1,}(\.)[\0-9]{1,}(\.)"
                ElseIf myTxt = "Figure" Then
                    .text = "<(Figure) [0-9]{1,}(\.)[\0-9]{1,}(\.)"
                ElseIf myTxt = "Paragraph" Then
                    .text = "<(Paragraph) [0-9]{1,}(\.)[\0-9]{1,}(\.)"
                End If
                .Replacement.text = "^&"
                .Forward = True
                .Wrap = wdFindContinue
                .Format = True
                .MatchCase = False
                .MatchWholeWord = False
                .MatchAllWordForms = False
                .MatchSoundsLike = False
                .MatchWildcards = True
                .Execute Replace:=wdReplaceAll
            End With
        End With
    End Sub

  2. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    For your text example, you could just use a single wildcard Find/Replace with:
    Find = \([FTP][ai][brg][aul][!\)]@\) [0-9.]{1,}
    Replace = ^&
    You thus wouldn't need the input box for any of your three words.

    As for use with SEQ fields, you'd need a macro like:
    Code:
    Sub Demo()
    Application.ScreenUpdating = False
    Dim Rng As Range
    Set Rng = Selection.Range
    With Selection.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "\([FTP][ai][brg][aul][!\)]@\) "
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchWildcards = True
        .Execute
      End With
      Do While .Find.Found
        If .InRange(Rng) Then
          .MoveEndUntil ".", wdForward
          .End = .End + 1
          .MoveEndUntil ".", wdForward
          If Not .Characters.Last.InRange(Rng) Then Exit Do
          .End = .End + 1
          .Font.Bold = True
        Else: Exit Do
        End If
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    Application.ScreenUpdating = True
    End Sub
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  3. #3
    2 Star Lounger
    Join Date
    Aug 2011
    Posts
    112
    Thanks
    35
    Thanked 3 Times in 3 Posts
    Hi Paul,

    I am desperately trying to understand the process here, but seems I'm failing as it's not working.

    For the first part, for the text, you suggested...

    For your text example, you could just use a single wildcard Find/Replace with:

    Find = \([FTP][ai][brg][aul][!\)]@\) [0-9.]{1,}
    Replace = ^&

    You thus wouldn't need the input box for any of your three words.
    ...which I'm first testing thru the Find/Replace dialogue before using the macro approach.

    I looked up the regular expression to understand as best I can the parts of the regular expression.

    Since the first letters searched are \[FTP], I'm guessing the subsequent brackets might refer to the first letter in order.

    So when I look at your suggestion, there isn't an "a" within Figure, nor is there a "brg" within Table. If my reasoning is correct, I adjusted too...

    \([FTP][ig][ab][ar][!\)]@\)[0-9.]{1,}

    None the less, this did not work nor the initial suggestion.

    As for use with SEQ fields, you'd need a macro like:
    As for the macro, when I step thru the code, the code reaches...

    Code:
    Do While .Find.Found
    ...and then exits out

    I sure I'm missing something simple here.

  4. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Sorry, I mis-read you code. Try a single wildcard Find/Replace with:
    Find = <[FTP][ai][brg][aul][! ]@ [0-9.]{1,}
    Replace = ^&

    and, in the code, change:
    .Text = "\([FTP][ai][brg][aul][!\)]@\) "
    to:
    .Text = "<[FTP][ai][brg][aul][! ]@ "

    Your alternate F/R code indicates you don't understand how wildcards work. Compare the letter sequence in the Find expression against the first four letters of each of your words.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  5. The Following User Says Thank You to macropod For This Useful Post:

    jrb (2016-04-09)

  6. #5
    2 Star Lounger
    Join Date
    Aug 2011
    Posts
    112
    Thanks
    35
    Thanked 3 Times in 3 Posts
    Your alternate F/R code indicates you don't understand how wildcards work. Compare the letter sequence in the Find expression against the first four letters of each of your words.
    Sure is easy when somebody points out the possible obvious. Thanks Paul

  7. #6
    2 Star Lounger
    Join Date
    Aug 2011
    Posts
    112
    Thanks
    35
    Thanked 3 Times in 3 Posts
    Hi Paul,

    The macro below works well to bold the reference (Figure, Table, Paragraph, Step, Attachments), but if the reference is at the end of the sentence, the period should not be bold.

    At the beginning of the thread I said the requirement was (e.g., Table 1.1., Table 1.2., Table 1.3.), but the requirement has been changed on me and there is no trailing period at the end of a reference.

    So, as an example...

    2.1. Step 1. (The period at the end should be not bold, this is the end)
    2.2. Step 2. (The period at the end should be not bold, this is the end)
    2.4. Step 4. (The period at the end should be not bold, this is the end)
    etc.,

    Paragraph 1.4. (The period at the end should be not bold as this is the end of the sentence)
    Tables 2.2. (The period at the end should be not bold as this is the end of the sentence)

    I hope this makes sense

    2. Application Instructions.

    2.1. Step 1. Obtain the average monthly number of missions handled by the ATOC per location as described in Paragraph 1.4.

    2.2. Step 2. Given the results from Step 1, determine manpower requirements for each location using Tables 2.1 and Tables 2.2. Add results from both tables together to calculate the fractional manpower requirement.

    2.3. Step 3. Check Attachment 3 for variances. Add any applicable fractional manpower requirements to the total from Step 2.

    2.4. Step 4. Round up the fractional manpower requirement result from Step 3 to the next whole requirement. Refer to the manpower table at Attachment 2 to determine proper grade and skill determination.
    Code:
    Sub FixReferences()
        'Figure, Table, Paragraph, Step, Attachment
        Dim oRng As Range
        Set oRng = ActiveDocument.Range
        With Selection
            With oRng.Find
                .ClearFormatting
                .Replacement.ClearFormatting
                .Replacement.Font.Bold = True
                .text = "<[FTPSA][ait][gbret][! ]@ [0-9]{1,}"
                .Replacement.text = "^&"
                .text = "<[FTPSA][ait][gbret][! ]@ [0-9.]{1,}"
                .Replacement.text = "^&"
                .Forward = True
                .Wrap = wdFindContinue
                .Format = True
                .MatchCase = False
                .MatchWholeWord = False
                .MatchAllWordForms = False
                .MatchSoundsLike = False
                .MatchWildcards = True
                .Execute Replace:=wdReplaceAll
            End With
        End With
    End Sub

  8. #7
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Quote Originally Posted by jrb View Post
    if the reference is at the end of the sentence, the period should not be bold.
    VBA has no concept of a grammatical sentence, so it's not possible for a macro to conditionally format anything at the end of one. As far as VBA is concerned, every period followed by a space, paragraph break, tab, etc. defines the end of a sentence.

    You could try:
    Code:
    Sub FixReferences()
    'Figure, Table, Paragraph, Step, Attachment
    With ActiveDocument.Range.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Format = True
      .Forward = True
      .Wrap = wdFindContinue
      .MatchWildcards = True
      .Replacement.Style = "Strong"
      .Text = "<[FTP][ai][brg][aul][! ]@ [0-9]{1,}.[0-9]{1,}"
      .Replacement.Text = "^&"
      .Execute Replace:=wdReplaceAll
      .Text = "Step [0-9]{1,}"
      .Execute Replace:=wdReplaceAll
      .Text = "Attachment [0-9]{1,}"
      .Execute Replace:=wdReplaceAll
    End With
    End Sub
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  9. #8
    2 Star Lounger
    Join Date
    Aug 2011
    Posts
    112
    Thanks
    35
    Thanked 3 Times in 3 Posts
    Curios, why did you use?

    Code:
    .Replacement.Style = "Strong"
    From what I can tell thru the searching I did, "Strong" is set to the Default Paragraph Font which seems to be the same as the "Normal" style.

    If that is the case, why doesn't this work?

    Code:
    .Replacement.Style = "Normal"
    I thought I was learning Excel rather well, but it seems Word is another world of it's own.

  10. #9
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    The Strong Style simply a Character Style that takes the underlying font and applies bold formatting to it. The visual effect is the same as applying bold, but using the Strong Style should make for a smaller file size and a document less susceptible to corruption. Applying the Normal Style affects the underlying Paragraph Style; it has no effect on Character Styles, etc.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  11. #10
    2 Star Lounger
    Join Date
    Aug 2011
    Posts
    112
    Thanks
    35
    Thanked 3 Times in 3 Posts
    Hi Paul,

    I thought I had checked this the other day and it was working fine, but not so now.

    Using the code in post #7, all the references turn bold.

    2. Application Instructions.
    2.1. Step 1. Obtain the average monthly number of missions as described in Paragraph 1.4.
    2.2. Step 2. Given results from Figure 1.1 , determine requirements using Table 1.1.
    2.3. Step 3. Check Attachment 3 for variances. Add to Step 2.
    2.4. Step 4. Round Step 3 to next whole requirement and refer to Attachment 2.

    Table 1.1. Test the table reference.

    Figure1.1. Test the figure reference.
    The part that is not working is the code from post #2 and the update in post #4.

    All of these steps have the 1, 2, 3, and 4 as a SEQ field

    2.1. Step 1.
    2.2. Step 2.
    2.3. Step 3.
    2.4. Step 4.

    I would like to have the Step 1, Step 2 etc., as bold.

    The Table and Figure references below also use SEQ fields and they need to be bold as well. They are both on one line followed on the next line by a table and/or figure. Is it possible to just have the whole line bolded?

    Table 1.1. This is one table reference.

    Figure1.1. This is one figure reference.
    Last edited by jrb; 2016-04-28 at 19:14.

  12. #11
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Using the macro in post #7, your sample from:
    post #6 is rendered as
    2. Application Instructions.
    2.1. Step 1. Obtain the average monthly number of missions handled by the ATOC per location as described in Paragraph 1.4.
    2.2. Step 2. Given the results from Step 1, determine manpower requirements for each location using Tables 2.1 and Tables 2.2. Add results from both tables together to calculate the fractional manpower requirement.
    2.3. Step 3. Check Attachment 3 for variances. Add any applicable fractional manpower requirements to the total from Step 2.
    2.4. Step 4. Round up the fractional manpower requirement result from Step 3 to the next whole requirement. Refer to the manpower table at Attachment 2 to determine proper grade and skill determination.
    and post #10 (after a space is inserted after the final 'Figure') is rendered as
    2. Application Instructions.
    2.1. Step 1. Obtain the average monthly number of missions as described in Paragraph 1.4.
    2.2. Step 2. Given results from Figure 1.1, determine requirements using Table 1.1.
    2.3. Step 3. Check Attachment 3 for variances. Add to Step 2.
    2.4. Step 4. Round Step 3 to next whole requirement and refer to Attachment 2.

    Table 1.1. Test the table reference.
    Figure 1.1. Test the figure reference.
    Any bolded periods at the end of a sentence are the results of some previous process - the macro in post #7 is not intended to correct that.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  13. #12
    2 Star Lounger
    Join Date
    Aug 2011
    Posts
    112
    Thanks
    35
    Thanked 3 Times in 3 Posts
    Any bolded periods at the end of a sentence are the results of some previous process - the macro in post #7 is not intended to correct that.
    Yes, the code in post #7 is working great for my needs. Bolded periods are truly not an issue for me.

    The reason the Step 1, Step 2, etc. in post #10 were not bold, is because they have a SEQ Field now and the code in post #7 will not bold those references. That is why you provided the code in post #2, but it was just for Tables, Figures, and Paragraphs; however, the requirement now is to include the Step reference at the beginning of the sentence. I thought I had initially tested bolding with a SEQ field, but maybe not as those references (Table/Figure) will not bold using post #2 code.

    2.1. Step {SEQ Step1 \n}.
    2.2. Step {SEQ Step1 \n}.

    Table 1.1. {SEQ Table1 \n}.
    Figure 1.1. {SEQ Figure1 \n}.

  14. #13
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    The macro in post #7 never would have worked with SEQ fields, and there was no indication in the previous discussion of which, if any, of the expressions contain them. For that you would need to supplement the existing code with:
    Code:
      .MatchWildcards = False
      ActiveWindow.View.ShowFieldCodes = True
      .Text = "Step ^d"
      .Execute Replace:=wdReplaceAll
      ActiveWindow.View.ShowFieldCodes = False
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  15. #14
    2 Star Lounger
    Join Date
    Aug 2011
    Posts
    112
    Thanks
    35
    Thanked 3 Times in 3 Posts
    Hi Paul,

    This works great. Appreciate your patience.

Posting Permissions

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