Excel VBA - Assigning a might be not existing content to a variable

Dear Experts,

Actually writing a code with SAP scripting (a special scripting language for SAP application) in VBA and got to an issue, could you please advise in it?

I would like to assign a content to the variable ErrMes with the following line,

ErrMes = session.findById("wnd[2]/usr/txtMESSTXT1").Text

Open in new window

which is basically a message text on Window 2 in SAP application (wnd[2]), but that might not appear at all in certain scenarios.

Briefly the issue is that if that does not appear in the application then there is nothing to assign to the variable and the VBA code stops. Do you have idea eventually how to handle those exception cases?

Thanks in advance,
csehzIT consultantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

That is what error handling or trapping is all about.
This is a simple example:

    On Error Goto ErrHandler:
    ErrMes = session.findById("wnd[2]/usr/txtMESSTXT1").Text ' may cause an error
    ' more code 
    ' error handling code such as a message box notifying the user about the error
    Resume Next
    End Sub 

Open in new window

On Error Goto ErrHandler
tells the code to skip to the ErrHandler label when an error occurs.
The ErrHandler label (or whatever you choose to name it) is typically written as the last code block of a subroutine or function.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NorieAnalyst Assistant Commented:
When the code stops do you get an error message?

How is ErrMes declared?

Have you considered something like this?
Set objMes = session.findById("wnd[2]/usr/txtMESSTXT1").Text

If obJMes Is Nothing Then ' message not found
    MsgBox "Message not found"
   ErrMes = objMes.Text
End If

Open in new window

csehzIT consultantAuthor Commented:
Thank you for the answers
NorieAnalyst Assistant Commented:
Oops, my code was slightly incorrect.

Here's the corrected version.
Set objMes = session.findById("wnd[2]/usr/txtMESSTXT1")

If obJMes Is Nothing Then ' message not found
    MsgBox "Message not found"
   ErrMes = objMes.Text
End If

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.