none
Paste Chart into Email -- Run-time error '4605' RRS feed

  • Question

  • Subject: Error 4605 during paste function of word editor into body of email

    Program: Excel 2010

    Specific Line: wEditor.Application.Selection.Paste

    I am using word editor in Excel VBA to copy and paste a cell range and then multiple charts into the body of a new Outlook email. Occasionally a 4605 error will arise on the paste function during runtime. I have an error catching loop set up to keep trying the paste function for a certain amount of times in the event this does happen. What's odd is that the error is random and there is not one thing I can find to specifically set it off.

    Any thoughts on how to set focus to the body of the email before pasting? Or perhaps a better way to paste images into a new email?

    Thank you!

    Sub sendEmail()
        Dim sendEmail As Integer
        Dim loopCount As Integer
        
        'Verify sending of email
        Sheets("ECC SNAPSHOT").Activate
        sendEmail = MsgBox("Are you ready to send the email?" & vbNewLine & vbNewLine & "Note: this will also save/close the workbook.", vbYesNo + vbQuestion, "Send Email")
        
        If sendEmail = vbYes Then
            With Application
                .EnableEvents = False
                .ScreenUpdating = False
            End With
            
            'Create email objects
            Set mailApp = CreateObject("Outlook.Application")
            Set mail = mailApp.CreateItem(olMailItem)
            
            'Set email variables
            With mail
                .SentOnBehalfOfName = "DESIRED EMAIL HERE"
                .Bcc = "RECIPIENTS"
                .Subject = "SUBJECT"
                .Body = ""
            End With
            
            'Open email and create Word editor
            mail.Display
            Set wEditor = mailApp.ActiveInspector.wordEditor
            
            'Paste dashboard into email
            Worksheets("ECC SNAPSHOT").Activate
            Range("A5:X16").Copy
            On Error GoTo ErrHandler:
            wEditor.Application.Selection.Paste
            loopCount = 0
            
            'Add space after dashboard
            Worksheets("ECC SNAPSHOT").Activate
            Range("AA1:AA2").Copy
            On Error GoTo ErrHandler:
            wEditor.Application.Selection.Paste
            loopCount = 0
            
            'Add charts to email body
            Worksheets("Graphs").ChartObjects("US Tech").Activate
            ActiveChart.ChartArea.Copy
            On Error GoTo ErrHandler:
            wEditor.Application.Selection.Paste
            loopCount = 0
            
            Worksheets("Graphs").ChartObjects("US APS").Activate
            ActiveChart.ChartArea.Copy
            On Error GoTo ErrHandler:
            wEditor.Application.Selection.Paste
            loopCount = 0
            
            Worksheets("Graphs").ChartObjects("US MS").Activate
            ActiveChart.ChartArea.Copy
            On Error GoTo ErrHandler:
            wEditor.Application.Selection.Paste
            loopCount = 0
            
            'Add space before totals charts
            Worksheets("ECC SNAPSHOT").Activate
            Range("AA1:AB2").Copy
            On Error GoTo ErrHandler:
            wEditor.Application.Selection.Paste
            loopCount = 0
            
            Worksheets("Graphs").ChartObjects("US Total").Activate
            ActiveChart.ChartArea.Copy
            On Error GoTo ErrHandler:
            wEditor.Application.Selection.Paste
            loopCount = 0
            
            Worksheets("Graphs").ChartObjects("CAN Total").Activate
            ActiveChart.ChartArea.Copy
            On Error GoTo ErrHandler:
            wEditor.Application.Selection.Paste
            
            'The following line will send the email
            mail.Send
        Else
            End
        End If
        
        Set mailApp = Nothing
        Set mail = Nothing
        Set olMailItem = Nothing
        Set wEditor = Nothing
        Application.CutCopyMode = False
        
        With Application
            .EnableEvents = True
            .ScreenUpdating = True
        End With
            
        'Save the workbook, then close it
        Sheets("ECC SNAPSHOT").Activate
        Range("V3").Select
        ThisWorkbook.Close savechanges:=True
        'End procedure if above does not
        End
        
    ErrHandler:
        'Limit resume option
        If loopCount < 8000 Then
            loopCount = loopCount + 1
            Resume
        End If
        
        'If limit reached, close email and prompt user
        mail.Close 1
        Sheets("ECC SNAPSHOT").Activate
        Range("V3").Select
        Application.CutCopyMode = False
        MsgBox ("Email creation was unsuccesful after multiple attempts, please try again." + vbNewLine + vbNewLine + _
            "If you notice the email open upon running and the body remains blank, you may try clicking your mouse in the body of the email for it to paste the data.")
        End
        
    ErrHandler2:
        'The below is currently not in use
        mail.Close 1
        Sheets("ECC SNAPSHOT").Activate
        Range("V3").Select
        Application.CutCopyMode = False
        MsgBox ("Sorry, email creation encountered a problem, please try again.")
        End
    End Sub
    • Edited by Joh2239509 Thursday, June 25, 2015 1:18 PM Extra text that was uneccessary
    Thursday, June 25, 2015 1:15 PM

All replies

  • **UPDATE: Embedding the images via HTML as seen below is great however I cannot view the images via a mobile email application (Good for Enterprise). Not sure if the app is interpreting the HTML wrong or if this is expected behavior. Any suggestions are appreciated!

    Well after some Google searches I seem to have resolved the problem by using HTML rather than pasting the images into the email body. I want to give credit however I cannot post links in my replies yet.

    Do a search for "Solved: Embed image into body mail"

    Sample snippet of the code (credit mark007 from the website I found):

    .... 
        With CreateObject("Outlook.Application").CreateItem(0) 
            .Attachments.Add myPath & "\" & myChart1
            .HTMLBody = "<html><p>This is a picture</p>" & _ 
            "<img src=cid:myChart1.png>" & _ 
            .Display 
        End With 
    ....



    • Marked as answer by Joh2239509 Thursday, June 25, 2015 3:08 PM
    • Unmarked as answer by Joh2239509 Thursday, June 25, 2015 6:28 PM
    • Edited by Joh2239509 Thursday, June 25, 2015 6:34 PM
    Thursday, June 25, 2015 3:08 PM