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.
Related
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
I am new to using VBA so there is a chance this has been done before but I cant find a discussion on this. I have a template ppt already created that I need tables to be added to specific slides (see image).
Would it be easier to create a table and run it into Powerpoint? or Connect it with a pre-existing table in PPT that can be populated?
There are a couple of options you have. You can either export the Excel Range as an OLE Object, a PowerPoint Table Object, or a Linked OLE Object. Each has its own advantages and disadvantages but, in your case, I would just paste it as a table if the formatting doesn't have to match exactly.
Here is some code that will demonstrate pasting a range of cells to PowerPoint, from Excel, using VBA.
Sub ExportMultipleRangeToPowerPoint_Method2()
'Declare PowerPoint Variables
Dim PPTApp As PowerPoint.Application
Dim PPTPres As PowerPoint.Presentation
Dim PPTSlide As PowerPoint.Slide
Dim SldArray as Variant
'Declare Excel Variables
Dim ExcRng As Range
Dim RngArray As Variant
'Populate our array. This will define two ranges that exist in Sheet 1.
'This also populates our slide array which specifies which slide each range is pasted on.
'YOU WILL NEED TO CHANGE THIS IN ORDER FOR IT TO WORK ON YOURS!
RngArray = Array(Sheet1.Range("A1:F8"), Sheet1.Range("A10:F18"))
SldArray = Array(1, 2)
'Create a new instance of PowerPoint
Set PPTApp = New PowerPoint.Application
PPTApp.Visible = True
'Create a new Presentation
Set PPTPres = PPTApp.Presentations.Add
'Loop through the range array, create a slide for each range, and copy that range on to the slide.
For x = LBound(RngArray) To UBound(RngArray)
'Set a reference to the range
Set ExcRng = RngArray(x)
'Copy Range
ExcRng.Copy
'Enable this line of code if you receive an error about the range not being in the clipboard - This will fix that error by pausing the program for ONE Second.
'Application.Wait Now + #12:00:01 AM#
'GRAB AN EXISTING SLIDE.
Set PPTSlide = PPTPres.Slide(SldArray(x))
'Paste the range in the slide as a Table (Default).
PPTSlide.Shapes.PasteSpecial DataType:=ppPasteDefault
Next x
End Sub
Again, it really depends on how frequently you update it but most of the time I push people to keep it easier before making it harder. If you need more a step by step walkthrough, I do have a series on YouTube which you can follow along. It's especially helpful if you want to do more advanced stuff like positioning or even pasting as different data types. Here is the playlist: Excel to PowerPoint
Background: I am on the backend of an effort to capture and collate data collected in PowerPoint template form. A template was distributed. The result is ~150 PowerPoint 2010 presentations of ~30 slides each. ~15 of slides in each presentation contain an imbedded XLS.
Benefit to community: Examples of PowerPoint to Excel techniques and in general MS Office techniques vs. solely one MS Office tool.
Problem: I'm only an introductory VBA developer. I seem to find many examples how to get Excel data to PowerPoint, but not much (!) about this seemingly backward approach of data from PowerPoint into Excel. PeltierTech.com gets me close. I found some texts but need a solution before I can get through them.
Need:
1) Loop through all presentations (.PPTX) in a folder (open/close)
2) Inspect each slide in each presentation for an imbedded XLS
3) If found
a) Copy the imbedded source XLS range (not image)
b) Find the last row of the target XLS tab
c) Write the .PPTX name into tab column A
d) Paste the source XLS into target Excel column B
Finally I would prefer the "host" VBA be Excel.
The ideal result is a single .XLSX with ~15 tabs. The resultant data can be scrubbed for unique headers and converted into a pivotable dataset.
This doesn't appear the most to be the most challenging exercise. I think I'm hung on combining the two object models in a single set of procedures. (Yes, the references are correctly set ;-) )
THANK YOU!!!
Here is an example I wrote to extract one chart data from PP file (code is in a excel module). I've commented the code so you will be able to build your own loops to this, but the mechanics of extracting chart data from PP-file via Excel-VBA is as in the following block -
'"Microsoft PowerPoint xx.x Object Library" needed to be installed
'
Sub OpenPPandCopyChartData()
'Create an instance of Powerpoint
Dim PPT As Object
Set PPT = CreateObject(Class:="PowerPoint.Application")
'Open the powerpoint file
Dim pp As PowerPoint.Presentation
Set pp = PPT.Presentations.Open(Filename:=ThisWorkbook.Path & "\Presentation1.pptx", ReadOnly:=msoTrue)
'Select the wanted slide
Dim ps As PowerPoint.slide
Set ps = pp.Slides(1)
'Select the shape
Dim sh As PowerPoint.Shape
Set sh = ps.Shapes(2)
'You can make your own loop to check all the shapes if they contain a chart by using HasChart Method
Debug.Print sh.HasChart = msoTrue
'Select the shape that has chart and activate
sh.Chart.ChartData.Activate
'Set the activated workbook to a variable
Dim wb As Workbook
Set wb = sh.Chart.ChartData.Workbook
'Select the sheet the data is contained
Dim ws As Worksheet
Set ws = wb.Sheets(1)
'Select the range and copy
ws.UsedRange.Copy
'Set where to copy
ThisWorkbook.Sheets(1).Range("A1").PasteSpecial xlPasteValues
wb.Close
pp.Close
PPT.Quit
End Sub
The code opens the PPT.file in the same folder that the Excel file is in and selects Slide(1) and Shape(2) from that slide. Then it activates the chart data (if not present this code will cause a subscript out of range error, so on your own code a data checking will be needed) and copies it to the Excel that the sub was started in.
I am new to the forum, and a few months new to VBA. All my coding experience is in Matlab, which so far I find much easier than VBA. I have read the forum rules, and searched for this answer high and low, but to no avail.
I am trying to paste different excel ranges into different slides of a PowerPoint. I want to modify the code below (taken in snipets from different forum answers) so that the range is pasted into the corresponding PowerPoint slide I denote, regardless of what the active slide is. I have tried "gotoslide" and that didn't work, and used ppviewnormal, but that also didn't work. This code will then be repeated for different ranges on different slides. So far the code will only work when I am on the slide it was written for, and I know it has to do with issues regarding the active slide, but cannot seem to figure out how to modify the code. Thank you in advance for your help!
' Desired Range in Excel to be copied into ppt
Sheets("Summary").Activate
Range("A5:H40").Select
' Initialize PowerPoint Object Library
Set PPApp = GetObject(, "Powerpoint.Application")
' Reference active presentation
Set PPPres = PPApp.ActivePresentation
PPApp.ActiveWindow.ViewType = ppViewSlide
' Denote slide where range is to be copied
Set PPSlide = PPPres.Slides(5)
' Copy the range as a picture
Selection.CopyPicture Appearance:=xlScreen, _
Format:=xlPicture
' Paste the range
PPSlide.Shapes.Paste.Select
Remove ".Select" from last line
This is shorter version of your code:
' Initialize PowerPoint Object Library
Set PPApp = GetObject(, "Powerpoint.Application")
' Reference active presentation
Set PPPres = PPApp.ActivePresentation
' Copy the range as a picture
Sheets("Summary").Range("A5:H40").CopyPicture Appearance:=xlScreen, Format:=xlPicture
' Paste the range
PPPres.Slides(1).Shapes.Paste
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