PDA

View Full Version : Deleting a chart object in Powerpoint



macroppt123
05-12-2011, 07:13 AM
Hi,
I tried the below in excel. If I need to delete a chart in a Powerpoint slide I cannot get the code to work(pasted the end.

--Excel code
Sub Macro1()
'
' Macro1 Macro
'
'
Sheets("HFIChart1").Select
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.ChartArea.Select
ActiveChart.Parent.Delete
End Sub


--Powerpoint code

Public Function delete_slide_object()
' Reference existing instance of PowerPoint
Set PPApp = GetObject(, "Powerpoint.Application")
' Reference active presentation
Set PPPres = PPApp.ActivePresentation
' Delete object in slide
'PPPres.Slides
ActiveSlide.ChartObjects.Activate
ActiveSlide.Parent.Delete
End Function

Regards,
macroppt123

Bob Phillips
05-12-2011, 08:02 AM
Public Function delete_slide_object()
' Reference existing instance of PowerPoint
Set PPApp = GetObject(, "Powerpoint.Application")
' Reference active presentation
Set PPPres = PPApp.ActivePresentation
' Delete object in slide
'PPPres.Slides
Set PPSLIDE = PPPres.Slides(1)
For Each PPShape In PPSLIDE.Shapes

If Left$(PPShape.Name, 7) = "Picture" Then

PPShape.Delete
End If
Next PPShape

Set PPShape = Nothing
Set PPSLIDE = Nothing
Set PPPres = Nothing
End Function

macroppt123
05-12-2011, 08:29 AM
Great Thanks . This worked perfectly and deleted the chart which was referenced by name in xls.

Another question. If the Powerpoint slide already has a chart object but not a name tag, and we need to delete this chart object, is there we can delete that object.

This is what is happening. The Delete function is deleting the chart that we just copied over. Is there a way to reference the chart existing in Powerpoint(no tag name) and delete that chart using the delete function. We are passing the sheet, chart_name, slide to the delete function.

Public Function delete_slide_object(sheet, chart_name, slide)

We are calling the delete funciton in the main module as below:

delete_slide_object "sheet2", "HFIChart2", 2



I have the below copy function

Public Function copy_chart(sheet, chart_name, slide, awidth, aheight, atop, aleft)
Sheets("HFIChart2").Select

'Sheets(Array("HFIChart2", "HFIChart1")).Select

' Set a VBE reference to Microsoft PowerPoint Object Library
Dim PPApp As PowerPoint.Application
Dim PPPres As PowerPoint.Presentation
Dim PPSlide As PowerPoint.slide
' Reference existing instance of PowerPoint
Set PPApp = GetObject(, "Powerpoint.Application")
' Reference active presentation
Set PPPres = PPApp.ActivePresentation
PPApp.ActiveWindow.ViewType = ppViewSlide
PPApp.ActiveWindow.View.GotoSlide (slide)
' Reference active slide
Set PPSlide = PPPres.Slides(PPApp.ActiveWindow.Selection.SlideRange.SlideIndex)
'ActiveSheet.ChartObjects(chart_name).Activate
ActiveChart.ChartArea.Copy
PPSlide.Select
'If PPSlide.Shapes.Count > 0 Then
'PPSlide.Shapes.Range.Delete
'Else
PPSlide.Shapes.PasteSpecial ppPastePNG
PPSlide.Select
PPSlide.Shapes(PPSlide.Shapes.Count).Select
Dim sr As PowerPoint.ShapeRange
Set sr = PPApp.ActiveWindow.Selection.ShapeRange
' Resize:
sr.Width = awidth
sr.Height = aheight
If sr.Width > 519 Then
sr.Width = 884
End If
If sr.Height > 420 Then
sr.Height = 525
End If
' Realign:
sr.Align msoAlignCenters, True
sr.Align msoAlignMiddles, True
sr.Top = atop
If aleft <> 0 Then
sr.Left = aleft
End If
'End If
Set PPSlide = Nothing
Set PPPres = Nothing
Set PPApp = Nothing
End Function

Bob Phillips
05-12-2011, 08:31 AM
The code I gave looked for any shape starting with Picture. I did this as you most likely will have Rectangles and the like. When I add charts to a slide, I ALWAYS name them, so I can easily control them.

macroppt123
05-12-2011, 08:36 AM
Sorry, I was pasting from the source that had not chart as I was in the midst of testing. I quit the fil without saving and started again. The copy works fine but the delete does not and this is probably because the slide has no name tag in the Powerpoint slide.

Since the slide exists , does it make to start with a brand new Powerpoint file to test the delete.

Is there a way to give a name tag to the existing chart in the Powerpoint slide? Else starting from a brand new Powerpoint file with a name tag for the chart object the only option?

Also note that the slide has other title and text which we want to retain. Just the chart object needs to be deleted.

Bob Phillips
05-12-2011, 09:47 AM
I think you have lost me. What does tag name have to do with it?