maddy
maddy

Reputation: 83

how do i select a paragraph based on style make changes in the para using VBA

I have a Word document which contains of lot of styles; in that I want to select particular style and make starting numbers alone bold in all the paragraph style

eg:

1. first numbers alone

23. first number alone

This is my code

     Sub ParaStyle()

        Selection.HomeKey wdStory
        Dim i       As Integer 
        i = 1                              

        Do Until i = Application.ActiveDocument.Paragraphs.Count

            If Selection.ParagraphFormat.Style = "heading3" Then

                     Selection.Find.ClearFormatting
                    Selection.Find.Style = ActiveDocument.Styles("heading3")
                    Selection.Find.Replacement.ClearFormatting
                    Selection.Find.Replacement.Font.Bold = True
                         With Selection.Find
                               .Text = "([0-9]{1,2})"
                               .Replacement.Text = "\1"
                               '.Forward = True
                               .Wrap = wdFindContinue
                               .Format = True
                               .MatchWildcards = True
                               .Execute Replace:=wdReplaceOne

                        End With
            End If

            i = i + 1

        Loop

    End Sub

Thanks in advance

Upvotes: 0

Views: 4481

Answers (1)

Christina
Christina

Reputation: 1379

You are running the search on the Selection, but you're not changing that selection between runs. So you just end up making the same text bold over and over again. Here's a way to do what you're doing without the Selection object:

Sub ParaStyle()
Dim objPara As Paragraph

    For Each objPara In ActiveDocument.Paragraphs

        If objPara.Style = "heading3" Then
            With objPara.Range.find
                .ClearFormatting
                .Text = "([0-9]{1,2})"
                .Style = ActiveDocument.Styles("heading3")
                With .Replacement
                    .ClearFormatting
                    .Font.Bold = True
                    .Text = "\1"
                End With
                '.Forward = True
                .Wrap = wdFindContinue
                .Format = True
                .MatchWildcards = True
                .Execute replace:=wdReplaceOne
            End With
        End If
    Next objPara

End Sub

I didn't change much; instead of using a Do loop we loop through all the paragraphs in the document and work on each one. You can also use your code but make sure to do ActiveDocument.Paragraphs(i).Select before running the replace. I don't recommend that, as it's best to avoid using the Selection object when you can (one good reason to avoid it is that, if you have a script that takes a while and you try to do something else in a text editor, say, you'll run the risk of contaminating your clipboard).

Do keep in mind that there's nothing here to prevent this search from finding a number in the middle of the paragraph if there's none at the beginning. I'm assuming that narrowing it down by style is enough for you, or you wouldn't be using this approach.

Upvotes: 1

Related Questions