【问题标题】:VBA Outlook "No active explorer found"VBA Outlook“找不到活动的资源管理器”
【发布时间】:2015-07-03 03:50:25
【问题描述】:

我遇到了 Outlook 自动化问题,

为简单起见,我将首先向您展示我的代码的较短版本:

Sub test()
  Dim GetOutlookApp As Object

  Set GetOutlookApp = CreateObject("Outlook.Application")
End Sub

首先,我确实想保留后期绑定解决方案。

此子在任务栏中启动 Outlook(小图标)。当我双击它时,会弹出一条消息:“未找到活动的资源管理器对象”(窗口标题:“错误”)。然后,当我单击“确定”时,Outlook 收件箱窗口打开。

我的脚本是为最终用户准备的,所以即使用户只需单击“确定”,我也不希望出现此消息(主子程序的其余部分没有任何问题)。

我必须解决这个问题才能使用 Outlook 发送电子邮件并确保电子邮件不在发件箱文件夹中。

我正在寻找的是一种使用后期绑定打开 Outlook 的方法,没有此消息。

以下是发送电子邮件前打开 Outlook 的完整代码(来源:ron de bruin)。除了前景消息外,它工作得很好。该行弹出消息:

obj.Session.GetDefaultFolder(olFolderInbox).Display

我尝试了 AppActivate 和其他东西,但我没有成功,并且在谷歌上找不到任何相关信息!

感谢您的帮助

Sub send_mail ()
   Dim OutApp  As Object
    Set OutApp = OutlookApp() 'OPEN OUTLOOK
    'Set OutApp = CreateObject("Outlook.Application") 'OPEN OUTLOOK simple solution


    With ActiveSheet.MailEnvelope
     ...
    End With
End sub

Public Function OutlookApp( _
    Optional WindowState As Long = olMaximized, _
    Optional ReleaseIt As Boolean = True _
    ) As Object
'***This sub is a part to the global way to open outlook before sending an email (prevent the outbox bug, email stucked into the outbox)
'***Source: http://www.rondebruin.nl/win/s1/outlook/openclose.htm, late binding mode

Static obj As Object

On Error GoTo ErrHandler


    Select Case True
        Case obj Is Nothing, Len(obj.Name) = 0
            Set obj = GetObject(, "Outlook.Application")
            If obj.Explorers.Count = 0 Then
InitOutlook:
                'Open inbox to prevent errors with security prompts
                obj.Session.GetDefaultFolder(olFolderInbox).Display
                obj.ActiveExplorer.WindowState = WindowState
            End If
        Case ReleaseIt
            Set obj = Nothing
    End Select
    Set OutlookApp = obj

ExitProc:
    Exit Function
ErrHandler:
    Select Case Err.Number
        Case -2147352567
            'User cancelled setup, silently exit
            Set obj = Nothing
        Case 429, 462
        MsgBox "Err.Number OutlookApp: " & Err.Number
            Set obj = GetOutlookApp()
            If obj Is Nothing Then
                Err.Raise 429, "OutlookApp", "Outlook Application does not appear to be installed."
            Else
                Resume InitOutlook
            End If
        Case Else
            MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "Unexpected error"
    End Select
    Resume ExitProc
    Resume
End Function

Private Function GetOutlookApp() As Object
'***This sub is a part to the global way to open outlook before sending an email (prevent the outbox bug, email stucked into the outbox)
'***Source: http://www.rondebruin.nl/win/s1/outlook/openclose.htm, late binding mode

'On Error GoTo ErrHandler

    Set GetOutlookApp = CreateObject("Outlook.Application")

ExitProc:
    Exit Function
ErrHandler:
    Select Case Err.Number
        Case Else
            'Do not raise any errors
            Set GetOutlookApp = Nothing
    End Select
    Resume ExitProc
    Resume
End Function

