VBA for Inserting a chart into a ppt slide - excel

Below is a procedure that I use to pull charts into a ppt from excel spreadsheets. However one thing I cannot figure out is how to insert the picture into the "object" instead of just pasting it onto th screen. (ie if I did a ppLayoutFourObjects, and sent fours charts to this slide, before adding another, I need to know how to paste the chart into each designated rectangle shown from the 4 Objects selection). I know that the first one seems to always be rectangle five, I can't get the code right. Please help.
This is all 2003 Office.
sub xls2ppt()
'I use this to pull charts into ppt from excel
Dim xlApp As Object
Dim xlWrkBook As Object
Dim lCurrSlide As Long
Set xlApp = CreateObject("Excel.Application")
' Open the Excel workbook
Set xlWrkBook = xlApp.Workbooks.Open("X:\Users\Admin\Desktop\Budget Overview.xls")
' Copy picture of the 1st chart object onto the clipboard
xlWrkBook.Worksheets(2).ChartObjects(1).CopyPicture
' Get the slide number
lCurrSlide = ActiveWindow.Selection.SlideRange.SlideNumber
' Paste the picture onto the PowerPoint slide.
ActivePresentation.Slides(lCurrSlide).Shapes.Paste
' Close the open workbook without saving changes
xlWrkBook.Close (False)
xlApp.Quit
Set xlApp = Nothing
Set xlWrkBook = Nothing
End Sub
Thanks for any help. VBA for PowerPoint is my weakest, but I am really in need to pick it up for work! Thanks guys!

AFAIK you can't paste a chart "into an object" in PowerPoint, even through the UI. In Word, you can paste into a textbox or into a table cell, but not in PowerPoint.
What you need to do instead is position the 4 pasted charts so that they're the right size & position - and that's easy enough to do...
Set oSlide = ActivePresentation.Slides(lCurrSlide)
Set oShape = oSlide.Shapes.Paste
oShape.Top = 10
oShape.Left = 10
oShape.Width = 100
oShape.Height = 100

Related

How do I close Embedded Excel processes in PowerPoint?

I am using VBA to open the open PowerPoint chart data in Excel and perform a series of actions, such as hiding/deleting rows & columns. I am using the chart.ChartData.Activate command to open the Excel. I had issues in the past with trying to close the workbook immediately after processing, using Workbook.Close(), so I left the Excels open. This has now become an issue with larger presentations and it's causing PowerPoint to crash and open back up in Recovery Mode. Even when I reinstate the Workbook.Close() command, sometimes these instances of Excel still remain open or I lose scope to them inside the routine.
I am processing the presentation on a slide by slide basis so I'm looking for a way to close these open instances all at once, after I'm done processing each slide.
Does anyone know how to access these hanging Excel processes? I've included a picture to better help explain where they reside.
I created an example routine below. I am using the ChartData.ActivateChartDataWindow command instead ChartData.Activate b/c when originally designing this, the Activate command caused the full Excel application to open instead of the ChartDataWindow and tremendously slowed down processing and would sometimes crash when repeated over and over again.
I also added an image of PowerPoint with the ChartDataWindows that are left open by my code.
Private Sub ClearColumnsInExcel()
'Set the slide
Dim slide As slide
Set slide = pptPres.Slides(1)
'Index through each shape on the slide
Dim shapeX As Integer
For shapeX = 1 To slide.Shapes.Count
'If this shape has a chart
If slide.Shapes(shapeX).Type = msoChart Then
'Set the chart
Dim chart As chart
Set chart = slide.Shapes(shapeX).chart
'Set the worksheet
Dim wks As Worksheet
Set wks = chart.ChartData.Workbook.Worksheets(1)
'Activate the workbook
chart.ChartData.ActivateChartDataWindow
'Clear target columns
'Remove objects from memomry
Set wks = Nothing
Set chart = Nothing
End If
Next shapeX
End Sub
I am not sure how you build you code, but you are just closing the workbook. To get the desired outcome, you need to quit the excel application.
I think something like this you do the trick:
Private Sub testSave()
Dim xlsApp As Excel.Application
Dim xlsWbk As Excel.Workbooks
Set xlsApp = New Excel.Application
xlsApp.Visible = True
Set xlswkb = xlsApp.Workbooks.Add 'creating a new wokbook just to test
'do your thing here
xlswkb.Close SaveChanges:=False ' close workbook without saving in this
example
xlsApp.Quit ' quitting the excel app
End Sub

