Outlook 2007 never seems to shut down when instructed to
Currently working on a VBScript to automate some of the dirty PST ingestion work I do, and I've found something problematic after upgrading from Outlook 2003 to 2007.
(Had to upgrade to get around a RTF Body issue in OL2003 ..)
Even after you instruct Outlook to close the PST store, Log off and then destroy the object (set objNS = Nothing, etc.), Outlook still hangs around for 1-30 seconds depending on the size of the PST files I'm working with.
I can easily workaround and put in a delay (Wscript.Sleep(300)) but I find this dirty and don't trust it completely ... any ideas on how to get Outlook to close properly?
I have also tried polling for the instance via GetObject() but it seems that it returns False even when OUTLOOK.EXE is still visible in Task Manager.
Code I'm using below:
Function TestPSTInOutlook(strFileName)
' Open PST in Outlook then closes it, primarily to determine
' if Outlook has any difficulty in processing the PST in the
' first place. Not interested in corruptions per message, just
' PST-wide (and passwords).
Const olMailItem = 0
Const olMSG = 3
Const olDiscard = 1
On Error Resume Next
Dim objOL ' Outlook.Application
Dim objNS ' Outlook.Namespace
Dim objFolder ' Outlook.MAPIFolder
Dim objIS ' Outlook.Inspector
Dim objMail ' Outlook.MailItem
Set objOL = CreateObject("Outlook.Application")
Set objNS = objOL.GetNamespace("MAPI")
objNS.Logon
objNS.AddStore strFileName
If Err.Number <> 0 Then
loggit_silent = True
loggit("TestPSTInOutlook(): failed to open " & strFileName & " for reason: " & Err.Description)
loggit_silent = False
TestPSTInOutlook = False
Else
Set objFolder = objNS.Folders.GetLast
objFolder.Name = strFileName
Set objMail = objOL.CreateItem(olMailItem)
Set objIS = objMail.GetInspector
objIS.Close (olDiscard)
objMail.Close (olDiscard)
objNS.RemoveStore objFolder
loggit_silent = True
开发者_C百科loggit("TestPSTInOutlook(): success opening " & strFileName)
loggit_silent = False
TestPSTInOutlook = True
End If
' BUG: Outlook 2007 refuses to shut down when told and takes its time - we have to wait otherwise we error on trying to move the next PST file ...
' Does not exist in OL2003 but if we roll back then we don't get fixed Unicode PST support and degraded ingestion performance
'
objNS.Logoff
objOL.Session.Logoff
objOL.Quit
Set objIS = Nothing
Set objMail = Nothing
Set objNS = Nothing
Set objOL = Nothing
Wscript.sleep(300)
End Function
NB: loggit() is purely a logging function (sends to stdout and to a debuglog.txt)
What about a function like this to see if outlook.exe is still running
Function IsRunning(procName)
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery("Select * from Win32_Process Where Name = '" & procName & "'")
If colProcesses.Count > 0 Then
request = True
Else
request = False
End If
Set colProcesses = Nothing
Set objWMIService = Nothing
IsRunning = request
End Function
Then you can just run a loop until the function returns false Something like this
Do While IsRunning("notepad.exe")
Loop
精彩评论