VBA操作WORD(七)设置每一级大纲式样

Sub 设置每一级样式格式()
    With ActiveDocument.Styles(wdStyleHeading1).Font
   	 .Color = wdColorBlack
   	 .Bold = False '标题不再加粗
    	.Size = 22 '二号
    	.Name = "方正小标宋简体"
    End With
    With ActiveDocument.Styles(wdStyleHeading2).Font
    	.Color = wdColorBlack
    	.Bold = False '黑体一般不再加粗
    	.Size = 16 '三号
    	.Name = "黑体"
    End With
    With ActiveDocument.Styles(wdStyleHeading3).Font
    	.Color = wdColorBlack
   	 .Bold = True
   	 .Size = 16
    	.Name = "楷体_GB2312"
    End With
    With ActiveDocument.Styles(wdStyleHeading4).Font
    	.Color = wdColorBlack
    	.Bold = True
   	 .Size = 16
   	 .Name = "仿宋_GB2312"
    End With
    With ActiveDocument.Styles(wdStyleHeading5).Font
    	.Color = wdColorBlack
   	 .Bold = False
    	.Size = 16
    	.Name = "仿宋_GB2312"
    End With
    With ActiveDocument.Styles(wdStyleNormal).Font    '设置正文普通文字格式
    	.Color = wdColorBlack
   	 .Bold = False
    	.Size = 16
    	.Name = "仿宋_GB2312"
    End With
End Sub

调用类似

.Range.Style = wdStyleNormal '注意:设置为模板,如果将内容复制到其他word,会变成目标word所设置wdStyleNormal式样,和源不同。

但是有两个弊端,一是将内容复制到另外一份word文档的时候,会变成目标word所设置的式样。二是大纲标题有点,不适合公文等场合(或许可以设置去掉,不过我不知道)。

于是便手工设置:

Sub 设置每一级标题格式(ib As Paragraph)
'注意:标题后面可能不换行直接跟着内容,所以对第一句设置,而不是整段。
'注意:如果设置为模板,将内容复制到其他word,会变成目标word所设置wdStyleNormal式样,和源不同。
'规则:如果只有一句话,才设置大纲级别,否则不设置。
    If ib.Range Like "[一二三四五六七八九十百零千]、*" Or ib.Range Like "[一二三四五六七八九十百零千]、*。*" Then
    '类似 一、开头的二级标题
        If ib.Range.Sentences.Count = 1 Then
            'ib.Range.Style = wdStyleHeading2
            ib.OutlineLevel = wdOutlineLevel2
        End If
        With ib.Range.Sentences(1).Font 'ib.Range.Font
            .ColorIndex = wdColorBlack
            .Bold = False '黑体一般不再加粗
            .Name = "黑体"
            .Size = 16
        End With
    ElseIf ib.Range Like "([一二三四五六七八九十百零千])*" Then
    '类似 (一)开头的三级标题
        If ib.Range.Sentences.Count = 1 Then
            'ib.Range.Style = wdStyleHeading3
            ib.OutlineLevel = wdOutlineLevel3
        End If
        With ib.Range.Sentences(1).Font
            .ColorIndex = wdColorBlack
            .Bold = True
            .Name = "楷体_GB2312"
            .Size = 16
        End With
    ElseIf ib.Range Like "[0-9][、..]*" Then
    '类似 1、或1.开头的四级标题
        If ib.Range.Sentences.Count = 1 Then
            'ib.Range.Style = wdStyleHeading4
            ib.OutlineLevel = wdOutlineLevel4
        End If
        With ib.Range.Sentences(1).Font
            .ColorIndex = wdColorBlack
            .Bold = True
            .Name = "仿宋_GB2312"
            .Size = 16
        End With
    ElseIf ib.Range Like "([0-9])*" Then
    '类似(1)开头的五级标题
        If ib.Range.Sentences.Count = 1 Then
            'ib.Range.Style = wdStyleHeading5
            ib.OutlineLevel = wdOutlineLevel5
        End If
        With ib.Range.Sentences(1).Font
            .ColorIndex = wdColorBlack
            .Bold = True
            .Name = "仿宋_GB2312"
            .Size = 16
        End With
    End If
End Sub

你可能感兴趣的:(word,VBA,word,开发语言,vba,办公自动化,大纲格式)