Using VBA for word to select text and make it bold

好久不见. 提交于 2021-02-07 09:39:36

问题


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".


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/4457050/using-vba-for-word-to-select-text-and-make-it-bold

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!