Outlook 2007 parece nunca desligar quando instruído a
-
13-09-2019 - |
Pergunta
Atualmente trabalhando em um VBScript para automatizar algumas das suja PST trabalho ingestão eu faço, e eu encontrei algo problemático após a atualização do Outlook 2003 a 2007.
(tinha para atualizar para contornar um problema RTF Corpo em OL2003 ..)
Mesmo depois de instruir Outlook para fechar a loja PST, faça logoff e, em seguida, destruir o objeto (conjunto objNS = Nada, etc.), o Outlook ainda paira em torno de 1-30 segundos, dependendo do tamanho do PST arquivos que eu' m trabalhando.
Eu posso facilmente solução e colocar em um atraso (WScript.Sleep (300)), mas eu encontrar este sujo e não confiar nele completamente ... algumas ideias sobre como obter o Outlook para fechar corretamente?
Eu também tentei polling para a instância via GetObject (), mas parece que ele retorna False mesmo quando OUTLOOK.EXE ainda é visível no Gerenciador de tarefas.
código que estou usando abaixo:
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
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 () é puramente uma função de registro (envia para stdout e um debuglog.txt)
Solução
Que tal uma função como esta para ver se outlook.exe ainda está em execução
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
Em seguida, você pode simplesmente executar um loop até que a função retorna falso Algo como isto
Do While IsRunning("notepad.exe")
Loop