使用当前打开的电子邮件 [英] Working with current open email

查看:87
本文介绍了使用当前打开的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我发现了这一点:

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.currentItem but it doesn't work either.

对于 ActiveInspector 这件事一定有我不了解的地方.

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

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

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:

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

推荐答案

我不确定您的代码到底出了什么问题.但是,一方面,您并没有验证新的可编辑电子邮件是否处于打开状态.以下概念验证恰好符合我的想法:将一些文本插入正在编写的活动电子邮件中.如果无法做到这一点,则会显示一个消息框,说明原因.

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.

仅当Word被用作电子邮件编辑器时,插入文本的部分才有效(它将

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.

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

这篇关于使用当前打开的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