【问题讨论】:

  • 你试过转Application.DisplayAlerts= false吗?同样,您可以尝试OutlookApp.DisplayAlerts=false,虽然还没有尝试过
  • 是的,我试过了,但没有解决问题。 :(我忘了说使用VBA文件时,在“obj.Session.GetDefaultFolder(olFolderInbox).Display”代码行上弹出消息。

标签: vba email outlook


【解决方案1】:

使用 Explorers 类的 Add 方法创建资源管理器窗口的新实例。然后需要调用Explorer类(不是Folder)的Display方法。

  Sub DisplayDrafts()
   Dim myExplorers As Outlook.Explorers
   Dim myOlExpl As Outlook.Explorer
   Dim myFolder As Outlook.Folder

   Set myExplorers = Application.Explorers
   Set myFolder = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
   Set myOlExpl = myExplorers.Add(myFolder, olFolderDisplayNoNavigation)
   myOlExpl.Display
  End Sub

此子在任务栏中启动 Outlook(小图标)。当我双击它时,会弹出一条消息:“未找到活动的资源管理器对象”(窗口标题:“错误”)。然后,当我单击“确定”时,Outlook 收件箱窗口打开。

通常您不会在任务栏中看到任何图标。确保为操作系统和 Outlook 安装了所有最新的更新和服务包。在自动化 Outlook 之前,还要检查正在运行的进程列表 - 确保此时没有 Outlook 实例在运行。

请参阅How to automate Outlook from another program 了解更多信息。

【讨论】:

  • 感谢 Eugene,它可以工作(对后期绑定模式进行了一些修改),我的代码在下一个主题答案中。 :)
【解决方案2】:

使用 Eugene Astafiev 代码我已经解决了我的问题!谢谢尤金!

代码如下:

发送电子邮件的子:

Sub Send_Mail()
'**This sub aims to send the mail that contains the job sheet

'Deactivate the screen updating : increase the speed and looks better
Application.ScreenUpdating = False

    'Select the range of data
    ActiveSheet.Range(FirstCol_JS & 1 & ":" & LastCol_JS & Firstrow_JS + nb_item_scanned - 1).Select
    'Show the envelope on the ActiveWorkbook. This line prevents a bug (Method 'MailEnveloppe' of object '_Worksheet' failed. -2147467259, 80004005)
    ActiveWorkbook.EnvelopeVisible = True

    'Make sure outlook is opened or open it *****HERE IS WHY MY QUESTION*****
    Call OutlookApp <------------------------------------------------

    'Email error handling
    On Error GoTo ErrorManagement

    With ActiveSheet.MailEnvelope
        'Subject is the title of the mail
        .Item.Subject = "Job Sheet"
        'Introduction is the content of the mail
        .Introduction = "Hi John," & vbCrLf & _
                        "..." & vbCrLf & _
                        "Regards, The computer"
       .Item.To = "alias@domain.com"
       .Item.Send
    End With

    'Select the home page (main sheet)
        'It is needed to activate the screenupdating so that the userform can be displayed on the sheet1
        Application.ScreenUpdating = True

Else
    'Normally, this message should never appear
    MsgBox "You can't create a job sheet without any item. Nothing was done.", , "Action not allowed"
End If

'Exit sub before the error handling codes
Exit Sub

ErrorManagement:
'Activate the screen updating : be able to show that the outlook interface disappears
Application.ScreenUpdating = True

'Hide the outlook interface
ActiveWorkbook.EnvelopeVisible = False

'Activate the Excel windows so that the msgbox does not appear in the Windows taskbar
'This line is mandatory because the outlook interface is in front of the Excel workbook when it appears, so we have to activate again the Excel worbook
Call ActivateExcel

End Sub

设法打开 Outlook 的主子

Sub OutlookApp(Optional ReleaseIt As Boolean = True)
'***This sub is a part to the global way to open outlook before sending an email (prevent the outbox bug, email stucked into the outbox)
'***Source: http://www.rondebruin.nl/win/s1/outlook/openclose.htm, late binding mode, and Eugene Astafiev http://stackoverflow.com/questions/31198130/vba-outlook-no-active-explorer-found

