如何在 Excel 中使用 Visual Basic 创建 Word 文档,将某些文本设置为粗体?
我见过这个,但它对我不起作用;我不知道在哪里将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您编写
.Content.Font.Bold = True
和.Content.Font.Bold = False
时,您将切换整个内容
在粗体和非粗体之间来回切换。此类的最后一个语句是.Content.Font.Bold = False
,因此当然没有任何内容最终会变成粗体。这是一种做你想做的事的方法。它写得确实很糟糕而且很快,但它应该能让你走上正轨。
When you write
.Content.Font.Bold = True
and.Content.Font.Bold = False
, you're toggling the entireContent
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.