Paste Excel Charts to PPT Dynamically using chart name

abc_xyz

New Member
Joined
Jan 12, 2022
Messages
47
Office Version
  1. 2016
Platform
  1. Windows
Trying to create a dashboard wherein the user can add details regarding the File Name, Sheet Name, Chart Name, Slide No and many more.
The below code gives an error when it tries to copy a chart from the excel sheet (Subscription Out of Range). Not sure where am I going wrong. Can anyone help?

Area of error -
With FileToOpen

Set Chart = Worksheets("OpenBook_Sheet").ChartObjects("OpenBook_Charts")

Chart.CopyPicture Appearance:=xlScreen, Format:=xlPicture

End With



VBA Code:
Sub Excel_Charts_To_PPT()
    
Dim OpenBook_path As String, OpenBook_Sheet, OpenBook_Charts, Available_File As String, Next_File As String
Dim Chart As ChartObjects
Dim FileToOpen As Workbook
Dim i As Long, Lastrow As Long
Dim FSO As Object
Dim StartTime As Double
Dim MinutesElapsed As String


StartTime = Timer


With Application
   .ScreenUpdating = False
   .DisplayAlerts = False
   .EnableEvents = False
End With
    
    
    
    
pptTemplate = ThisWorkbook.Sheets("Dashboard_Charts").Range("F3").Value

Set PPT = CreateObject("Powerpoint.Application")

PPT.Presentations.Open Filename:=pptTemplate
    
    
    
    
Lastrow = ThisWorkbook.Sheets("Dashboard_Charts").Range("F" & Rows.Count).End(xlUp).Row
    
    For i = 9 To Lastrow
        
        OpenBook_path = ThisWorkbook.Sheets("Dashboard_Charts").Cells(i, 6)  'Path includes file name with extension
        OpenBook_Sheet = ThisWorkbook.Sheets("Dashboard_Charts").Cells(i, 7)
        OpenBook_Charts = ThisWorkbook.Sheets("Dashboard_Charts").Cells(i, 8).Value
        
        Slide_No = ThisWorkbook.Sheets("Dashboard_Charts").Cells(i, 9)
        Img_Height = ThisWorkbook.Sheets("Dashboard_Charts").Cells(i, 10)
        Img_Width = ThisWorkbook.Sheets("Dashboard_Charts").Cells(i, 11)
        Img_Horizontal_Pos = ThisWorkbook.Sheets("Dashboard_Charts").Cells(i, 12)
        Img_Vetical_Pos = ThisWorkbook.Sheets("Dashboard_Charts").Cells(i, 13)
        
        
        
'Check if file is open,if open, then use open file; if not, open file from the path in the cell
        Available_File = Dir(OpenBook_path) 'extracts the file name from the path
        
        Set FileToOpen = Nothing
        If Not wbOpen(Available_File, FileToOpen) Then
            Set FileToOpen = Workbooks.Open(OpenBook_path)
        End If
        
        
        
'open workbook from the path in the cell
        With FileToOpen
        
        Set Chart = Worksheets("OpenBook_Sheet").ChartObjects("OpenBook_Charts")
            
                Chart.CopyPicture Appearance:=xlScreen, Format:=xlPicture
        
        End With
        
        
        
'Paste Excel range in slide with dimensions
        With PPT.ActivePresentation.Slides(Slide_No)
             .Shapes.PasteSpecial
                With .Shapes(.Shapes.Count)
                     .LockAspectRatio = msoFalse
                     .Height = Img_Height * 28.3465
                     .Width = Img_Width * 28.3465
                     .Left = Img_Horizontal_Pos * 28.3465
                     .Top = Img_Vetical_Pos * 28.3465
                End With
        End With
        
        
        
        
 'Check if Below File Path & Name are same
        Next_File = ThisWorkbook.Sheets("Dashboard_Charts").Cells(i + 1, 6) 'extracts the file name from the path
        If Trim(Next_File) = "" Then
            Exit For
        Else
            If OpenBook_path = Next_File Then
                
            Else
                On Error Resume Next
                FileToOpen.Activate
                FileToOpen.Close False
                Set FileToOpen = Nothing
                On Error GoTo 0
            End If
        End If
    Next i
    On Error Resume Next
    FileToOpen.Close False
    
    
    ThisWorkbook.Sheets("Dashboard_Charts").Activate
    
    
With Application
   .ScreenUpdating = True
   .DisplayAlerts = True
   .EnableEvents = True
End With
    
    
    Successful_Msg
    PPT.Visible = True
    
    
'Determine how many seconds code took to run
  MinutesElapsed = Format((Timer - StartTime) / 86400, "hh:mm:ss")

'Notify user in seconds
  MsgBox "This code ran successfully in " & MinutesElapsed & " minutes", vbInformation
    
    
    
End Sub



Function wbOpen(wbName As String, wbO As Workbook) As Boolean
    On Error Resume Next
    Set wbO = Workbooks(wbName)
    wbOpen = Not wbO Is Nothing
End Function
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Your variable Chart is being assigned a single ChartObject object. So it should be declared as follows...

VBA Code:
Dim Chart As ChartObject

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
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