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?
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?