注意,你的处理后的文档,有一个标点符号漏标。另外,应该在标点符号后面设置上下标。(按实例) 请按代码头提示进行粘贴和运行以下代码: '* +++++++++++++++++++++++++++++
'* Created By I Love You_Word!@ExcelHome 2005-2-22 07:28:29
'仅测试于System: Windows NT Word: 10.0 Language: 2052
'^The Code CopyIn [ThisDocument-ThisDocument]^'
'* -----------------------------Sub InterpunctionCount()
Dim MyIptBox As String, MyArray() As String, aArray As Variant, N As Integer
Dim BinString As String, I As Range, UpDown As String
N = 0: BinString = "011000100111001010101100" '初始化变量
MyIptBox = InputBox("请输入您想要查找/统计的标点符号,以/(斜杠)号分隔!")
If MyIptBox = "" Then Exit Sub '如果为""或者按下取消键则退出程序
MyArray = VBA.Split(MyIptBox, "/") '以"/"分隔取得数组
Application.ScreenUpdating = False '关闭屏幕更新
For Each aArray In MyArray '在数组中循环
With ActiveDocument.Content.Find '查找指定的标点符号数量
.ClearFormatting '清除查找格式
Do While .Execute(FindText:=aArray, Forward:=True, MatchCase:=True) = True
N = N + 1 '累加
Loop
End With
Next
' MsgBox N
If N < Len(BinString) Then MsgBox "此操作不可完成!", vbOKOnly + vbCritical, "Warning" _
: Exit Sub
For Each I In ActiveDocument.Words '在词中循环并着色
If InStr(MyIptBox, I) > 0 Then I.Bold = True: I.Font.Color = wdColorRed
Next
N = 0
Selection.HomeKey wdStory '移到文档首位置
With Selection.Find '设置查找条件
.ClearFormatting '清除查找格式
.Font.Color = wdColorRed '设置为红色
.Font.Bold = True '设置为粗本
.Text = ""
While .Execute
N = N + 1 '累加
UpDown = Mid(BinString, N, 1)
If N > Len(BinString) Then '当超过二进制文本长度时
With .Replacement '设置一次性替换
.ClearFormatting '清除替换格式
.Font.Bold = False '替换为常规字体
.Font.Color = wdColorAutomatic '自动颜色(黑色)
End With
.Execute FindText:="", ReplaceWith:="", Format:=True, _
Replace:=wdReplaceAll '将剩余格式恢复
Application.ScreenUpdating = True '恢复屏幕更新
End '终止程序
End If
With Selection
.InsertAfter UpDown '插入指定的二进制值
.Font.Bold = False '恢复常规字体
.Font.Color = wdColorAutomatic '自动颜色
'如果为1则为下标,如果为0则为上标
If UpDown = 1 Then .Characters(2).Font.Subscript = True _
Else .Characters(2).Font.Superscript = True
.Characters(2).Font.Size = 15 '设置字号
.HomeKey wdStory '移到文档首
End With
Wend
End With
Application.ScreenUpdating = True '恢复屏幕更新
End Sub
'----------------------
检查一下是否有问题,再作交流,包括检查是否有上下标的事宜,宜在此基础上深入。 由于使用大量的单个查找与替换,以及在词循环,以及反复插入与字体设置,受CPU和内存影响,运行速度及代码效率不是很高(也许有更好地办法),此程序运行需要一定时间。 |