|
本帖最后由 duquancai 于 2018-4-15 14:20 编辑
承蒙多次邀请!亦或今天周末有点时间,写了一下代码,也没时间逐字逐句斟酌!仅供一个思路!
目前代码:对“单音节、双音节、三音节、四音节”进行了排序!更多的“五音节、六音节......”,可扩展。- Sub test()
- Dim S As Range, p As Paragraph, nDoc As Document
- Dim reg As Object, d As Object, mt, sr$, k, mySortKey$(), myK
- Set S = IIf(Selection.Type = wdSelectionIP, ActiveDocument.Content, Selection.Range)
- If S = ActiveDocument.Content Then
- AB = MsgBox("要进行全文排序吗?", vbYesNoCancel + vbQuestion, "全文处理判断")
- If AB <> vbYes Then Exit Sub
- End If
- Set reg = CreateObject("VBScript.Regexp")
- reg.Global = True: reg.Pattern = "\u2022\u02c8?"
- Set d = CreateObject("Scripting.Dictionary")
- For Each p In S.Paragraphs
- n = n + 1
- If f(p.Range.Text, "/[^/一-﨩]+/") Then
- Select Case reg.Execute(p.Range.Text).Count
- Case Is = 0 '单音节
- d("A") = d("A") & "," & n
- Case Is = 1 '双音节
- sr = reg.Execute(p.Range.Text)(0).Value
- If Not f(sr, "\u02c8") Then
- d("B") = d("B") & "," & n
- Else
- d("BA") = d("BA") & "," & n
- End If
- Case Is = 2 '三音节
- Set mt = reg.Execute(p.Range.Text)
- For i = 1 To mt.Count
- If f(mt(i - 1).Value, "\u02c8") Then Exit For
- Next
- Select Case i
- Case Is = 3
- d("C") = d("C") & "," & n
- Case Is = 1
- d("CA") = d("CA") & "," & n
- Case Is = 2
- d("CB") = d("CB") & "," & n
- End Select
- Case Is = 3 '四音节
- Set mt = reg.Execute(p.Range.Text)
- For i = 1 To mt.Count
- If f(mt(i - 1).Value, "\u02c8") Then Exit For
- Next
- Select Case i
- Case Is = 4
- d("D") = d("D") & "," & n
- Case Is = 1
- d("DA") = d("DA") & "," & n
- Case Is = 2
- d("DB") = d("DB") & "," & n
- Case Is = 3
- d("DC") = d("DC") & "," & n
- End Select
- End Select
- End If
- Next
- k = d.keys: ReDim mySortKey(1 To d.Count)
- For i = 0 To UBound(k)
- mySortKey(i + 1) = k(i)
- Next
- WordBasic.SortArray mySortKey()
- Set nDoc = Documents.Add
- For Each myK In mySortKey
- For Each t In Split(Mid(d(myK), 2), ",")
- With nDoc.Bookmarks("\EndOfDoc").Range
- .FormattedText = S.Paragraphs(t).Range.FormattedText
- End With
- Next
- Next
- End Sub
- Function f(ByVal sr As String, ByVal r As String) As Boolean
- Dim reg As Object
- Set reg = CreateObject("VBScript.Regexp")
- reg.Pattern = r
- If reg.test(sr) Then f = True
- End Function
复制代码 |
评分
-
1
查看全部评分
-
|