Error Handling

R

Rhino

Is it possible to make a macro stop executing and give a non-zero return
code back to the VBScript that is executing it?

I'm just putting the final polish on my first serious macro. Basically, I
have a VBScript that invokes Word invisibly and invokes a macro that reads a
text file and builds a resume document using the data that is in the text
file.

There is one situation in which the input in the text file may not be
usable. If that happens, I want the main macro to exit when I detect that
situation and I need the VBScript to be able to tell that the macro did not
complete normally but had to abort due to the problem. Programming languages
often use return codes, usually numeric, to indicate that an error caused
the program to abort.

I'd like the main macro to exit with a non-zero return code if there is a
serious error; otherwise, I want it to complete normally with a zero return
code.

Is that, or anything similar, possible in VBA?

Also, assuming it is possible to return different codes from a VBA macro,
can anyone tell me how to make my VBScript check the return code that VBA
gives? This is my VBScript in its entirety:

------------------------------------------------------------------------------------------
' Get arguments into variables
If WScript.Arguments.Count > 0 Then
MsgBox "Too many arguments; expecting none."
WScript.Quit
End If

' Find path for MyDocuments folder
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(&H5&)
strMyDocPath = objFolder.Self.Path

' Start Word Application, open TestDoc.doc in MyDocuments
Set oWd = CreateObject("Word.Application")
oWd.Visible = False
Set oDoc = oWd.Documents.Open(strMyDocPath & "\resume.doc")

'Run macro named createResumeFromFile, which has no arguments
oWd.Run "createResumeFromFile"

' Save changes to doc on closing and quit Word
oDoc.Save
oDoc.Close
oWd.Quit
Set oWd = Nothing

Set objShell = Nothing
------------------------------------------------------------------------------------------

I'm envisioning something like the following, inserted after the last line
of the VBScript:

'Get return code from macro
if (oWd.ReturnCode <> 0) then
exit(oWd.ReturnCode)
end if

In other words, regardless of the return code, proceed to save the document,
close the document, and exit Word. But if the macro ended with a non-zero
return code, end the script itself with the same non-zero return code that
the macro ended with.

Can anyone enlighten me on how to do this or point me to a more appropriate
newsgroup for VBScript questions?
 
J

Jezebel

Write the macro as a function, and return whatever value you like.

Public Function MyMacro() as boolean

DO whatever ...

If [ErrorCondition] then
MyMacro = FALSE
Else
MyMacro = TRUE
End if

End Function


Alternatively, your macro can raise an error and return error information
like any other function ...

Public Sub MyMacro()

DO whatever ...

If [Error Condition] then
Err.Raise Number:=vbObjectError, Source:="MyMacro",
Description:="Text file was unusable"
End if

End Sub
 
R

Rhino

Hmm.

I lean toward the second alternative where the macro is raising an error.
Unfortunately, Word is supposed to be running invisibly since no user
interaction is required in the creation of the resume and Err.Raise causes a
debug/error dialog to popup. I don't really want that.

Is there any way for the macro to return an error code (ideally including
the source, description, etc.) _without_ displaying the debug/error dialog?

Or is writing the main macro as a function my only option?

--
Rhino


Jezebel said:
Write the macro as a function, and return whatever value you like.

Public Function MyMacro() as boolean

DO whatever ...

If [ErrorCondition] then
MyMacro = FALSE
Else
MyMacro = TRUE
End if

End Function


Alternatively, your macro can raise an error and return error information
like any other function ...

Public Sub MyMacro()

DO whatever ...

If [Error Condition] then
Err.Raise Number:=vbObjectError, Source:="MyMacro",
Description:="Text file was unusable"
End if

End Sub
 
J

Jezebel

Use 'On error resume next' in your calling code, and test the err object on
return.


Rhino said:
Hmm.

I lean toward the second alternative where the macro is raising an error.
Unfortunately, Word is supposed to be running invisibly since no user
interaction is required in the creation of the resume and Err.Raise causes
a debug/error dialog to popup. I don't really want that.

Is there any way for the macro to return an error code (ideally including
the source, description, etc.) _without_ displaying the debug/error
dialog?

Or is writing the main macro as a function my only option?

--
Rhino


Jezebel said:
Write the macro as a function, and return whatever value you like.

Public Function MyMacro() as boolean

DO whatever ...

If [ErrorCondition] then
MyMacro = FALSE
Else
MyMacro = TRUE
End if

End Function


Alternatively, your macro can raise an error and return error information
like any other function ...

Public Sub MyMacro()

DO whatever ...

If [Error Condition] then
Err.Raise Number:=vbObjectError, Source:="MyMacro",
Description:="Text file was unusable"
End if

End Sub







Rhino said:
Is it possible to make a macro stop executing and give a non-zero return
code back to the VBScript that is executing it?

I'm just putting the final polish on my first serious macro. Basically,
I have a VBScript that invokes Word invisibly and invokes a macro that
reads a text file and builds a resume document using the data that is in
the text file.

There is one situation in which the input in the text file may not be
usable. If that happens, I want the main macro to exit when I detect
that situation and I need the VBScript to be able to tell that the macro
did not complete normally but had to abort due to the problem.
Programming languages often use return codes, usually numeric, to
indicate that an error caused the program to abort.