Copy/Paste Special as Microsoft Graphic Object from Excel to Powerpoint

I have a report I create each week that copies a series of charts into a series of slides. Currently, I have VBA code that copies and pastes the charts as images, but the client has asked that the charts be graphic objects so they can inspect the source data from the PPT. Below is a simplified version of the relevant code:
Sub CreatePPT
Dim PP As PowerPoint.Application
Dim PPPres As PowerPoint.Presentation
Set PP = New PowerPoint.Application
Set PPPres = SCPP.Presentations.Open("C:\filepath\Template.pptx")
PP.Visible = True
'Select the slide I want to paste the chart to (I am not really sure why I need this line, but get an error if I do not have it)
PPPres.Slides(1).Select
'Copy the range where the chart is located
Sheets("Charts").Range("c10:D20").CopyPicture Appearance:=xlScreen, Format:=xlPicture
'Paste the chart to the slide
PPPres.Slides(1).Shapes.Paste.Select
Set PPPres = Nothing
Set PP = Nothing
End Sub
I have tried using paste special, but none of the available data types is anything like the "Paste as Graphic Object" that is available when I manually copy/paste special from excel to ppt manually. Does anyone know it is possible to replicate this type of paste special using VBA? Thanks in advance!
Instead of copying a range (presumably under a chart) as a picture, copy the chart itself, regular copy, not copy picture. Then do a straightforward paste:
Sheets("Charts").ChartObjects("Chart 1").Chart.ChartArea.Copy
PPPres.Slides(1).Shapes.Paste
But if you don't send along the Excel source workbook, and if they don't set up the links properly, they won't be able to view the data in Excel.

Is there a way to create excel worksheets out of a powerpoint presentation?

