Hi,

I am looking for a simple script that will close Outlook 2003 in an elegant
fashion rather than killing the outlook.exe process.

I have in the past used a script but I see that wscipt.exe hangs at 50% as
if the script caonnt complete.

on error resume next
Set objOutlook = GetObject(, "Outlook.Application")

do while ObjOutlook.Inspectors.Count <> 0
For Each objInspector In objOutlook.Inspectors
objInspector.CurrentItem.Save
objInspector.Close True
Next

loop
objOutlook.quit

Can anyone let me know if they have a script that works all the time without
wscript.exe hanging at all?

Thank you

Oli

Re: Closes Outlook using VBS script by Tom

Tom
Wed May 07 07:23:13 CDT 2008

On May 7, 4:26 am, Oli <O...@discussions.microsoft.com> wrote:
> Hi,
>
> I am looking for a simple script that will close Outlook 2003 in an elegant
> fashion rather than killing the outlook.exe process.
>
> I have in the past used a script but I see that wscipt.exe hangs at 50% as
> if the script caonnt complete.
>
> on error resume next
> Set objOutlook = GetObject(, "Outlook.Application")
>
> do while ObjOutlook.Inspectors.Count <> 0
> For Each objInspector In objOutlook.Inspectors
> objInspector.CurrentItem.Save
> objInspector.Close True
> Next
>
> loop
> objOutlook.quit
>
> Can anyone let me know if they have a script that works all the time without
> wscript.exe hanging at all?
>
> Thank you
>
> Oli

I don't know what the problem is, but my guess is that one of two
things is happening, just based on a review of the scripting
techniques.

The first problem may be that there is an error encountered within the
DO loop that goes unreported because of the ON ERROR RESUME NEXT
statement. Since it is unhandled in your code, it just keeps getting
repeated because the exit condition for the loop is not achieved,
creating an infinite loop. My advice, either kill the ON ERROR
statement or handle the error. At a minimum, do this ...

do while (ObjOutlook.Inspectors.Count <> 0) and (Err.Number <> 0)
For Each objInspector In objOutlook.Inspectors
objInspector.CurrentItem.Save
objInspector.Close True
Next
loop

This still may not trap the error, depending on which line it occurs.
I'd opt for just killing the ON ERROR line.

The other possibility, which is a long shot, is that you script is
hogging so much of the CPU time that the requested Outlook functions
just don't have time to complete. This could only be true if one of
them executes asynchronously (as a separate thread). In any case, the
solution is easy and one I would adopt anyway to give other processes
more access to the CPU when the script is running - just ad a short
Sleep in the Do Loop ...

do while (ObjOutlook.Inspectors.Count <> 0) and (Err.Number <> 0)
For Each objInspector In objOutlook.Inspectors
objInspector.CurrentItem.Save
objInspector.Close True
Next
wsh.sleep 100
loop

HTH,

Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/