如何在 Excel 中使用 Visual Basic 创建 Word 文档,将某些文本设置为粗体?

发布于 2024-11-16 21:53:53 字数 1014 浏览 4 评论 0原文

我见过这个,但它对我不起作用;我不知道在哪里将 insertafter 更改为 typetext。我应该更改以下内容以使部分文本变为粗体?

Sub CreateNewWordDoc()
    Dim wrdDoc As Word.Document
    Dim wrdApp As Word.Application
    Set wrdApp = CreateObject("Word.Application")
    Set wrdDoc = wrdApp.Documents.Add
    With wrdDoc
        .Content.InsertAfter "not bold "
        .Content.Font.Bold = True
        .Content.InsertAfter "should be bold"
        .Content.Font.Bold = False
        .Content.InsertAfter " again not bold, followed by newline"
        .Content.InsertParagraphAfter
        .Content.Font.Bold = True
        .Content.InsertAfter "bold again"
        .Content.Font.Bold = False
        .Content.InsertAfter " and again not bold"
        .Content.InsertParagraphAfter
        .SaveAs ("testword.doc")
        .Close
    End With
    wrdApp.Quit
    Set wrdDoc = Nothing
    Set wrdApp = Nothing
End Sub

I've seen this, but it doesn't work for me; I don't get where to change insertafter to typetext. What should I change in the following to make part of the text bold?

Sub CreateNewWordDoc()
    Dim wrdDoc As Word.Document
    Dim wrdApp As Word.Application
    Set wrdApp = CreateObject("Word.Application")
    Set wrdDoc = wrdApp.Documents.Add
    With wrdDoc
        .Content.InsertAfter "not bold "
        .Content.Font.Bold = True
        .Content.InsertAfter "should be bold"
        .Content.Font.Bold = False
        .Content.InsertAfter " again not bold, followed by newline"
        .Content.InsertParagraphAfter
        .Content.Font.Bold = True
        .Content.InsertAfter "bold again"
        .Content.Font.Bold = False
        .Content.InsertAfter " and again not bold"
        .Content.InsertParagraphAfter
        .SaveAs ("testword.doc")
        .Close
    End With
    wrdApp.Quit
    Set wrdDoc = Nothing
    Set wrdApp = Nothing
End Sub

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

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

发布评论

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

评论(1

薔薇婲 2024-11-23 21:53:53

当您编写 .Content.Font.Bold = True.Content.Font.Bold = False 时,您将切换整个 内容在粗体和非粗体之间来回切换。此类的最后一个语句是 .Content.Font.Bold = False,因此当然没有任何内容最终会变成粗体。

这是一种做你想做的事的方法。它写得确实很糟糕而且很快,但它应该能让你走上正轨。

    .Content.InsertAfter "not bold "
    .Content.InsertAfter "should be bold"
    .Content.InsertAfter " again not bold, followed by newline"
    .Content.InsertParagraphAfter
    .Content.InsertParagraphAfter
    .Content.InsertParagraphAfter
    .Content.InsertAfter "bold again"
    .Content.InsertAfter " and again not bold"
    .Content.InsertParagraphAfter

    .Words(3).Font.Bold = True
    .Words(4).Font.Bold = True
    .Words(5).Font.Bold = True
    .Words(16).Font.Bold = True
    .Words(17).Font.Bold = True

When you write .Content.Font.Bold = True and .Content.Font.Bold = False, you're toggling the entire Content back and forth between bold and not bold. The last statement of this sort is .Content.Font.Bold = False, so of course nothing ends up being bold.

Here's a way to do what you want. It's really badly and quickly written but it should put you on the right track.

    .Content.InsertAfter "not bold "
    .Content.InsertAfter "should be bold"
    .Content.InsertAfter " again not bold, followed by newline"
    .Content.InsertParagraphAfter
    .Content.InsertParagraphAfter
    .Content.InsertParagraphAfter
    .Content.InsertAfter "bold again"
    .Content.InsertAfter " and again not bold"
    .Content.InsertParagraphAfter

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