|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
针对楼上代码,一测试马上出错,的确如楼主所说!遂经过F8逐语句反复测试(从没这么认真过),发现查找范围跑出所选,最后灵机一动,巧妙地把m2、m3替换为m2`、m3`,终于测试成功(并观察总字数没有变化,说明宏是安全的)!但也建议,楼主在应用我的宏(包括任何宏)时,要备份原文件后再应用。这回可以全选,也可以选定任意文字(不必是段落)后应用此宏,并且查找语句也精简为一条,添加了查找m2/m3的次数统计。
Sub 设置上标()
Dim myRange As Range, i As Long, j As Long
If Selection.Type = wdSelectionIP Then Selection.WholeStory
Set myRange = Selection.Range
myRange.Find.Execute FindText:="^", ReplaceWith:="", Replace:=wdReplaceAll
myRange.Find.Execute FindText:="m2", ReplaceWith:="m2`", Replace:=wdReplaceAll
myRange.Find.Execute FindText:="m3", ReplaceWith:="m3`", Replace:=wdReplaceAll
Do
Selection.Find.Execute FindText:="m2`", Forward:=True
If Selection.Find.Found = False Then Exit Do
If Selection.Find.Found = True Then i = i + 1
Selection.MoveStart Unit:=wdCharacter, Count:=1
Selection.Font.Color = wdColorRed
Selection.Font.Superscript = True
Selection.MoveRight Unit:=wdCharacter, Count:=1
Loop
myRange.Select
Do
Selection.Find.Execute FindText:="m3`", Forward:=True
If Selection.Find.Found = False Then Exit Do
If Selection.Find.Found = True Then j = j + 1
Selection.MoveStart Unit:=wdCharacter, Count:=1
Selection.Font.Color = wdColorRed
Selection.Font.Superscript = True
Selection.MoveRight Unit:=wdCharacter, Count:=1
Loop
myRange.Find.Execute FindText:="`", ReplaceWith:="", Replace:=wdReplaceAll
myRange.Select
MsgBox "处理完毕!(共处理" & i & "个m2," & j & "个m3)", vbOKOnly + vbExclamation, "设置上标"
End Sub |
|