Val函数并不能返回以字符串形式保存的表达式的值,在AutoCADVBA中可以用以下代码,但调用时,字符串形式的表达式必须是合法的数学表达式.
Sub CalExample()
MsgBox CalExp("20-5*9")
End Sub
Public Function CalExp(sExp As String) As Double
Dim Temp As Double
Temp = ThisDrawing.GetVariable("userr1")
ThisDrawing.SendCommand "setvar userr1 " & "'cal " & sExp & vbCr
CalExp = ThisDrawing.GetVariable("userr1")
ThisDrawing.SetVariable "userr1", Temp
End Function