15

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

我发现了这个:如何使用 VBA 在 Outlook 中当前打开的窗口中获取对邮件项目的引用?但是它不起作用,因为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
4

3 回答 3

20

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

插入文本的部分仅在 Word 用作电子邮件编辑器时才有效(在 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
于 2013-03-18T17:18:21.157 回答
5

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

于 2013-03-18T18:03:40.310 回答
2
            '
            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
于 2017-04-11T06:04:22.973 回答