|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 sandorn 于 2016-12-23 18:13 编辑
行政法规(4项).zip
(52.56 KB, 下载次数: 88)
为了收集法律法规,提高排版效率,学习高手例程,自己写了一段,请大家批评指正。
两种方法,一是全局匹配,二是逐段循环。主要作用是将 章节设置为预设好的样式,并且去掉章节标题中的空格,同时,将第X条加粗。
方法1:全局匹配公文
- Sub 全局匹配公文()
- '全局匹配,修改章节条格式
- Dim T: T = Timer
- If ActiveDocument.Fields.Count <> 0 Then Call FieUnlink '去除域代码
- Dim patt, mStyle, bSp, bArticle '正则表达式,样式名称,是否去除中空格,是否加粗$1
- patt = Array("(?:^[ ]*)(第[零〇一二三四五六七八九十百\d]+条)(?:[ ]*)([^ ][^\r]*)", _
- "(?:^[ ]*)(第[零〇一二三四五六七八九十百\d]+章)(?:[ ]*)([^ ][^\r]*)", _
- "(?:^[ ]*)(第[零〇一二三四五六七八九十百\d]+节)(?:[ ]*)([^ ][^\r]*)")
- mStyle = Array("公文正文", "公文1级", "公文2级")
- bSp = Array(True, True, True)
- bArticle = Array(True, False, False)
- Dim crang As Object, RegEx As New RegExp '构建正则对象
- RegEx.IgnoreCase = True: RegEx.Global = True: RegEx.MultiLine = True
- Set crang = ActiveDocument.Content
- crang.Style = ActiveDocument.Styles("公文正文") '将全文样式设置为"公文正文",此处会影响表格
- Dim i%
- For i = 0 To UBound(patt)
- RegEx.Pattern = patt(i) '标题正则表达式,捕获组必须为两个
- Dim mathss
- If RegEx.test(crang) Then Set mathss = RegEx.Execute(crang) '测试通过则全局匹配
- Dim j%, Index$, Subject$, m&, n&, oRang As Range
- For j = mathss.Count - 1 To 0 Step -1 '匹配结果倒序循环,避免去空格影响计数
- Index = mathss(j).SubMatches(0) '$1序号
- Subject = IIf(bSp(i), Replace(Replace(mathss(j).SubMatches(1), " ", ""), " ", ""), mathss(j).SubMatches(1)) '$2标题,是否去掉空格
- m = mathss(j).FirstIndex: n = mathss(j).Length '结果开始位置及长度
- Set oRang = ActiveDocument.Range(crang.Start + m, crang.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)) '选择$1内容
- oRang.Font.Bold = True
- End If
- Next j
- Next i
- MsgBox (Timer - T) * 1000
- End Sub
复制代码
方法2:逐段循环匹配公文
- Sub 段落匹配公文()
- '将全文逐段设置格式为"公文正文",并修改章节条的格式
- 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
- MsgBox (Timer - T) * 1000
- End Sub
复制代码 上面涉及到了一个去除域代码的语句,具体如下:
- Function FieUnlink()
- '删除域代码
- Dim a%, Fie As Field
- a = ActiveDocument.Fields.Count
- For Each Fie In ActiveDocument.Fields
- Fie.Unlink
- Next
- MsgBox a & "-->" & ActiveDocument.Fields.Count
- End Function
复制代码
上传了文档做为附件,可以测试下。
|
评分
-
1
查看全部评分
-
|