Working with current open email

前端 未结 3 932
日久生厌
日久生厌 2020-12-03 17:47

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

3条回答
  •  独厮守ぢ
    2020-12-03 17:56

    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.

    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 & "

    " & myText & "

    " End Select End If End If End If End Sub

提交回复
热议问题