|
楼主 |
发表于 2014-9-20 22:17
|
显示全部楼层
本帖最后由 zhanglei1371 于 2014-9-21 10:09 编辑
看来对VBA感兴趣的人还是不多啊,本人就亲自解决吧:- Sub sadf()
- Application.ScreenUpdating = 0
- l = ActiveDocument.PageSetup.CharsLine
- For Each pa In ActiveDocument.Paragraphs
- If InStr(pa, "D.") Then
- pa.Range.Select:
- If Len(pa) > l * 2 Then '长度超过两行就每个选项一行
- Call A3
- ElseIf Len(pa) < l Then '长度小于一行就所有选项一行
- Call a1(l - Len(pa.Range))
- Else '长度在两行内就AB一行,CD一行,E项单独一行
- Call a2
- End If
- End If
- Next
- Application.ScreenUpdating = 1
- End Sub
- Sub a1(n) '分1行
- s = Selection.End
- i = 1
- With Selection.Find
- .ClearFormatting
- .Text = "[ ]{1,}[B-E]."
- .MatchWildcards = 1
- Do While .Execute
- If .Parent.End > s Then Exit Sub
- ' If a > 1 Then
- .Parent.End = .Parent.End - 2
- .Parent = Chr(9)
- La = Selection.End - Selection.Paragraphs(1).Range.Start + n * i / 4
- Selection.ParagraphFormat.TabStops.Add Position:=CentimetersToPoints(La * 15 / 39)
- .Parent.Collapse 0
- i = i + 1
- Loop
- End With
- End Sub
- Sub a2() '分三行
- a = 1: i = 1
- s = Selection.End
- With Selection.Find
- .ClearFormatting
- .Text = "B.*C."
- .MatchWildcards = 1
- If .Execute Then
- ac = Len(.Parent) - 2 'B项长度
- ac = (39 - ac)
- .Parent.Collapse 1
- End If
- .Text = "D.*E."
- .MatchWildcards = 1
- If .Execute Then
- cd = Len(.Parent) - 2 'D项长度
- cd = (39 - cd)
- min = ac
- If min > cd Then min = cd - 7
- If min > 20 Then min = 20
- .Parent.HomeKey
- End If
- .Text = "[ ]{1,}[B-E]."
- .MatchWildcards = 1
- Do While .Execute
- If .Parent.End > s Then Exit Sub
- .Parent.End = .Parent.End - 2
- If a / 2 = Int(a / 2) Then
- .Parent = Chr(13) '若是CE则加入回车
- Else
- .Parent = Chr(9) '若是BD项则加入tab
- Selection.ParagraphFormat.TabStops.Add Position:=CentimetersToPoints(min * 15 / 39)
- End If
- a = a + 1
- .Parent.Collapse 0
- i = i + 1
- Loop
- End With
- End Sub
- Sub A3() '分5行
- s = Selection.End
- a = 1
- With Selection.Find
- .ClearFormatting
- .Text = "[A-E]."
- .MatchWildcards = 1
- Do While .Execute
- If .Parent.End > s Then Exit Sub
- If a > 1 Then
- .Parent = Chr(13) & .Parent
- .Parent.Collapse 0
- End If
- a = a + 1
- Loop
- End With
- End Sub
复制代码
|
|