Link to home
Create AccountLog in
Avatar of montrof
montrofFlag for United States of America

asked on

VBA copying images to PPT

I have been using the below function to copy images to excel for a long time.  I have always used the named range to copy the chart.  Now I want to change it so that it selects the actual chart and not the range around the chart.  

This is the code I was using

 
Range(strRange).Copy
  Range("A1").Select
  Debug.Print Err.Description

Open in new window


This is what I want to change it to

     
ActiveWorkbook.Range("Excel" & strRange).Select
     ActiveSheet.ChartObjects(strRange).Activate
     ActiveChart.ChartArea.Copy
     Range("A1").Select

Open in new window


it seems to error out at the range select I am just using the range select to make sure I am on the right sheet that the chart is loacted on .  

Here is the full function


Function CopyRangeToPPT(oPPTApp As PowerPoint.Application, intSlideNum As Integer, intLeft As Integer, intWidth As Integer, intHeight As Integer, intTop As Integer, strRange As String) As String
    Dim shShape As Object, strError As String

    'delete shape if it exists on slide
    On Error Resume Next
    oPPTApp.ActivePresentation.Slides("Slide_" & strRange).Shapes("ExcelSlide_" & strRange).Delete
    If Err.Number <> 0 Then CopyRangeToPPT = Err.Number & " " & Err.Description & ": " & strRange & vbCrLf
    Err.Clear
    ' Select the range then copy it.

    On Error GoTo 0
     ActiveWorkbook.Range("Excel" & strRange).Select
     ActiveSheet.ChartObjects(strRange).Activate
     ActiveChart.ChartArea.Copy
     Range("A1").Select
    
'        ActiveSheet.ChartObjects("strRange").Activate
'        ActiveChart.ChartArea.Copy
'
'
'    ActiveSheet.ChartObjects("Slide1").Activate
'    ActiveChart.ChartArea.Copy
'
    

'    Range(strRange).Copy
'    Range("A1").Select
'    Debug.Print Err.Description

    ' Paste the range

    Set shShape = oPPTApp.ActivePresentation.Slides(intSlideNum).Shapes.PasteSpecial(ppPasteEnhancedMetafile)

    ''    oPPTApp.Run ("ResizeShapeSize")

    ' Align the pasted range

    With shShape
        .LockAspectRatio = 0
        .Left = intLeft
        .Top = intTop
        .Width = intWidth
        .Height = intHeight
        .Name = "ExcelSlide_" & strRange
    End With
    
   
End Function

Open in new window



Thanks,
Montrof
ASKER CERTIFIED SOLUTION
Avatar of andrewssd3
andrewssd3
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of montrof

ASKER

I am getting Run-time error '438' Object doesn't support this property or method

Thanks,
Montrof
Avatar of montrof

ASKER

I've requested that this question be closed as follows:

Accepted answer: 0 points for montrof's comment #a39327698

for the following reason:

Ok I got it to work, I had an error.  Thanks for the help.

Montrof