'Declaration of an object for outlook. The static mode allows to keep the object when this sub is launched more than one time
Static olObject As Object   'Early binding: Outlook.Application

'Declaration of variable objects to open the outlook window (prevent the email to be stuck in the Outbox folder)
Dim myExplorers As Object   'Early binding: Outlook.Explorers
Dim myOlExpl As Object      'Early binding: Outlook.Explorer
Dim myFolder As Object      'Early binding: Outlook.Folder

'Error handling
On Error GoTo ErrHandler


Select Case True
    'If the olObject is nothing then try to create it
    Case olObject Is Nothing, Len(olObject.Name) = 0
        'This line will work if outlook is already opened, otherwise it will create an error and the code will go to ErrHandler
        Set olObject = GetObject(, "Outlook.Application")

        'If there is not already one opened windows of outlook
        If olObject.Explorers.Count = 0 Then
InitOutlook:
            'Open outlook window to prevent the email to be stucked in the Outbox folder (not sent)
            Set myExplorers = olObject.Explorers
            Set myFolder = olObject.GetNamespace("MAPI").GetDefaultFolder(6) 'olFolderInbox: 6
            Set myOlExpl = myExplorers.Add(myFolder, 0) 'olFolderDisplayNoNavigation: 2, olFolderDisplayNormal:0
                            'Early binding code:
                            'Set myExplorers = Application.Explorers
                            'Set myFolder = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
                            'Set myOlExpl = myExplorers.Add(myFolder, olFolderDisplayNoNavigation)
            myOlExpl.Display
        End If
End Select

'Delete the olObject variable is the ReleaseIt boolean is true
If ReleaseIt = True Then
    Set olObject = Nothing
End If

'Possibility to set the OutlookApp function as the outlook object, if OutlookApp is declared like this: "Function OutlookApp(Optional ReleaseIt As Boolean = True) as Object"
'Set OutlookApp = olObject

ExitProc:
    Exit Sub
ErrHandler:
    Select Case Err.Number
        Case -2147352567
            'User cancelled setup, silently exit
            Set olObject = Nothing
        Case 429, 462 '429: outlook was not opened, the Set olObject = GetObject(, "Outlook.Application") code line above did not work
            Set olObject = CreateOutlook()  'Launch the CreateOutlook function: CreateOutlook = CreateObject("Outlook.Application")
            If olObject Is Nothing Then     'If the outlook object is still empty it means that there is a more serious issue (outlook not installed on the computer for example)
                Err.Raise 429, "OutlookApp", "Outlook Application does not appear to be installed."
            Else                            'If olObject is no more nothing, go back to the code above and open the outlook window
                Resume InitOutlook
            End If
        Case Else   'For any other error numbers
            MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "Unexpected error"
    End Select
    Resume ExitProc
    Resume 'For debugging
End Sub

设法打开前景的第二个子

Private Function CreateOutlook() As Object
'***This sub is a part to the global way to open outlook before sending an email (prevent the outbox bug, email stucked into the outbox)
'***Source: http://www.rondebruin.nl/win/s1/outlook/openclose.htm, late binding mode

'Error handling
On Error GoTo ErrHandler

Set CreateOutlook = CreateObject("Outlook.Application")

ExitProc:
    Exit Function
ErrHandler:
Set CreateOutlook = Nothing

    Resume ExitProc
    Resume 'For debugging
End Function

仅供参考,这里是激活 Excel 窗口的代码

Sub ActivateExcel()
'***This sub aims to activate Excel windows (so that it's in front of the windows)

'Set variable title equal to exact application window title
Dim ExcelTitleCaption As String
ExcelTitleCaption = Application.Caption

'Activate Microsoft Excel
AppActivate ExcelTitleCaption
End Sub

谢谢!!题目解决了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-26
    • 1970-01-01
    • 2023-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多