I'd like the main macro to exit with a non-zero return code if there is
a serious error; otherwise, I want it to complete normally with a zero
return code.

Is that, or anything similar, possible in VBA?

Also, assuming it is possible to return different codes from a VBA
macro, can anyone tell me how to make my VBScript check the return code
that VBA gives? This is my VBScript in its entirety:

------------------------------------------------------------------------------------------
' Get arguments into variables
If WScript.Arguments.Count > 0 Then
MsgBox "Too many arguments; expecting none."
WScript.Quit
End If

' Find path for MyDocuments folder
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(&H5&)
strMyDocPath = objFolder.Self.Path

' Start Word Application, open TestDoc.doc in MyDocuments
Set oWd = CreateObject("Word.Application")
oWd.Visible = False
Set oDoc = oWd.Documents.Open(strMyDocPath & "\resume.doc")

'Run macro named createResumeFromFile, which has no arguments
oWd.Run "createResumeFromFile"

' Save changes to doc on closing and quit Word
oDoc.Save
oDoc.Close
oWd.Quit
Set oWd = Nothing

Set objShell = Nothing
------------------------------------------------------------------------------------------

I'm envisioning something like the following, inserted after the last
line of the VBScript:

'Get return code from macro
if (oWd.ReturnCode <> 0) then
exit(oWd.ReturnCode)
end if

In other words, regardless of the return code, proceed to save the
document, close the document, and exit Word. But if the macro ended with
a non-zero return code, end the script itself with the same non-zero
return code that the macro ended with.

Can anyone enlighten me on how to do this or point me to a more
appropriate newsgroup for VBScript questions?
 
R

Rhino

Ok, I'll give that a whirl if I ever get a reply on the vbscript group on
how to check return codes in a vbscript.

Thanks!

Rhino

Jezebel said:
Use 'On error resume next' in your calling code, and test the err object
on return.


Rhino said:
Hmm.

I lean toward the second alternative where the macro is raising an error.
Unfortunately, Word is supposed to be running invisibly since no user
interaction is required in the creation of the resume and Err.Raise
causes a debug/error dialog to popup. I don't really want that.

Is there any way for the macro to return an error code (ideally including
the source, description, etc.) _without_ displaying the debug/error
dialog?

Or is writing the main macro as a function my only option?

--
Rhino


Jezebel said:
Write the macro as a function, and return whatever value you like.

Public Function MyMacro() as boolean

DO whatever ...

If [ErrorCondition] then
MyMacro = FALSE
Else
MyMacro = TRUE
End if

End Function


Alternatively, your macro can raise an error and return error
information like any other function ...

Public Sub MyMacro()

DO whatever ...

If [Error Condition] then
Err.Raise Number:=vbObjectError, Source:="MyMacro",
Description:="Text file was unusable"
End if

End Sub







Is it possible to make a macro stop executing and give a non-zero
return code back to the VBScript that is executing it?

I'm just putting the final polish on my first serious macro. Basically,
I have a VBScript that invokes Word invisibly and invokes a macro that
reads a text file and builds a resume document using the data that is
in the text file.

There is one situation in which the input in the text file may not be
usable. If that happens, I want the main macro to exit when I detect
that situation and I need the VBScript to be able to tell that the
macro did not complete normally but had to abort due to the problem.
Programming languages often use return codes, usually numeric, to
indicate that an error caused the program to abort.

I'd like the main macro to exit with a non-zero return code if there is
a serious error; otherwise, I want it to complete normally with a zero
return code.

Is that, or anything similar, possible in VBA?

Also, assuming it is possible to return different codes from a VBA
macro, can anyone tell me how to make my VBScript check the return code
that VBA gives? This is my VBScript in its entirety:

------------------------------------------------------------------------------------------
' Get arguments into variables
If WScript.Arguments.Count > 0 Then
MsgBox "Too many arguments; expecting none."
WScript.Quit
End If

' Find path for MyDocuments folder
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(&H5&)
strMyDocPath = objFolder.Self.Path

' Start Word Application, open TestDoc.doc in MyDocuments
Set oWd = CreateObject("Word.Application")
oWd.Visible = False
Set oDoc = oWd.Documents.Open(strMyDocPath & "\resume.doc")

'Run macro named createResumeFromFile, which has no arguments
oWd.Run "createResumeFromFile"

' Save changes to doc on closing and quit Word
oDoc.Save
oDoc.Close
oWd.Quit
Set oWd = Nothing

Set objShell = Nothing
------------------------------------------------------------------------------------------

I'm envisioning something like the following, inserted after the last
line of the VBScript:

'Get return code from macro
if (oWd.ReturnCode <> 0) then
exit(oWd.ReturnCode)
end if

In other words, regardless of the return code, proceed to save the
document, close the document, and exit Word. But if the macro ended
with a non-zero return code, end the script itself with the same
non-zero return code that the macro ended with.

Can anyone enlighten me on how to do this or point me to a more
appropriate newsgroup for VBScript questions?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top