|
结合楼主与tangqingfu兄的附件文档试写了个宏,只做了简单测试。对中英文字符只做了简单判断,有5个与6个选项的情形不作处理。假设段落与字符格式常规。
- Sub test()
- Dim i%, n%, wid!
- Dim myRange As Range
- Dim oRange As Range
-
- With ActiveDocument.PageSetup
- wid = .PageWidth - .LeftMargin - .RightMargin - .Gutter
- End With
-
- With ActiveDocument.Content.Find
- .Text = "[^13^s^t. ][A-F][..]"
- .MatchWildcards = True
- Do While .Execute
- With .Parent
- Set oRange = .Duplicate
- If .Characters(2) = "A" Then
- Set oRange = .Duplicate
- If myRange Is Nothing Then
- Set myRange = oRange.Characters(2)
- Else
- myRange.End = .Start
- myRange.End = FindLastOption(myRange.Duplicate).End
- OptionsSetting i, wid, myRange
- Set myRange = oRange.Characters(2)
- End If
- i = 0
- n = n + 1
- End If
- End With
- i = i + 1
- Loop
- myRange.End = oRange.End
- myRange.End = FindLastOption(myRange.Duplicate).End
- OptionsSetting i, wid, myRange
- End With
- MsgBox "共处理了" & n & "题。"
- End Sub
- Function FindLastOption(aRange As Range) As Range
- With aRange.Find
- .Text = "[^13^s^t ][B-F][..]"
- .MatchWildcards = True
- .Forward = False
- If .Execute Then
- Set FindLastOption = .Parent
- FindLastOption.EndOf wdParagraph, 1
- Else
- MsgBox "异常选项!请检查文档内容"
- End If
- End With
- End Function
- Sub OptionsSetting(i As Integer, wid As Single, aRange As Range)
- Dim n%, s%, t%, st&, aIndent!, pos!, info$()
- Dim TF As Boolean
- Dim Reg As Object
- Dim Match As Object
-
- With aRange.Paragraphs.First
- aIndent = .LeftIndent + .FirstLineIndent
- End With
- Set Reg = CreateObject("VBScript.RegExp")
- With Reg
- .Global = True
- .MultiLine = True
- .Pattern = "(\b[A-F][..][^\r]+?)[\s ]*?(?=[A-F][..]|$)"
- For Each Match In .Execute(aRange.Text)
- ReDim Preserve info(n)
- info(n) = Match.submatches(0)
- If Len(info(n)) > s Then s = Len(info(n))
- n = n + 1
- Next
- End With
-
- Application.ScreenUpdating = False
- With aRange
- .End = .End - 1
- st = .Start - .Paragraphs(1).Range.Start
- If st > 0 Then aIndent = aIndent + st * .Font.Size
- Reg.Pattern = "[\u4e00-\u9fa5]"
- If Reg.test(.Text) Then TF = True
- If TF = True Then t = 1 Else t = 2
- .ParagraphFormat.TabStops.ClearAll
- Select Case i
- Case 4
- If (wid - aIndent) / 4 * t > (s + 1) * .Font.Size Then
- pos = (wid - aIndent) / 4
- For n = 1 To 3
- .ParagraphFormat.TabStops.Add pos * n + aIndent
- Next
- .Text = Join(info, vbTab)
- ElseIf (wid - aIndent) / 2 * t > (s + 1) * .Font.Size Then
- pos = (wid - aIndent) / 2 + .Font.Size
- For n = 1 To 2
- .ParagraphFormat.TabStops.Add pos * n + aIndent
- Next
- .Text = Join(info, vbTab)
- Reg.Pattern = "(\t[^\t]+)\t([\w\W]+$)"
- .Text = Reg.Replace(.Text, "$1" & Chr(13) & "$2")
- If st > 0 Then .Paragraphs(2).Range.InsertBefore String(st, .Paragraphs(1).Range.Characters(1))
- End If
- Case 3
- If (wid - aIndent) / 3 * t > (s + 1) * .Font.Size Then
- pos = (wid - aIndent) / 3
- For n = 1 To 2
- .ParagraphFormat.TabStops.Add pos * n + aIndent
- Next
- .Text = Join(info, vbTab)
- ElseIf (wid - aIndent) / 2 * t > (s + 1) * .Font.Size Then
- pos = (wid - aIndent) / 2 + .Font.Size
- .ParagraphFormat.TabStops.Add pos + aIndent
- .Text = Join(info, vbTab)
- Reg.Pattern = "(\t[^\t]+)\t([\w\W]+$)"
- .Text = Reg.Replace(.Text, "$1" & Chr(13) & "$2")
- If st > 0 Then .Paragraphs(2).Range.InsertBefore String(st, .Paragraphs(1).Range.Characters(1))
- End If
- Case 2
- If (wid - aIndent) / 2 * t > (s + 1) * .Font.Size Then
- pos = (wid - aIndent) / 2
- .ParagraphFormat.TabStops.Add pos * n + aIndent
- .Text = Join(info, vbTab)
- End If
- Case Else
- '5、6个选项时自定义(略)
- End Select
- .HighlightColorIndex = wdYellow
- TF = False
- End With
- Application.ScreenUpdating = True
- End Sub
复制代码 |
|