Consulting

Results 1 to 11 of 11

Thread: The paste method of the _chart object failed

  1. #1

    The paste method of the _chart object failed

    Good evening,
    I am using this code that I found on the internet, however when running in Excel2013 the following message appears
    "Run-time error 1004
    The paste method of the _chart object failed.
    How can I solve.

    Thank you
    Private Sub Etapa2GIF()
    Dim rng As Excel.Range
    Dim cht As Excel.ChartObject
    TempFile = VBA.Environ$("temp") & "\"
    Application.ScreenUpdating = False
    Set rng = Plan1.Range("B44:P46")
    rng.CopyPicture xlScreen, xlPicture
    Set cht = ActiveSheet.ChartObjects.Add(0, 0, rng.Width, rng.Height)
    cht.Chart.Paste
    cht.Chart.Export TempFile & "Etapa2.gif"
    cht.Delete
    ExitProc:
    Application.ScreenUpdating = True
    Set cht = Nothing
    Set rng = Nothing
    End Sub

  2. #2
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    try this

    DoEvents
    DoEvents
    DoEvents
    cht.Chart.Paste

  3. #3
    Mana,

    Good afternoon,

    I tried as you requested and even then the problem continues.

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    https://msdn.microsoft.com/en-us/lib.../ff198129.aspx

    Private Sub Etapa2GIF()
        Dim TempFile As String
        TempFile = VBA.Environ$("temp") & "\" & "Etapa2.gif"
        ActiveSheet.ChartObjects(1).Chart.Export Filename:=TempFile, FilterName:="GIF"
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    Mdmackillop,

    Good Morning,


    I am new to VBA, I tried to run from the sub that you put and gives the following error message.


    "Application definition or object definition error"


    How should I proceed to adjust your SUB to my macro?


    Thank you very much for your support.

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Can you post your workbook? Use Go Advance/Manage Attachments.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    Good Morning,


    Attached file.
    Attached Files Attached Files

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Your project code is locked and I see no chart nor data to create one
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    mdmackillop

    Good afternoon,
    Sorry for the carelessness.
    It does not really have graphics, it saves in GIF the defined area.
    Attached Files Attached Files

  10. #10
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Your original code basically uses the same method as this but looks a lot simpler. This seems to work.
    Option Explicit
    Sub Test()
        Dim sht As Worksheet, shS As Worksheet
        Dim TempFile As String
        
        Application.ScreenUpdating = False
        Set shS = ActiveSheet
        Set sht = Sheets.Add(After:=shS)
    
    
        shS.Range("E44:L46").Copy
        sht.Pictures.Paste
        
        TempFile = VBA.Environ$("temp") & "\" & "Etapa2.gif"
        Call ExportPic(sht, TempFile)
        Application.DisplayAlerts = False
        sht.Delete
        Application.DisplayAlerts = True
        Application.ScreenUpdating = True
    End Sub
    
    
    Sub ExportPic(sht As Worksheet, fname As String)
        'Based upon https://stackoverflow.com/questions/25333558/export-pictures-excel-vba
    
    
        Dim MyChart As Chart
                If InStr(sht.Shapes(1).Name, "Picture") > 0 Then
                    'create chart as a canvas for saving this picture
                    Set MyChart = Charts.Add
                    MyChart.Name = "TemporaryPictureChart"
                    'move chart to the sheet where the picture is
                    Set MyChart = MyChart.Location(Where:=xlLocationAsObject, Name:=sht.Name)
    
    
                    'resize chart to picture size
                    MyChart.ChartArea.Width = sht.Shapes(1).Width
                    MyChart.ChartArea.Height = sht.Shapes(1).Height
                    MyChart.Parent.Border.LineStyle = 0 'remove shape container border
    
    
                    'copy picture
                    sht.Shapes(1).Copy
    
    
                    'paste picture into chart
                    MyChart.ChartArea.Select
                    MyChart.Paste
    
    
                    'save chart as jpg
                    MyChart.Export fname, "gif"
    
    
                    'delete chart
                    sht.Cells(1, 1).Activate
                    sht.ChartObjects(sht.ChartObjects.Count).Delete
                End If
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  11. #11
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Now I've seen the principle, try this
    Private Sub Etapa2GIF()
        Dim rng As Excel.Range
        Dim cht As Excel.ChartObject
        TempFile = VBA.Environ$("temp") & "\"
        Application.ScreenUpdating = False
        Set rng = Plan1.Range("B44:P46")
        rng.CopyPicture xlScreen, xlPicture
        Set cht = ActiveSheet.ChartObjects.Add(0, 0, rng.Width, rng.Height)
        cht.Select
        cht.Chart.Paste
        cht.Chart.Export TempFile & "Etapa2.gif"
        cht.Delete
    ExitProc:
        Application.ScreenUpdating = True
        Set cht = Nothing
        Set rng = Nothing
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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