vba 使用当前打开的电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15476986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 15:11:02  来源:igfitidea点击:

Working with current open email

vbaoutlook

提问by dan

I would like to get the active opened MailItem (whether it's a new mail or a received mail). I need to add some content to that mail when the user runs my macro. I'm using Outlook 2003 and VBA.

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

I found this: How do you get a reference to the mail item in the current open window in Outlook using VBA?It doesn't work however because TypeName(Application.ActiveWindow)is set to nothing. I also tried Set Mail = Application.ActiveInspector.currentItembut it doesn't work either.

我发现了这个:如何使用 VBA 在 Outlook 的当前打开窗口中获得对邮件项目的引用?但是它不起作用,因为TypeName(Application.ActiveWindow)设置为空。我也尝试过,Set Mail = Application.ActiveInspector.currentItem但它也不起作用。

There must be something I don't understand about the ActiveInspectorthing.

关于ActiveInspector 的事情,我一定有什么不明白的地方。

As requested, this is the procedure/macro located in a dedicated module, called when the user click on a menu-button added in the Application_Startup()method:

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

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

回答by Joshua Honig

I don't know exactly what's wrong with your code. For one thing, though, you are not validating that a new, editable email is even open. The following proof-of-concept does exactly what I think you're looking to do: insert some text into the active email being composed. If this is not possible it displays a message box explaining why.

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

The portion that inserts text will only work if Word is being used as the email editor (which will ALWAYS be the case in Outlook 2010+). If it is not you will have to parse and update the Body or HTMLBody text directly.

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

回答by Dmitry Streblechenko

Do you mean the currently selected message? In that case you need to use Application.ActiveExplorer.Selection collection, not Application.ActiveInspector.CurrentItem.

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

回答by Restevao

            '
            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