Run Time Error '1004' Copy method of picture class failed

Shinod

New Member
Joined
Jun 29, 2022
Messages
38
Office Version
  1. 2019
Platform
  1. Windows
I'm facing a problem (Run Time Error '1004' Copy method of picture class failed) randomly while running this VBA. Any solution to avoid this error?

VBA Code:
' print payslip for all selected employees
' uses the selected rows
Sub PrintPaySlips()
    Dim rCl As Range
    Dim rRng As Range
    With Sheets("Project")
        Set rRng = .Range(.Cells(2, 1), .Cells(.Rows.Count, 1).End(xlUp))
        For Each rCl In rRng
            With Sheets("Payslip")
                .Cells(3, 7).Value = rCl.Value
        Dim xRg As Range
    Application.ScreenUpdating = False
        For Each xRg In Range("G12:G33")
            If xRg.Value = "0" Then
                xRg.EntireRow.Hidden = True
        
            Else
                xRg.EntireRow.Hidden = False
            End If
        Next xRg
    Application.ScreenUpdating = True

    
    Dim ChO As ChartObject, ExportName As String
    Dim CopyRange As Range
    Dim Pic As Picture
    Dim i As Long
     Dim Fold As String
    Dim Name As String
    Dim Path As String
    
   
    
    Fold = ("C:\Users\Accounts\Desktop\Test")
    Name = ActiveSheet.Range("G3").Value & " " & ActiveSheet.Range("C3").Value
    Path = Fold & Application.PathSeparator & Name & ".jpg"




    With ActiveSheet
        Set CopyRange = Range("A1:H43")
        If Not CopyRange Is Nothing Then
            Application.ScreenUpdating = False
            ExportName = Path
            If Not ExportName = "False" Then
                CopyRange.Copy
                .Pictures.Paste
                Set Pic = .Pictures(.Pictures.Count)
                Set ChO = .ChartObjects.Add(Left:=10, Top:=10, Width:=Pic.Width, Height:=Pic.Height)
                Application.CutCopyMode = False
                Do
                    DoEvents
                    Pic.Copy
                    DoEvents
                    ChO.Chart.Paste
                    DoEvents
                    i = i + 1
                Loop Until (ChO.Chart.Shapes.Count > 0 Or i > 50)

                ChO.Chart.Export Filename:=ExportName, Filtername:="JPG"
                ChO.Delete
                Pic.Delete
            End If
            Application.ScreenUpdating = True
        End If
    End With


            End With
        Next rCl

    End With

                MsgBox "All payslips have been exported successfully to " & Fold

End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Pasting a lot of pictures in a loop is often problematic. Try allowing for more delay after the copy & paste operations. You can experiment with the delay.

VBA Code:
                Do
                    DoEvents
                    Pic.Copy
                    Application.Wait Now + TimeValue("0:00:02")
                    ChO.Chart.Paste
                    Application.Wait Now + TimeValue("0:00:02")
                    DoEvents
                    i = i + 1
                Loop Until (ChO.Chart.Shapes.Count > 0 Or i > 50)
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top