+ Reply to Thread
Results 1 to 5 of 5

Object variable or With block variable not set when copying chart to outlook email body

Hybrid View

jprlimey Object variable or With block... 10-09-2019, 08:52 AM
Norie Re: Oject variable or With... 10-09-2019, 09:04 AM
jprlimey Re: Oject variable or With... 10-09-2019, 10:33 AM
Norie Re: Oject variable or With... 10-09-2019, 11:30 AM
jprlimey Re: Oject variable or With... 10-09-2019, 04:17 PM
  1. #1
    Forum Contributor
    Join Date
    12-22-2012
    Location
    Pittsburgh, PA
    MS-Off Ver
    Excel 2007 & 2010
    Posts
    205

    Object variable or With block variable not set when copying chart to outlook email body

    With the following VBA code I'm attempting to copy a chart from the active sheet to an email body, however I keep getting run-time error 91 - Object variable or With block variable not set, what is the cause of this?

    Additionally once I get past this issue I'd like to copy numerous charts to the same email, would simply copying the code specific to capturing the chart on the active sheet with the next chart name be the most efficient?

    Thanks

    Sub testmailchart()
    
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    
    With OutMail
        .To = "xxx.com"
        .Subject = "Test"
        '.Body = "See charts below" & vbCrLf
    
        Set vInspector = OutMail.GetInspector
        Set wEditor = vInspector.WordEditor
    
        ActiveSheet.ChartObjects("Test Chart").Activate
        ActiveChart.ChartArea.Copy
        wEditor.Application.Selection.Start = Len(.Body)
        wEditor.Application.Selection.End = wEditor.Application.Selection.Start
        wEditor.Application.Selection.Paste
            
        .Display
        
    End With
    
    End Sub
    Last edited by jprlimey; 10-09-2019 at 04:18 PM. Reason: Solved

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Oject variable or With block variable not set when copying chart to outlook email body

    Does this work?
    Sub testmailchart()
    
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    
        With OutMail
            .To = "xxx.com"
            .Subject = "Test"
            '.Body = "See charts below" & vbCrLf
        
            Set vInspector = OutMail.GetInspector
            Set wEditor = vInspector.WordEditor
        
            ActiveSheet.ChartObjects("Test Chart").Copy
            wEditor.Paragraphs(1).Range.Paste
                
            .Display
            
        End With
    
    End Sub
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    12-22-2012
    Location
    Pittsburgh, PA
    MS-Off Ver
    Excel 2007 & 2010
    Posts
    205

    Re: Oject variable or With block variable not set when copying chart to outlook email body

    Norie,

    The solution worked great.

    Is this something adaptable to add multiple charts, I tried coping for multiples and it didn't work.

    Thanks


    ActiveSheet.ChartObjects("Test Chart 1").Copy
            wEditor.Paragraphs(1).Range.Paste
    
    ActiveSheet.ChartObjects("Test Chart 2").Copy
            wEditor.Paragraphs(1).Range.Paste

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Oject variable or With block variable not set when copying chart to outlook email body

    I think that code would copy over 'Chart 1' with 'Chart2'.

    You'll need to paste the next chart after the previous one, perhaps something like this.
        
            ActiveSheet.ChartObjects("Chart 1").Copy
            wEditor.Paragraphs(1).Range.Paste
            
            wEditor.Paragraphs(1).Range.InsertParagraphAfter
    
            ActiveSheet.ChartObjects("Chart 1").Copy        
            wEditor.Paragraphs(2).Range.Paste

  5. #5
    Forum Contributor
    Join Date
    12-22-2012
    Location
    Pittsburgh, PA
    MS-Off Ver
    Excel 2007 & 2010
    Posts
    205

    Re: Oject variable or With block variable not set when copying chart to outlook email body

    Norie,

    Thanks for your help on the 2 solutions, now I'm back on track.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Generating Outlook Email and inseting chart into email with Text body.
    By Hyperion1571 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-17-2019, 12:41 PM
  2. Copying Outlook email body into excel
    By wolfdemon710 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-27-2015, 08:53 AM
  3. Run-time error '91': Oject Variable or with block variable not set
    By bharath.achar in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-19-2015, 01:39 AM
  4. [VBA] Outlook Calendar : Object Variable or With block variable not set
    By Giaccc in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-14-2014, 09:36 AM
  5. Email macro, include variable in body & add default outlook signature
    By promithius in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-13-2013, 09:05 AM
  6. Replies: 0
    Last Post: 04-16-2013, 07:15 AM
  7. [SOLVED] Run Time Error 91 'Oject variable or With block variable not set
    By aakash_bh in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-13-2012, 03:04 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1