Excel VBA到Word Doc - 更改句子/段落中的字体样式

时间:2015-05-13 17:40:58

标签: excel vba excel-vba ms-word

我的目标是编写一个句子/段落,能够在同一个句子/段落中更改字体样式。这是在 Excel VBA 中完成的,并在word文档中输出(我确定你可以告诉)。

示例:"请参阅表1 注意表1是假设的。"

使用下面的代码,似乎将整个句子设置为最后一个被调出的wdStyleTypeCharacter。

Dim appWord As Word.Application
Dim objDoc As Word.Document
Set appWord = New Word.Application
appWord.Visible = True
Set objDoc = appWord.Documents.Add

Set Normal = objDoc.Styles.Add(Name:="Normal", Type:=wdStyleTypeCharacter)
Set Bold = objDoc.Styles.Add(Name:="Bold", Type:=wdStyleTypeCharacter)
Set Italic = objDoc.Styles.Add(Name:="Italic", Type:=wdStyleTypeCharacter)

With objDoc

With .Styles(Normal)
.Font.Name = "Arial Narrow"
.Font.Size = 11
.Font.Italic = False
.Font.Bold = False
End With

With .Styles(Bold)
.Font.Name = "Arial Narrow"
.Font.Size = 11
.Font.Italic = False
.Font.Bold = True
End With

With .Styles(Italic)
.Font.Name = "Arial Narrow"
.Font.Size = 11
.Font.Italic = True
.Font.Bold = False
End With

.Content.InsertParagraphAfter
.Range.Style = .Styles(Normal)
.Content.InsertAfter "Please see "
.Range.Style = .Styles(Bold)
.Content.InsertAfter "Table 1"
.Range.Style = .Styles(Italic)
.Content.InsertAfter ".  Note Table 1 is assumed."
.Content.InsertParagraphAfter

End with

objDoc.Close savechanges:=wdSaveChanges
appWord.Quit
Set objDoc = Nothing
Set objWord = Nothing
End Sub

1 个答案:

答案 0 :(得分:0)

我不是100%肯定你为什么要这么。选择word.application但这解决了我的问题。如果有人能够回答这个问题,那将是我的基本理解(对此不熟悉)。

Dim appWord As Word.Application
Dim objDoc As Word.Document
Set appWord = New Word.Application
appWord.Visible = True
Set objDoc = appWord.Documents.Add

With appWord.Selection
.Font.Name = "Arial Narrow"
.Font.Size = 11
.TypeText Text:="Please see "
.Font.Bold = wdToggle
.TypeText Text:="Table 1"
.Font.Bold = wdToggle
.Font.Italic = wdToggle
.TypeText Text:=". Note Table 1 is assumed."
End With

objDoc.Close savechanges:=wdSaveChanges
appWord.Quit
Set objDoc = Nothing
Set objWord = Nothing
End Sub

希望将来可以帮助其他人。