在此借用守柔老师的CalValue宏,表谢意,以下解决部分符号替换问题,请多指正! Sub 计算() Dim Mycal As Single, Myrange As Range, Er
On Error Resume Next Err.Clear
Er = (Right(Selection, 1) * 1)
If Err.Number = 13 Then '此判断主要针对选定过程中容易选中段落符,否则可省略。
Set Myrange = ActiveDocument.Range(Start:=Selection.Start, End:=Selection.End - 1)
Else
Set Myrange = ActiveDocument.Range(Start:=Selection.Start, End:=Selection.End)
End If
Application.ScreenUpdating = False
With Myrange
'将原表达式拷贝剪贴板
.Copy '复制到原表达式之后
'ActiveDocument.Range(Start:=Selection.Start, End:=Selection.End).Copy '复制到原表达式下一行 '将表达式中×÷中括号大括号替换为小括号
Myrange = Replace(Myrange, "×", "*")
Myrange = Replace(Myrange, "÷", "/")
Myrange = Replace(Myrange, "[", "(")
Myrange = Replace(Myrange, "]", ")")
Myrange = Replace(Myrange, "{", "(")
Myrange = Replace(Myrange, "}", ")") Mycal = .Calculate
.Paste '将剪贴板内容覆盖原表达式
.InsertAfter "=" & Mycal
End With
Application.ScreenUpdating = True
End Sub |