使用 VBA for word 选择文本并使其加粗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4457050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:59:55  来源:igfitidea点击:

Using VBA for word to select text and make it bold

vbaword-vba

提问by Aaron

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.

我每周都会制作几页的 Word 文档。我从 PDF 复制文本并将其粘贴到 Word 文档中,然后格式化粘贴的文本。

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

有60种不同的代码。例如“FIPS”或“LILL”。

回答by Dr. belisarius

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

回答by BeemerGuy

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.

一旦你回答了这个问题,你就可以将它与宏的代码结合起来并自动执行任务。