I want to create excel worksheets out of a powerpoint presentations where i can keep the format of the powerpoint slides and am able to adjust certain values. Does anyone know a way?
I heard of creating powerpoint slides out of a excel worksheet, though I need to go the other way around as I want to keep the format of the powerpoint slides but need to be able to adjust certain values. Does anyone know a way?
I basically need a Excel worksheet that looks and works like my powerpoint slide.
Here's what I have so far:
Dim PowerPointApp As Object
Dim myPresentation As Object
.
.
.
'Adds a slide to the presentation - is this also possible for worksheets?
Set mySlide = myPresentation.slides.Add(myPresentation.slides.Count + 1, 11) '11 = ppLayoutTitleOnly
' Pastes the copied range out of the excel into the Powerpoint
mySlide.Shapes.PasteSpecial DataType:=2
.
.
.
What I would like to do is to turn these around, I cant find any hints how to. Neither in books nor on the internet.
Here is a rudimentary approach. This code, which is run from within Excel, requires a reference to the powerpoint object library. It creates a new worksheet in Excel, one for each slide, and copies across the slide contents (ie. all the shapes). It positions the shapes per where they are located on the slide. A bit of a concept starter. Cheers.
Option Explicit
' ---> ADD REFERENCE TO MICROSOFT POWERPOINT OBJECT LIBRARY
Public Sub CreateSheetsFromSlides()
Dim vPowerPoint As PowerPoint.Application
Dim vPresentation As PowerPoint.Presentation
Dim vSlide As PowerPoint.Slide
Dim vPowerpointShape As PowerPoint.Shape
Dim vExcelShape
Dim vSheet As Worksheet
' Open the powerpoint presentation
Set vPowerPoint = New PowerPoint.Application
Set vPresentation = vPowerPoint.Presentations.Open("source.pptx")
' Loop through each powerpoint slide
For Each vSlide In vPresentation.Slides
' Create a new worksheet ... one per slide ... and name the worksheet same as the slide
Set vSheet = ThisWorkbook.Sheets.Add(, After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
vSheet.Name = vSlide.Name
MsgBox vSheet.Name
' Loop through each shape on the powerpoint slide and copy to the new worksheet
For Each vPowerpointShape In vSlide.Shapes
vPowerpointShape.Copy
' Create the shape on the worksheet and position it on the sheet at the same top/left as it is on the slide
vSheet.PasteSpecial
Set vExcelShape = vSheet.Shapes(vSheet.Shapes.Count)
vExcelShape.Top = vPowerpointShape.Top
vExcelShape.Left = vPowerpointShape.Left
Next
Next
vPresentation.Close
vPowerPoint.Quit
End Sub

Excel VBA Duplicating a Slide in PowerPoint

I am working on exporting Contents from Excel to PowerPoint. I have a blank formatted slide in my PowerPoint presentation which I Need to duplicate everytime and write on it. The Problem is that my code adds a new slide ahead of the current slide which is posing Problems in writing the Contents to the exact slide number. I want that the new slide should be added after the current slide.
Set pptSlide = oPPTApp.ActivePresentation.Slides(1).Duplicate.Item(1)
oPPTFile.Slides(SlideNum).Select
Set oPPTShape = oPPTFile.Slides(SlideNum).Shapes("Table 1")
any Suggestions ?
There's almost NEVER a good reason to select anything when automating PPT.
Assuming a shape named Table 1 on Slide 1, this should do what you want:
Dim oSl As Slide
Dim oSh As Shape
Set oSl = ActivePresentation.Slides(1).Duplicate()(1)
Set oSh = oSl.Shapes("Table 1")
ActivePresentation.Slides.Range(1).Cut
ActivePresentation.Slides.Paste -1
this function will move the first slide to the last. so it stands to reason you could figure out a formula to keep track of where you want the slide inserted.
if you need to know how many slides are in the range it's
ActivePresentation.Slides.Range.count

Import excel chart with spin button to powerpoint

I have a chart in excel with a spin button that changes a value to change the chart when pressed. I can import them into powerpoint by using insert -> object but this doesn't make the spin button usable.
I'm trying to get it so when showing the presentation I can click the spin button and the chart will correspondingly change, just like it does in excel. Is this possible?
THe spin button is connected to a macro and that's what's causing the chart to change in Excel.
You can copy the code from Excel and place it in a standard module in the Powerpoint file.
You will likely have to make some tweaks -- more or less, depending on how well the original macro was written (e.g., if it is full of ActiveSheet.This and ActiveChart.That then it's going to need a bit more tweaking than if it uses a Chart or ChartObject variable to represent the chart.
Here is an example of interacting with a powerpoint chart:
Sub TEST()
Dim sld As slide
Dim cht As Chart
Dim srs As Series
Set sld = ActivePresentation.Slides(1) 'Modify to be the correct slide #'
'You can hardcode the shape by index if you know it, otherwise'
' this method will apply to the FIRST chart object found on the slide'
For i = 1 To sld.Shapes.count
If sld.Shapes(i).Type = msoChart Then
Set cht = sld.Shapes(i).Chart
Exit For
End If
Next
'use a variable to interact with the Series:'
Set srs = cht.SeriesCollection(1) '<Modify as needed.'
'specify a particular formula for the series:'
'your macro likely changes the chart's source data/formula information'
' so something like this:'
srs.Formula = "=SERIES(Sheet1!$B$1,Sheet1!$A$2:$A$5,Sheet1!$B$2:$B$5,1)"
'Or you can display the series formula:'
MsgBox srs.Formula
'Or you can toggle the series axis group:'
With srs
If Not .AxisGroup = xlSecondary Then
.AxisGroup = xlSecondary
Else
.AxisGroup = xlPrimary
End If
'etc.'
'Basically anything you can do to an Excel chart in VBA, you can do in PPT.'
End With
End Sub
When you copy/paste anything from Excel to PowerPoint, you get an OLE object. What appears in PowerPoint is a Windows metafile picture of whatever you copied from Excel. You'd have to either activate the Excel content (doubleclick it within PPT's normal view or give it the appropriate Action Setting to have it activate during a slideshow). Then the spinner should also work within the instance of Excel that launches.
Or you could create another spinner in PPT and have it run code to modify the chart (using David's example code as a basis).

Resources