0
votes

I have searched the Stack Overflow site for questions related to closing Outlook. There were a number of hits but none seem to describe what I'm trying to do.

The problem I'm trying to solve is how to backup the Outlook data base automatically and unattended. Outlook needs to be closed (if it is running) before copying the .pst files.

I found a VBScript at (www.howto-outlook.com/howto/closeoutlookscript.htm) that seems like what I need. But I can't get it to run when initiated from the Windows Task Scheduler.

I am running on a Windows 8 Sony laptop.

My VBScript should close Outlook prior to doing a backup of the .pst files. The code is stored in CloseOutlookVerify.vbs.

Below is the offending code from CloseOutlookVerify.vbs:

Set colProcessList = objWMIService.ExecQuery _
   ("Select * from Win32_Process Where Name = 'Outlook.exe'")
For Each objProcess in colProcessList
   Set objOutlook = CreateObject("Outlook.Application")

' The above line fails with ERR = 70 - Permission denied

   objOutlook.Quit
   Closed = 1
Next
  • This script works correctly if I double-click on the .vbs file from Windows Explorer.
  • It works correctly if I run it from a DOS Command Prompt window.
  • It fails with err = 70 when run via the Windows Task Scheduler.

So, what is different about running this script from a command prompt vs. by the task scheduler? And how can I make it work correctly when run by the task scheduler?

FYI - I made my living programming in C and Unix shell languages, but this is my first exposure to VBS in the Windows environment.

Many thanks for any expertise you can provide.

1
What user account do you have set to run the task?admdrew

1 Answers

1
votes

I think it is because impersonationLevel has not been set. Try this:

      Set objWMIService = GetObject("winmgmts:" _
      & "{impersonationLevel=impersonate}!\\" & strSysName & "\root\cimv2")
      Set colProcessList = objWMIService.ExecQuery _
      ("Select * from Win32_Process Where Name = 'Outlook.exe'")
      For Each objProcess in colProcessList
        objProcess.Terminate()
      Next