使用当前打开的电子邮件

dnL*_*nLL 12 outlook vba

我想获得活动打开的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
Run Code Online (Sandbox Code Playgroud)

Jos*_*nig 17

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

插入文本的部分仅在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
Run Code Online (Sandbox Code Playgroud)


Dmi*_*nko 5

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