Outlook 2007在指示时似乎永远不会关闭

时间:2009-11-04 07:00:42

标签: vbscript outlook outlook-2007 outlook-vba

目前正在开发一个VBScript来自动执行一些脏的PST提取工作,并且在从Outlook 2003升级到2007之后我发现了一些问题。

(不得不升级以解决OL2003中的RTF Body问题..)

即使您指示Outlook关闭PST存储,注销然后销毁该对象(设置objNS = Nothing等),Outlook仍然会挂起1-30秒,具体取决于PST文件的大小。我正在使用。

我可以轻松解决并延迟(Wscript.Sleep(300))但我发现这很脏并且完全不相信它......关于如何让Outlook正常关闭的任何想法?

我还尝试通过GetObject()轮询实例,但即使在任务管理器中仍然可以看到OUTLOOK.EXE,它似乎也会返回False。

我在下面使用的代码:

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

注意:loggit()纯粹是一个日志记录功能(发送到stdout和debuglog.txt)

1 个答案:

答案 0 :(得分:0)

这样的函数如何看看outlook.exe是否仍在运行

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

然后你可以运行一个循环,直到函数返回false这样的东西

Do While IsRunning("notepad.exe")
Loop