自动化

时间:2016-01-21 21:49:18

标签: vb.net ms-word automation

我尝试在书签位置写一个word文档。我可以添加文字行,但我想添加多行,包括多种字体,大小等。 我想我需要添加多个段落,但是如何?

这是我的工作代码sofar。

Set oWord = CreateObject("Word.Application")
oWord.Visible = True
Set oDoc = oWord.Documents.Add
Set oDoc = oWord.Documents.Add("D:\MyDocument.dotx")     'Based on a template
oDoc.Bookmarks("OFFER").Range.Text = "First Line of text" & vbcrlf & "Second line of text"

我希望第二行的字体大小不同。

1 个答案:

答案 0 :(得分:2)

以下是如何做到这一点:

Dim oDoc as Document = oWord.Documents.Add("D:\MyDocument.dotx")

Dim range As Range = oDoc.Bookmarks("OFFER").Range

//Add paragraph 1
Dim paragraph1 As Paragraph = range.Paragraphs.Add()

paragraph1.Range.Text = "First Line of text"

paragraph1.Range.Font.Size = 30

//Add paragraph2 after paragraph 1
Dim paragraph2 As Paragraph = paragraph1.Range.Paragraphs.Add()

paragraph2.Range.Text = "Second line of text"

paragraph2.Range.Font.Size = 50