AnsweredAssumed Answered

How to catch errors using Application.Evaluate

Question asked by Andreas Killer on Oct 17, 2019
Latest reply on Oct 21, 2019 by Josh Brady



I have a userform with textboxes and the user can write expressions like 1+2 into the textboxes. I call Application.Evaluate to calulate the expression, get the result and process further. That works if the expression i valid, but if the user write an invalid expression into a textbox, the result is 0!


How can I catch that and get noticed that the expression can not be calculated?

Is there an official documentation available for that function? I can not find it.





Sub Test()
  'Function Evaluate(ExpressionString As String, simpleEquation As Boolean, compileOnly As Boolean) As Double
  '    Element von SolidWorks.Application
  '    Evalutes ExpressionString using the VBA engine

  Dim Expression As String
  Dim Result As Double
  Expression = "1+2*3"
  Result = Application.Evaluate(Expression, True, False)
  Debug.Print Result
  'Invalid expression:
  Expression = "x/12"
  Result = Application.Evaluate(Expression, True, False)
  'How can I know here that the expression could not be evaluated?
  Debug.Print Result
End Sub