【问题标题】:Working with current open email使用当前打开的电子邮件
【发布时间】:2020-12-08 12:35:24
【问题描述】:

我想获取活动打开的 MailItem(无论是新邮件还是收到的邮件)。当用户运行我的宏时,我需要向该邮件添加一些内容。我正在使用 Outlook 2003 和 VBA。

我发现了这个:How do you get a reference to the mail item in the current open window in Outlook using VBA? 但是它不起作用,因为TypeName(Application.ActiveWindow) 设置为空。我也试过Set Mail = Application.ActiveInspector.currentItem,但也没用。

ActiveInspector 一定有我不明白的地方。

根据要求,这是位于专用模块中的过程/宏,当用户单击Application_Startup() 方法中添加的菜单按钮时调用

Sub myMacro()
    Dim NewMail As Outlook.MailItem
    Set NewMail = Application.ActiveInspector.currentItem
End Sub

【问题讨论】:

  • 如果没有选择任何内容,那么ActiveInspector 将是Nothing。不过,我不知道ActiveWindow 怎么可能是Nothing。你把这段代码放在哪里,你是如何调用它的?
  • 代码在一个模块中,当用户手动运行宏或单击运行宏的菜单按钮时调用该过程。
  • 能否贴出整个方法的代码?
  • 这个脚本是从邮件窗口本身启动的吗?
  • 已添加代码,这里不多说...Set 行出错,ActiveExplorer 设置为空(或不存在)。正如我所说,这里可能有一些明显的我不明白的东西。顺便说一下,这是 Outlook VbaProject.OTM 文件。

标签: vba outlook


【解决方案1】:

我不知道您的代码到底有什么问题。但是,一方面,您并没有验证新的、可编辑的电子邮件是否已打开。以下概念验证完全符合我的想法:在正在撰写的活动电子邮件中插入一些文本。如果这是不可能的,它会显示一个消息框来解释原因。

仅当 Word 用作电子邮件编辑器(将 ALWAYS be the case in Outlook 2010+)时,插入文本的部分才有效。如果不是,您将不得不直接解析和更新 Body 或 HTMLBody 文本。

Sub InsertText()
    Dim myText As String
    myText = "Hello world"

    Dim NewMail As MailItem, oInspector As Inspector
    Set oInspector = Application.ActiveInspector
    If oInspector Is Nothing Then
        MsgBox "No active inspector"
    Else
        Set NewMail = oInspector.CurrentItem
        If NewMail.Sent Then
            MsgBox "This is not an editable email"
        Else
            If oInspector.IsWordMail Then
                ' Hurray. We can use the rich Word object model, with access
                ' the caret and everything.
                Dim oDoc As Object, oWrdApp As Object, oSelection As Object
                Set oDoc = oInspector.WordEditor
                Set oWrdApp = oDoc.Application
                Set oSelection = oWrdApp.Selection
                oSelection.InsertAfter myText
                oSelection.Collapse 0
                Set oSelection = Nothing
                Set oWrdApp = Nothing
                Set oDoc = Nothing
            Else
                ' No object model to work with. Must manipulate raw text.
                Select Case NewMail.BodyFormat
                    Case olFormatPlain, olFormatRichText, olFormatUnspecified
                        NewMail.Body = NewMail.Body & myText
                    Case olFormatHTML
                        NewMail.HTMLBody = NewMail.HTMLBody & "<p>" & myText & "</p>"
                End Select
            End If
        End If
    End If
End Sub

【讨论】:

  • 它有效。在程序结束时重置 oInspector 是否也是一个好习惯(类似于Set oInspector = Nothing)?
【解决方案2】:

您是指当前选择的消息吗?在这种情况下,您需要使用Application.ActiveExplorer.Selection 集合,而不是Application.ActiveInspector.CurrentItem

【讨论】:

    【解决方案3】:
                '
                Dim myOlExp As Outlook.Explorer
                Dim myOlSel As Outlook.Selection
    
                Set myOlExp = Application.ActiveExplorer
                Set myOlSel = myOlExp.Selection
                'MsgBox myOlSel.item(1)
    
    
                Dim selectedFolder As Outlook.MAPIFolder
                  Set selectedFolder = myOlExp.CurrentFolder
                Dim itemMessage As String
                  itemMessage = "Item is unknown."
    
    
                 Dim expMessage As String
                 expMessage = "Your current folder is " & selectedFolder.Name & "." & vbCrLf
    
                 If myOlSel.Count > 0 Then
    
                                 Dim selObject As Object
                                Set selObject = myOlSel.item(1)
    
                                If (TypeOf selObject Is Outlook.mailItem) Then
                                    Dim mailItem As Outlook.mailItem
                                    Set mailItem = selObject
                                    itemMessage = "The item is an e-mail message." & " The subject is " & mailItem.Subject & "."
                                    mailItem.Display (False)
    
                                ElseIf (TypeOf selObject Is Outlook.contactItem) Then
                                    Dim contactItem As Outlook.contactItem
                                    Set contactItem = selObject
                                    itemMessage = "The item is a contact." & " The full name is " & contactItem.Subject & "."
                                    contactItem.Display (False)
    
                                ElseIf (TypeOf selObject Is Outlook.AppointmentItem) Then
                                    Dim apptItem As Outlook.AppointmentItem
                                    Set apptItem = selObject
                                    itemMessage = "The item is an appointment." & apptItem.Subject & "."
    
                                ElseIf (TypeOf selObject Is Outlook.taskItem) Then
                                    Dim taskItem As Outlook.taskItem
                                    Set taskItem = selObject
                                    itemMessage = "The item is a task." & " The body is " & taskItem.Body & "."
                                ElseIf (TypeOf selObject Is Outlook.meetingItem) Then
                                    Dim meetingItem As Outlook.meetingItem
                                    Set meetingItem = selObject
                                    itemMessage = "The item is a meeting item. " & "The subject is " & meetingItem.Subject & "."
                                End If
                            End If
                            expMessage = expMessage & itemMessage
                        MsgBox (expMessage)
                End Sub
    

    【讨论】:

    • 你应该添加一些文字来解释你的答案,而不是只是一段代码。
    • @Minzkraut 特别针对 4 岁的问题
    • @dnLL 我不知道实际问题,因为我在评论屏幕中发表了评论。
    猜你喜欢
    • 2016-12-12
    • 2012-01-30
    • 1970-01-01
    • 2012-10-10
    • 1970-01-01
    • 1970-01-01
    • 2022-10-25
    • 2013-10-05
    • 2015-02-17
    相关资源
    最近更新 更多