在未显示的 MailItem 上获取 Inspector

问题描述 投票:0回答:1

好的,现在我上一篇文章中的变量传递已经解决了,新的问题又出现了。

单击“新电子邮件”按钮会触发例程,该例程显示用户表单,然后立即显示实际的邮件消息。但是,新消息会显示在用户窗体的顶部。可行,但不理想。

Private Sub objInsp_NewInspector(ByVal Inspector As Inspector)
    Dim frm As frmNewEmail
    Dim msg As Outlook.MailItem
    If TypeName(Inspector.CurrentItem) = "MailItem" Then
        If Inspector.CurrentItem.Subject <> "" Then
            Exit Sub 'This procedure is only for new email mesages
        End If
        Set frm = New frmNewEmail
        With frm
            Set .Message = Inspector.CurrentItem
            .StartUpPosition = 2
            .Show False
        End With
    End If
End Sub

一个简单的解决方案是使用户表单模态化(删除 .Show 之后的 False 选项)。事实上,现在只显示用户表单,并且我假设电子邮件消息窗口正在等待用户表单被卸载。至少,这是我所期望的。

在用户窗体中单击一种语言后,用户窗体将被隐藏、卸载,并执行 MyNewEmail 过程,从而启动其他几个过程。

但是,我的代码现在在 Set objInsp = objItem.GetInspector 处执行这些过程时会抛出错误,而使用非模态用户窗体时,代码执行时不会出现错误。我在一些论坛上读到,要在显示的邮件项目上调用 GetInspector。但是,任何显示邮件消息的尝试也会导致错误。

有问题的代码是这样的:(objItem是

If msg.Class = olMail Then
    Set objInsp = msg.GetInspector '<<< Throws an error if userform = Modal
    If objInsp.EditorType = olEditorWord Then
        If objInsp.CurrentItem.BodyFormat <> olFormatHTML Then
            objInsp.CurrentItem.BodyFormat = olFormatHTML
        End If
    End If
Else
    Exit Function
End If

我可以想到几个选项,但我使用的代码都没有解决此错误。

a)使新消息的检查器窗口可见(希望这有帮助)。但是,我无法识别 或 窗口。

b) 以非模式模式执行用户表单并最小化新电子邮件窗口。我尝试了 Application.ActiveWindow.WindowState = olMinimized,但在这种情况下,主 Outlook 窗口已最小化(消息窗口尚未显示)。

c)……?

有人遇到过这个问题吗?

vba outlook
1个回答
0
投票

我找到了解决方案!

  1. 蒂姆,你是对的,表单不需要传递 mailitem 对象。只需打开表单并让它根据表单中的用户选择返回一个值。
  2. 问题是 GetInspector 仅在显示 Inspector 窗口时才可用。打开模态窗体时出现问题,因为 NewInspector 进程在显示实际消息之前停止了名为“before”的过程的挂起执行。打开非模态表单不会干扰 NewInspector 程序。由于某些过程包括对消息正文的修改(需要 GetInspector),因此此类过程仅在 Inspector.Activate 事件之后可用(请参阅 https://www.vboffice.net/en/developers/newinspector-and-inspector 上的示例) -激活/
  3. 调整后的程序为:

Private WithEvents objOpenInsp As Outlook.Inspector Private WithEvents objInsp As Outlook.Inspectors Private Sub objInsp_NewInspector(ByVal Inspector As Outlook.Inspector) If TypeName(Inspector.CurrentItem) = "MailItem" Then Set objOpenInsp = Inspector End If End Sub Private Sub objOpenInsp_Activate() Dim objItem As Outlook.MailItem Dim frm As frmLanguage Dim lngLang As Long Set objItem = objOpenInsp.CurrentItem If Len(objItem.EntryID) = 0 And Len(objItem.Subject) = 0 Then 'This procedure is only for new email messages. 'Note that if you leave the subject line empty and try to close 'the objItem window, the Activate event is triggered too and may 'get stuck in a loop. Set frm = New frmLanguage frm.Show lngLang = frm.lngRetValue Unload frm If lngLang > 0 Then 'MyNewMessage Inspector.CurrentItem, lngLang MyNewMessage objItem, lngLang Else 'User pressed Cancel or closed the form objItem.Subject = " " 'prevent loop objItem.Close olDiscard End If End If Set frm = Nothing Set objItem = Nothing End Sub

再次感谢您的帮助!

© www.soinside.com 2019 - 2024. All rights reserved.