How do I make some text bold, using Visual Basic in Excel to create a Word document?
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
w开发者_Python百科rdApp.Quit
Set wrdDoc = Nothing
Set wrdApp = Nothing
End Sub
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
精彩评论