Consulting

Results 1 to 3 of 3

Thread: add words where is bold formating

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

    add words where is bold formating

    Hi, a following macro add desired words before and after part of paragraph when it contains bold formating, the problem is that it add that words even if paragraph is empty or it is one accidental sign. Could You help me with that?

    Sub add_bold()
      
        Dim x As Long, i As Long, ArrFnd()
        ArrFnd = Array("")
        For x = 0 To UBound(ArrFnd)
            With ActiveDocument.Range
                With .Find
                    .ClearFormatting
                    .Replacement.ClearFormatting
                    .Forward = True
                    .Wrap = wdFindStop
                    .Format = False
                    .MatchWildcards = True
                    .Font.bold = True
                    .Font.Color = RGB(33, 33, 32)
                    .ParagraphFormat.Alignment = wdAlignParagraphJustify
                    .Execute
                End With
                Do While .Find.Found
                    .InsertAfter "{/body:bold}"
                    .InsertBefore "{body:bold}"
                    .End = .End
                    .Collapse wdCollapseEnd
                    .Find.Execute
                Loop
            End With
        Next
      End Sub
    Karol

  2. #2
    Based on your code and with some sample text in the array, the following will work

    Sub add_bold()Dim orng As Range
    Dim x As Long, i As Long, ArrFnd()
        ArrFnd = Array("dolor", "nostrud", "blandit")
        For x = 0 To UBound(ArrFnd)
            Set orng = ActiveDocument.Range
            With orng.Find
                .ClearFormatting
                .Replacement.ClearFormatting
                .Forward = True
                .Wrap = wdFindStop
                .Format = False
                .MatchWildcards = True
                .Text = ArrFnd(x)
                .Font.Bold = True
                .Font.Color = RGB(33, 33, 32)
                .ParagraphFormat.Alignment = wdAlignParagraphJustify
                Do While .Execute
                    orng.Text = "{body:bold}" & orng.Text & "{/body:bold}"
                    orng.Collapse wdCollapseEnd
                Loop
            End With
        Next
    End Sub
    However if you just want to tag bold text

    Sub add_bold2()Dim orng As Range
        Set orng = ActiveDocument.Range
        With orng.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Text = ""
            .Font.Bold = True
            Do While .Execute
                orng.Text = "{body:bold}" & orng.Text & "{/body:bold}"
                orng.Collapse wdCollapseEnd
            Loop
        End With
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Regular
    Joined
    Jan 2018
    Posts
    50
    Location
    Many thanks, but it still add tags to empty bold paragraphs, or to bolded paragraph signs at the end of paragraphs. Anyway I've solved that by adding if/then loop with lenght condition:

    Sub add_bold2()
    Dim orng As Range
        Set orng = ActiveDocument.Range
        With orng.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Text = ""
            .Font.bold = True
            .Font.Color = RGB(33, 33, 32)
            Do While .Execute
            If Len(orng) > 3 Then
                orng.Text = "{body:bold}" & orng.Text & "{/body:bold}"
                orng.Collapse wdCollapseEnd
                End If
            Loop
        End With
    End Sub
    Karol

Posting Permissions

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