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

发布于 2024-10-08 00:54:38 字数 187 浏览 0 评论 0原文

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

这需要很长时间,我想将其自动化。

我需要一个宏或一些代码来选择特定文本,然后将该文本设置为粗体。我需要加粗的具体文本就是我所说的废代码。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不在是我 2024-10-15 00:54:38

像这样的:

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!

编辑

将美元金额切换为粗体

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

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
和我恋爱吧 2024-10-15 00:54:38

我建议录制一个宏。
然后进行所有修改和格式化。
最后看一下宏的代码,看看它是怎么做的。

您需要弄清楚的一件事是如何从逻辑上找到要加粗的文本。
是特定线路吗?它位于已知单词的开头吗?

一旦找到答案,您就可以将其与宏代码结合起来并自动执行任务。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文