Link to home
Start Free TrialLog in
Avatar of NerishaB
NerishaBFlag for South Africa

asked on

How Can I VBScript Outlook to Close and Save Open Messages and Shutdown Outlook Automatically during a forced reboot/shutdown.

Hi,

I am attempting to write a VBscript that will run during the shutdown process of Windows XP - called throught he logoff script.  I have copied the code I'm using below, but I am getting syntax errors.  The purpose of my script is:
1.  If a message is open, automatically save it to drafts and close the message.
2.  Close Outlook - Which will keep the cache file saved and not corrupt.

See the code below
Dim oOL ‘As Outlook.Application
Set oOL = GetObject(, "Outlook.Application")
If oOL Is Nothing Then
'no need to do anything, Outlook is not running
Else
'Outlook running
 Do while oOL.Inspectors.Count <> 0
    lngCount = oOL.Inspectors.Count
    For i = lngCount To 1 Step -1 
        oOL.Inspectors(i).CurrentItem.Save
        oOL.Inspectors(i).Close True
    Next
 Loop
oOL.Session.Logoff
oOL.Quit
End If
Set oOl = Nothing

Open in new window

Avatar of David Lee
David Lee
Flag of United States of America image

Hi, NerishaB.

What are the errors and what line(s) do they occur on?
Avatar of NerishaB

ASKER

Hi,

I get the following error on Line 1 : "Invalid character"
VBScript doesn't allow type declarations.  Change the line to

Dim oOL
I tried that, now I gett an error on Line 10, saying "Wrong number of arguments or invalid property assignment: 'Inspectors'"
I don't see anything wrong with line 10.  That aside, I modified the code slightly to make it simpler then tested here in my environment.  This works fine for me.
Dim oOL As Outlook.Application
Set oOL = GetObject(, "Outlook.Application")
If oOL Is Nothing Then
    'no need to do anything, Outlook is not running
Else
    'Outlook running
    For i = oOL.Inspectors.count To 1 Step -1
        oOL.Inspectors(i).CurrentItem.Save
        oOL.Inspectors(i).Close True
    Next
    oOL.Session.Logoff
    oOL.Quit
End If
Set oOL = Nothing

Open in new window

Thanks, but it still does not work perfectly.

If I have Outlook open with no unsent emails open, it closes properly.  However, if I have an email open that I have not sent yet, it gives me the following error:
Line 10: "Wrong number of arguments or invalid property assignment: 'Inspectors'"
This should do it.
Dim oOL As Outlook.Application
Set oOL = GetObject(, "Outlook.Application")
If oOL Is Nothing Then
    'no need to do anything, Outlook is not running
Else
    'Outlook running
    For i = oOL.Inspectors.count To 1 Step -1
        oOL.Inspectors.Item(i).CurrentItem.Save
        oOL.Inspectors.Item(i).Close True
    Next
    oOL.Session.Logoff
    oOL.Quit
End If
Set oOL = Nothing

Open in new window

I dont see a difference between this solution, and the last one you gave me....
ASKER CERTIFIED SOLUTION
Avatar of David Lee
David Lee
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you.  It works perfectly!!!
Glad to hear it.  You're welcome.