Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Slightly OT: Can't get VBScript to handle error

Status
Not open for further replies.

kathryn

Programmer
Apr 13, 2000
776
US
OK here is the text of my vbs file, which I am calling using wscript.exe (Windows Scripting):

Dim objApp 'variable for an instance of the appropriate Application'
Dim objDoc 'variable for an instance of document'
Dim result 'variable for result to html.xla'
Dim newFileName 'variable for new file name'

On Error Resume Next

dname = WScript.Arguments(0)

newFileName=left(dname,len(dname)-4) + ".htm"

Select Case right(dname,3)

Case "doc"

'create an instance of Word'
Set objApp = CreateObject("Word.Application")

'open the document'
objApp.Documents.Open "E:\inetpub\& dname

if err<>0 then
strErrorMessage = &quot;Error: &quot; & Err.Number
MsgBox strErrorMessage, CRITICAL_ERROR + MSGBOX_OKONLY, &quot;Error Type
= &quot; & Err.Description
objApp.quit
exit sub
end if

blah,blah, blah

I have enabled error handling, I think, by including the On Error
Resume Next. However, I get a Windows Script Host error message as
soon as the Documents.Open fails; it never gets to the &quot;if err<> 0&quot;
line. This has resulted in about 25 instances of Word being open on
my Web development server!

What am I doing wrong?
Kathryn
 
Hmm. Not sure. I stripped things down to this bare .vbs and it works fine:
Code:
Dim objApp, dname

On Error Resume Next

dname = &quot;junk.doc&quot;

Set objApp = CreateObject(&quot;Word.Application&quot;) 
    
objApp.Documents.Open dname
    
if err<>0 then
   strErrorMessage = &quot;Error: &quot; & Err.Number
   MsgBox &quot;Error Type= &quot; & Err.Description, _
      vbCritical + vbOKOnly, strErrorMessage
   objApp.quit
end if

MsgBox &quot;Done!&quot;
Set objApp = Nothing
Note that I reversed your MsgBox parameters because Err.Description is too big for the title bar and is multiline anyway. Also I used the built-in constants.

I have no junk.doc so I get the error that it can't be found just as you'd expect.
 
Hello, kathryn.

A couple of comments.
[1] you should place On Error Resume Next closest to the trouble spot and Clear it out as soon as the error handling routine is triggered. For instance,
...
On Error Resume Next
Set objApp = ... etc
If Err<>0 Then
...
End If
Err.Clear

ObjApp.Documents.Open ...
If Err<>0 Then
...
End If
Err.Clear
...
On Error GoTo 0

[2] Should not put too much confidence on having an Arguments(), rather, make a control on its existence. You can also then delay to put up a On Error Resume Next.

If WScript.Arguments.Count=0 Then
....
Else
dname = ...Arguments(0)
End If

[3] Neither should you take it for granted the file extension be of three characters. Instead, find the location of signature &quot;.&quot; by InStrRev(), then Right it to determine the exact extension content.

I think with these measures, it would save you quite a bit of trouble over time.

regards - tsuji
 
Thanks for the replies. They were both helpful.

One question, to dilettante, did you run the vbs from wscript.exe? I can get it to run fine by itself; I only have problems when calling it from wscript.exe.

Thanks. Kathryn


 
I made a file test.vbs with my stripped sample code in it, then just double-clicked the icon, which invokes it with Wscript.

Did you mean something else by &quot;calling it from wscript.exe?&quot;
 
kathryn,

Comment out &quot;On error resume next&quot; so you can find the line where it is getting the error. I'm assuming since it is opening the application, either your filename is wrong or it has problems with some macros maybe.

Use:

objApp.Visible = TRUE


fengshui_1998
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top