|
不知道楼主的代码是什么作用?
是自动设置标题吗?
能否帮忙优化一下下面的代码?或者没有必要优化了?
- Sub 设置公文格式()
- '将全文逐段设置格式为"正文GB",并修改章节条的格式
- Application.ScreenUpdating = False
- Dim t: t = Timer
- If ActiveDocument.Fields.Count <> 0 Then Call FieUnlink '去除域代码
- Dim regEx As New RegExp '构建正则
- regEx.IgnoreCase = True: regEx.Global = False: regEx.MultiLine = False
- Dim idx
- For Each idx In ActiveDocument.Paragraphs '按段落循环
- If Not idx.Range.Information(wdWithInTable) Then '判断非表格内
- idx.Range.Style = ActiveDocument.Styles("公文正文") '样式名称
- Dim patt, mStyle, bSp, bArticle
- patt = Array("( :^[ ]*)(第[零〇一二三四五六七八九十百\d]+条)( :[ ]*)([^ ][^\r]*)", _
- "( :^[ ]*)(第[零〇一二三四五六七八九十百\d]+章)( :[ ]*)([^ ][^\r]*)", _
- "( :^[ ]*)(第[零〇一二三四五六七八九十百\d]+节)( :[ ]*)([^ ][^\r]*)")
- mStyle = Array("公文正文", "公文1级", "公文2级")
- bSp = Array(True, True, True)
- bArticle = Array(True, False, False)
- Dim i%
- For i = 0 To UBound(patt)
- regEx.Pattern = patt(i)
- Dim Index$, Subject$, m%, n%, oRang As Range
- If regEx.Test(idx.Range) Then
- Index = regEx.Execute(idx.Range)(0).SubMatches(0) '序号
- Subject = IIf(bSp(i), Replace(Replace(regEx.Execute(idx.Range)(0).SubMatches(1), " ", ""), " ", ""), regEx.Execute(idx.Range)(0).SubMatches(1)) '标题是否去掉空格
- m = regEx.Execute(idx.Range)(0).FirstIndex: n = regEx.Execute(idx.Range)(0).length
- Set oRang = ActiveDocument.Range(idx.Range.Start + m, idx.Range.Start + m + n)
- oRang.Text = Index & " " & Subject
- oRang.Style = ActiveDocument.Styles(mStyle(i)) '样式名称
- If bArticle(i) Then
- Set oRang = ActiveDocument.Range(oRang.Start, oRang.Start + Len(Index))
- oRang.Font.Bold = True
- End If
- End If
- Index = "": Subject = "": m = 0: n = 0: Set oRang = Nothing
- Next i
- End If
- Next
- Application.ScreenUpdating = True
- ActiveDocument.Activate
- End Sub
复制代码 |
|