Aaron
Aaron

Reputation: 23

Using VBA for word to select text and make it bold

I make a several page word document every week. I copy text from a PDF and paste it into a word document, I then format the text that I pasted.

This takes a long time and i would like to automate it.

I need a macro or some code to select specific text, then make that text bold. The specific text i need to bold is what i call a scrap code.

There are 60 different codes. For example "FIPS", or "LILL".

Upvotes: 2

Views: 33247

Answers (2)

Dr. belisarius
Dr. belisarius

Reputation: 61016

Something like this:

Sub A()
'
' a Macro
'
'
Dim A(3) As String

A(1) = "code1"
A(2) = "code2"
A(3) = "code3"

For i = 1 To 3
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
       .Forward = True
       .Wrap = wdFindStop
       .Format = False
       .MatchCase = False
       .MatchWholeWord = False
       .MatchWildcards = False
       .MatchSoundsLike = False
       .MatchAllWordForms = False
       .Replacement.Font.Bold = True

       .Execute FindText:=A(i), ReplaceWith:=A(i), Format:=True, _
         Replace:=wdReplaceAll

    End With
Next i
End Sub  

HTH!

Edit

To switch dollar amounts to bold

Sub a()
'
' a Macro
'
'
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Replacement.Font.Bold = True
    With Selection.Find
        .Text = "$([0-9.,]{1,})"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
    End With
    Selection.Find.Execute
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub

Upvotes: 6

BeemerGuy
BeemerGuy

Reputation: 8269

I would suggest recording a macro.
Then do all the modifications and formatting.
Finally, look at the code of the macro and see how it did it.

The one thing you need to figure out is how you logically want to locate the text you want to bold.
Is it a specific line? Is it at the beginning of a known word?

Once you have that answered, you can combine it with the code of the macro and automate the task.

Upvotes: 0

Related Questions