+ Reply to Thread
Results 1 to 9 of 9

Method "Add" of object "ChartObjects" failed

Hybrid View

  1. #1
    Registered User
    Join Date
    05-27-2010
    Location
    Ireland
    MS-Off Ver
    Excel 2007
    Posts
    4

    Method "Add" of object "ChartObjects" failed

    I am getting this error:

    Runtime Error '2116157058 (81de017e)':
    Method "Add" of object "ChartObjects" failed

    This happens when I use my macro code:
    Set cht = ActiveSheet.ChartObjects.Add(0, 0, rng.Width + 10, rng.Height + 10)

    And it even happens when I use the Microsoft Help example code below:

    Dim ch As ChartObject
    Set ch = Worksheets("sheet1").ChartObjects.Add(100, 30, 400, 250)
    ch.Chart.ChartWizard source:=Worksheets("sheet1").Range("a1:a20"), _
    gallery:=xlLine, title:="New Chart"

    Does anybody know what could be causing this error, and how I can fix it?

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,978

    Re: Method "Add" of object "ChartObjects" failed

    Please put code tags round your code so we can assist you.
    (FYI, the code works for me in 2007)
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Registered User
    Join Date
    05-27-2010
    Location
    Ireland
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Method "Add" of object "ChartObjects" failed

    I get this error even if I copy the code directly from the example in Office 2007 Help, which is this:

    Dim ch As ChartObject
    Set ch = Worksheets("sheet1").ChartObjects.Add(100, 30, 400, 250)
    ch.Chart.ChartWizard source:=Worksheets("sheet1").Range("a1:a20"), _
    gallery:=xlLine, title:="New Chart"
    However, the project that I am trying to get to work is below. This code works on one machine (Vista, Office 2007) but does not work on the other (Windoes Server Edition 2003, Office 2007).

    Function CopyRangeToGIF()
    ' save a range from Excel as a picture
    Dim rng As Excel.Range
    Dim cht As Excel.ChartObject
    
    Const strPath As String = "C:\"
    
    Application.ScreenUpdating = False
    
    'Set rng = Range("A1").CurrentRegion
    Set rng = Range("A2:w35")
    
    rng.CopyPicture xlScreen, xlPicture
    
    Set cht = ActiveSheet.ChartObjects.Add(0, 0, rng.Width + 10, rng.Height + 10)
    
    cht.Chart.Paste
    
    cht.Chart.Export "C:\Users\Paul\Desktop\myfile.png"
    
    cht.Delete
    
    ExitProc:
    Application.ScreenUpdating = True
    Set cht = Nothing
    Set rng = Nothing
    End Function
    Any ideas? I have tried unprotecting the worksheet without any luck...

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,978

    Re: Method "Add" of object "ChartObjects" failed

    It definitely won't work if the sheet is protected when you run the code; otherwise, it should work fine as there is nothing wrong with the syntax.

  5. #5
    Registered User
    Join Date
    05-27-2010
    Location
    Ireland
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Method "Add" of object "ChartObjects" failed

    I know. I was working on this on the first machine without problems. Then I moved to the Windows Server 2003 machine and this simple function will not run...

    I'm not sure what could be causing the error...

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,978

    Re: Method "Add" of object "ChartObjects" failed

    Only thing I can think of offhand is that charts are very sensitive to printers - do you have a printer set up as the default on the server?

  7. #7
    Registered User
    Join Date
    05-27-2010
    Location
    Ireland
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Method "Add" of object "ChartObjects" failed

    No, there is no default printer set up on the server... And I don't 'print' anything at any stage of the code, so I'm not sure that I follow you - I don't see how a printer could cause a runtime error.

    But at this stage I'm open to try anything... it really has me stumped!

  8. #8
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,978

    Re: Method "Add" of object "ChartObjects" failed

    As I said, Excel charts are very closely tied to printers - in display terms, not just actual printing. Worth a try.

  9. #9
    Registered User
    Join Date
    07-15-2016
    Location
    Samara
    MS-Off Ver
    2010
    Posts
    1

    Re: Method "Add" of object "ChartObjects" failed

    Hi!
    So finally...
    Did you find a decision?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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