作为轻度word使用者,在写文件的过程中,老是会碰到需要手动去调整目录结构的情况,经常这么去操作的话就会非常的浪费时间,于是,就用vb写了下自动识别大纲。
由于我的目录只要求一、(一)及1、,因此识别的也只是这几个目录
原图片.png可以看到左边的文档结构图是空着的。
运行宏后
运行后.png自动生成目录的效果
代码见下
Sub 大纲()
Dim pg As Paragraph, r As Range
For Each pg In ActiveDocument.Paragraphs
pgtext = pg.Range.Text
title_name = Left(pgtext, 2)
title_name2 = Left(pgtext, 3)
If Len(pgtext) > 20 Then
title_name = ""
title_name2 = ""
End If
If (title_name = "一、" Or title_name = "二、" Or title_name = "三、" Or title_name = "四、" Or title_name = "五、" Or title_name = "六、" Or title_name = "七、" Or title_name = "八、" Or title_name = "九、" Or title_name = "十、") Then
pg.OutlineLevel = wdOutlineLevel1 'class为样式名
ElseIf (title_name2 = "(一)" Or title_name2 = "(二)" Or title_name2 = "(三)" Or title_name2 = "(四)" Or title_name2 = "(五)" Or title_name2 = "(六)" Or title_name2 = "(七)" Or title_name2 = "(八)" Or title_name2 = "(九)" Or title_name2 = "(十)") Then
pg.OutlineLevel = wdOutlineLevel2
ElseIf (title_name = "1、" Or title_name = "2、" Or title_name = "3、" Or title_name = "4、" Or title_name = "5、" Or title_name = "6、" Or title_name = "7、" Or title_name = "8、" Or title_name = "9、") Then
pg.OutlineLevel = wdOutlineLevel3
ElseIf (title_name = "1." Or title_name = "2." Or title_name = "3." Or title_name = "4." Or title_name = "5." Or title_name = "6." Or title_name = "7." Or title_name = "8." Or title_name = "9.") Then
pg.OutlineLevel = wdOutlineLevel3
End If
Next
End Sub
网友评论