使用当前打开的电子邮件

时间:2013-03-18 12:34:45

标签: vba outlook

我想获得活动打开的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

3 个答案:

答案 0 :(得分:17)

我不知道您的代码究竟出了什么问题。但是,一方面,您没有验证新的可编辑电子邮件是否已打开。以下概念验证完全符合我的想法:将一些文本插入正在编写的活动电子邮件中。如果无法做到这一点,则会显示一个消息框,说明原因。

插入文本的部分仅在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

答案 1 :(得分:4)

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

答案 2 :(得分:1)

            '
            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