|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
是较复杂,感觉文本不是很规范,特别是标点符号与断句。可试试如下代码,有些段落未能匹配。已有简单说明,可自行修改代码以尽量适应实际文档- Private num(0 To 99) As String
- Sub test()
- Dim i%, j%, k%, n1%, n2%, r%, s%
- Dim data$, code$, info$(), info1$(), info2$(), data2$()
- Dim RegExp As Object
- Dim Match As Object
- Dim RegExp2 As Object
- Dim Match2 As Object
-
- data = IIf(Selection.Type = wdSelectionIP, ActiveDocument.Content.Text, Selection.Text)
- code = "斤两钱分厘枚粒片" '数量单位据此先后排序
- Set RegExp = CreateObject("VBScript.RegExp")
- Set RegExp2 = CreateObject("VBScript.RegExp")
- With RegExp
- .Global = True
- .Pattern = "[用方]:([^。:]+。)" '特征文本:用字加冒号开头,同一段落至句号结束
- For Each Match In .Execute(data)
- ReDim info(Len(code))
- With RegExp2
- .Global = True
- For i = 1 To Len(code)
-
- '提取用于排序的文本特征,暂定数量及单位后须跟顿号或句号
- .Pattern = "([^、。]+?([十九八七六五四三二一半]+)" & Mid(code, i, 1) & ")[、。]"
- If .test(Match.submatches(0)) = True Then
-
- For Each Match2 In .Execute(Match.submatches(0))
- info(i - 1) = info(i - 1) & Match2.Value
- ReDim Preserve info1(1, n1)
- info1(0, n1) = Match2.submatches(0)
- info1(1, n1) = Match2.submatches(1)
- n1 = n1 + 1
- Next
-
- For k = 99 To 0 Step -1
- For j = 0 To UBound(info1, 2)
- If info1(1, j) = num(k) Then
- ReDim Preserve info2(n2)
- info2(n2) = info1(0, j)
- n2 = n2 + 1
- End If
- Next
- Next
- ReDim Preserve data2(r)
- data2(r) = data2(r) & Join(info2, "、")
- r = r + 1
-
- Erase info1
- Erase info2
- n1 = 0
- n2 = 0
- End If
- Next
- End With
-
- With Match
- data = Left(data, .firstindex + 2) & Join(data2, "、") & Mid(data, .firstindex + .Length)
- End With
-
- Erase info
- Erase data2
- r = 0
- Next
- For i = 1 To Len(code) '用于处理判断为相同数量及单位的药
- RegExp2.Pattern = "([^、。]+?)各?([十九八七六五四三二一半]+" & Mid(code, i, 1) & ")[、。]([^、。]+?)\2([、。])"
- If InStr(data, Mid(code, i, 1)) Then
- Do While RegExp2.test(data) = True
- data = RegExp2.Replace(data, "$1,$3各$2$4")
- s = s + 1
- Loop
- End If
- Next
- Documents.Add(ActiveDocument.FullName).Content.Text = data
- End With
- End Sub
- Sub NumToCnNum()
- '生成百位内汉字小写数字
- Const a As String = "一二三四五六七八九"
- Dim i%, n%
-
- For i = 1 To 99
- Select Case i
- Case Is >= 20
- If i Mod 10 > 0 Then
- num(i) = Mid(a, Int(i / 10), 1) & "十" & Mid(a, i Mod 10, 1)
- Else
- num(i) = Mid(a, Int(i / 10), 1) & "十"
- End If
- Case Is > 10
- num(i) = "十" & Mid(a, Int(i Mod 10), 1)
- Case Is < 10
- num(i) = Mid(a, i, 1)
- Case Else
- num(i) = "十"
- End Select
- Next
- num(0) = "半"
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|