前面介绍Range对象的字体设置时讲了,文本的字体设置可以有2种方式,一种是直接利用Range对象提供的字体设置相关的属性和方法,另一种是利用Range对象的Font属性得到得到Font对象,然后利用Font对象的属性和方法进行字体设置。
现在对段落的格式进行设置,对应的也是有2种方法,一种是直接使用Paragraph对象提供的相关属性和方法进行设置,另一种是用Format属性得到ParagraphFormat对象,然后通过设置该对象的属性和方法来实现。
下面打开文档test3.docx,对第2个段落进行格式设置。首先使用第一种方法,直接使用Paragraph对象的相关属性和方法进行设置。
code.vba
Sub Test()
Dim doc As Document
Set doc = Documents.Open("D:\test2.docx")
Dim pg As Paragraph
Set pg = doc.Paragraphs(2)
pg.IndentCharWidth 4
pg.Space2
End Sub
设置效果如图4-3所示。
图4-7 设置段落的格式
下面使用Paragraph对象的Format属性获取ParagraphFormat对象,使用它的属性和方法设置段落格式。
code.vba
Sub Test2()
Dim doc As Document
Set doc = Documents.Open("D:\test2.docx")
Dim pg As Paragraph
Set pg = doc.Paragraphs(2)
Dim pgf As ParagraphFormat
Set pgf = pg.Format
pgf.IndentCharWidth 4
pgf.Space2
End Sub
得到与图4-3相同的设置效果。