Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I close Word (or other app) if an error occurs in a VBScript?
    text
    copied!<p>I wrote a VBScript app to open Word and Excel documents and search and replace blocks of text and various sections, pulling the new text from a plain text file. I purposely avoided any error checking, primarily because I couldn't figure it out at the time (and the script ran reliably anyway). Now months later on my local machine, I am inexplicably getting error messages about Normal.dot being changed and a message box asking what I want to do about it (which requires three more dialogs to finally answer). Of course this kills my ability to run the script and simply walk away, as it causes the script to fail. Currently when this happens, I have to open the Task Manager, find Winword.exe (of which the GUI isn't running) and kill it then re-run my script.</p> <p>What's a reasonable way of catching the error and successfully shutting down Word (or Excel). Based on this <a href="https://stackoverflow.com/questions/157747/vbscript-using-error-handling">question</a> I'm trying this: </p> <pre><code>Set objDoc = objWord.Documents.Open(curDir1 + "\docs\template_spec.dot") If Err.Number &lt;&gt; 0 Then WScript.Echo "Error in Word Open:" &amp; Err.Description objWord.Quit Else Set objSelection = objWord.Selection 'Do replacement activities' ReplaceText(objSelection) objDoc.SaveAs(curDir1 + "\docs\mynewdocument.doc") objWord.Quit End If Set objShell = Nothing Set objWord = Nothing Set objExcel = Nothing </code></pre> <p>Of course, as fate would have it, I cannot replicate the problem, so it works like normal. Does this solution seem reasonable? And a side question: How the heck do I get Word to stop complaining about Normal.dot (or get the script to handle it)? It's as if Word leaves itself open in the background after I have closed the GUI in some cases.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload