Skip to main content

This article explains how to extract error message text from an OLE Exception object.

Problem:

We need a way to extract text from an OLE Exception object. A Net Express program reads an Excel spreadsheet using OLE. It works great until it encounters an error of some sort. The exception manager is set up to do a call back. The call back works and displays the Excel error message. The question is instead of displaying the error message, how can the error text be moved to a COBOL data item?

The following is the callback code which is copied from a Net Express example:

callback section.

entry "onOleException" using by reference lnkErrorObject

by reference lnkErrorNumber

by reference lnkErrorText.

display "Excel had returned an error..."

display "The COBOL exception number was: " lnkErrorNumber

invoke lnkErrorObject "display"

display " "

if lnkErrorText not = null

invoke lnkErrorText "display" *> This is the code which displays the error message.

display " "

end-if

goback.

Resolution:

Try the following:

Add these data items to working-storage.

01 anindex pic 9(9) comp-5 value 1.

01 acobolpicx object reference.

01 errormessage pic x(255).

Add the following code to the procedure division.

invoke lnkErrorText "at"

using anIndex

returning acobolpicx

end-invoke

invoke acobolpicx "getValue"

returning errormessage

end-invoke

display errormessage

-------------------------------------------------------------------------

Description as to how this works.

LnkErrorText is an object of type orderedcollection.

The first element in the collection is an object of type cobolpicx.

The following will extract the cobolpicx object from the collection:

invoke lnkErrorText "at"

using anIndex

returning acobolpicx

end-invoke

The following will extract the error string from the cobolpicx object and will place it in a pic x field.

invoke acobolpicx "getValue"

returning errormessage

end-invoke

Once the string is extracted it can be moved to your data item.

display errormessage

Incident Number: 2263357

Old KB# 14911