Generate multiple powerpoint files by updating chart - excel

I need to generate multiple powerpoint files by updating chart in 2nd slide on excel data available in each row(dynamic row counts)
I have a excel file with around 1000 rows(count is dynamic every time) and each row is a record, based on 1 row i have created a chart in excel itself which i need to copy in second slide of my existing ppt template. So in this way i need to generate 1000 ppts and save the files based on the name available in same row, can any one help me to resolve this query.
My logic is something like this.
Loop through all the rows
Create sheet
Create chart for first row
Copy paste in ppt fist slide
Then delete the chart or sheet in workbook
Repeat all the step till the end
Below is the code which i tried earlier where in i have created the chart in ppt and linked to the 1st row of my data file, but it only solves half of my problem that i can create only one report not multiple.
Sub Update()
Dim CName, pth
pth = ThisWorkbook.Path
Dim pptPres As PowerPoint.Presentation
Dim pptApp As PowerPoint.Application
Dim Sld As PowerPoint.Slide
Dim sh As PowerPoint.Shape
Dim wb As Workbook
Dim aLinks As Variant
Dim FName As String
Dim strPptTemplatePath As String
strPptTemplatePath = "C:\Users\DSS1080\Desktop\Business continuity planning\Report Template.pptx"
Application.ScreenUpdating = False
Set pptApp = CreateObject("PowerPoint.Application")
pptApp.Visible = msoTrue
Set pptPres = pptApp.Presentations.Open(strPptTemplatePath, untitled:=msoTrue)
NewLink = pth & "\" & ThisWorkbook.Name
pptApp.Activate
For Each Sld In pptPres.Slides
For Each sh In Sld.Shapes
If sh.Type = msoChart Then
sh.Chart.ChartData.Activate
Set wb = sh.Chart.ChartData.Workbook
aLinks = wb.LinkSources(xlExcelLinks)
wb.Sheets(1).Cells(100, 100).Value = aLinks
Oldfile = Cells(100, 100).Value
wb.ChangeLink Name:=Oldfile, NewName:=NewLink, Type:=xlExcelLinks
wb.Sheets(1).Cells(100, 100).Clear
wb.Close False
Set wb = Nothing
sh.Chart.ChartData.Activate
Set wb = sh.Chart.ChartData.Workbook
wb.Close False
Set wb = Nothing
End If
Next
Next
FName = Sheets("Quadrant").Range("C1").Text
CName = Left(strPptTemplatePath, Len(strPptTemplatePath) - 19) & FName
pptPres.SaveAs CName, ppSaveAsDefault
pptPres.Close
Set pptPres = Nothing
pptApp.Quit
Set pptApp = Nothing
Application.ScreenUpdating = True
End Sub

Related

Change the data shown on a powerpoint chart

I have an excel sheet that has the data, I have a powerpoint presentation that has a few charts. I need to run a report eveyday, so i am trying to automate it. I wrote a vba script to copy and paste the data from excel sheet to the chart in powerpoint . But i am unable to change the selection region(the data that is displayed on the graph eventhough there may be more data).
I have written the following script. Any help that helps me change the data to be displayed on the chart is appreciated.
Private Sub CommandButton1_Click()
Dim r As Range
Dim powerpointapp As Object
Dim mypresentation As Object
Dim myslide As Object
Dim myshape As Object
Dim ppath As String
Dim titlesh As Object
Dim tdate As String
Dim chartsh As Object
tdate = Format(Date, "mmmm dd, yyyy")
ppath = "path to ppt"
Set powerpointapp = CreateObject(class:="PowerPoint.Application")
Set mypresentation = powerpointapp.Presentations.Open(ppath)
Set myslide = mypresentation.Slides(1)
Set titlesh = myslide.Shapes("Dateh")
titlesh.TextFrame.TextRange.Text = tdate
Set myslide = mypresentation.Slides(2)
Set chartsh = myslide.Shapes("Chart 6")
chartsh.Chart.ChartData.Workbook.Sheets(1).Cells.Clear
Set r = ThisWorkbook.Worksheets("Weekly Tracking").Range("B84:C158")
r.Copy
chartsh.Chart.ChartData.Workbook.Sheets(1).Range("A2:B74").Value = r.Value
powerpointapp.Visible = True
powerpointapp.Activate
Application.CutCopyMode = False
End Sub
Depends on what your data looks like. You may use .end or usedrange or a loop to looking for keyword in order to locate the end of data.
Some code just to demo the idea:
Dim lastline1 As Long, lastline2 As Long
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Weekly Tracking")
lastline1 = ws.Cells(84, 2).End(xlDown).Row() 'Need to check whether row 84 is the last row, otherwise it may fly to 65536 or 1048576.
lastline2 = ws.UsedRange(ws.UsedRange.Rows.Count, ws.UsedRange.Columns.Count).Row
Set r = ThisWorkbook.Worksheets("Weekly Tracking").Range("B84:C" & lastline2)

Embed single sheet while pasting Excel chart to PPT using VBA

I have been working on automating the copying of editable charts from an excel workbook to a PowerPoint presentation through VBA. I got a lot of help through this link Using VBA to Paste Excel Chart with Data into PowerPoint which has sorted the copy-pasting bit.
Sub CopyChartSlide2()
Application.ScreenUpdating = False
Dim newPowerPoint As PowerPoint.Application
Dim activeSlide As PowerPoint.Slide
Dim cht1 As Excel.ChartObject
Dim Data As Excel.Worksheet
Dim pptcht1 As PowerPoint.Shape
Dim iLoopLimit As Long
Dim OpenPptDialogBox As Object
Dim SlideIndex As Long
Application.ScreenUpdating = False
'Look for existing instance
Set newPowerPoint = CreateObject("PowerPoint.Application")
Set OpenPptDialogBox = newPowerPoint.FileDialog(msoFileDialogOpen)
If OpenPptDialogBox.Show = -1 Then
newPowerPoint.Presentations.Open (OpenPptDialogBox.SelectedItems(1))
End If
Set activeSlide = newPowerPoint.ActivePresentation.Slides(1)
SlideIndex = 1
Set Data = Worksheets("Slide2")
Set cht1 = Data.ChartObjects("Chart1")
cht1.Copy
newPowerPoint.CommandBars.ExecuteMso "PasteExcelChartDestinationTheme"
DoEvents
On Error Resume Next
Do
DoEvents
Set pptcht1 = activeSlide.Shapes(activeSlide.Shapes.Count)
If Not pptcht1 Is Nothing Then Exit Do
iLoopLimit = iLoopLimit + 1
If iLoopLimit > 100 Then Exit Do
Loop
On Error GoTo 0
Debug.Print "iLoopLimit = " & iLoopLimit
With pptcht1
.Left = 103.68
.Top = 84.24
End With
iLoopLimit = 0
AppActivate newPowerPoint.Caption
Set activeSlide = Nothing
Set newPowerPoint = Nothing
Application.ScreenUpdating = True
End Sub
However, when the charts get pasted, it embeds the entire workbook instead of just the worksheet. Since I am working with a workbook of around 20 sheets, and each time a chart is pasted into the presentation, the entire workbook gets embedded and since there are many charts, it makes the PPT heavy and makes the process very slow. Is there a way to only embed the worksheet that is relevant to the chart?

Copying all charts from an Excel Sheet to a Powerpoint slide

I have built a workbook to facilitate the creation of a monthly report presentation I am in charge of. The workbook has some data sheets, some processing sheets and then numbered sheets which contain the charts I need to paste to the corresponding slide. So far, I've built the VBA for opening the PowerPoint template and looping through each excel sheet, and discriminating which sheet names are numeric, and then activating the corresponding slide on the powerpoint template.
Unlike other solutions to similar problems I've found, I'd like to copy all charts from each numbered sheet to each slide at a time, as they are different in shape, quantities and disposition for each sheet/slide. I've mostly only found people copying one chart at a time and pastying as image, which will also not work for me (I need to fine tune data labels and position on the final slide). Any hints as to how could I achieve that?
Here's what my code looks like so far:
Sub CriarSlides()
Dim pptApp As Powerpoint.Application
Dim pptPres As Powerpoint.Presentation
Dim strFileToOpen As Variant
Dim strFileName As String, Hosp As String
Dim datawb As Workbook
Dim xlsCounter As Integer, xlsSlide As Integer
Set datawb = ThisWorkbook
strFileToOpen = Application.GetOpenFilename _
FileFilter:="Powerpoint Files *.pptx (*.pptx),")
If strFileToOpen = False Then
Exit Sub
Else
Set pptApp = New Powerpoint.Application
pptApp.Visible = True
pptApp.Presentations.Open Filename:=strFileToOpen, ReadOnly:=msoFalse, Untitled:=msoTrue
Set pptPres = pptApp.Presentations(1)
End If
For xlsCounter = datawb.Worksheets.Count To 1 Step -1
If IsNumeric(datawb.Worksheets(xlsCounter).Name) Then
xlsSlide = datawb.Worksheets(xlsCounter).Name
' This is the problematic part
Debug.Print xlsSlide
End If
Next xlsCounter
End Sub
With the following modified code you can paste the chart-objects of each sheet in the corresponding slide:
Sub CriarSlides()
Dim pptApp As PowerPoint.Application, pptPres As PowerPoint.Presentation
Dim strFileToOpen As Variant, sh As Worksheet, ch As ChartObject
strFileToOpen = Application.GetOpenFilename(FileFilter:="Powerpoint Files *.pptx (*.pptx),")
If strFileToOpen = False Then Exit Sub
Set pptApp = New PowerPoint.Application
pptApp.Visible = True
Set pptPres = pptApp.Presentations.Open(fileName:=strFileToOpen, ReadOnly:=msoFalse)
For Each sh In ThisWorkbook.Sheets
If IsNumeric(sh.name) Then
For Each ch In sh.ChartObjects
ch.Copy
With pptPres.Slides(CLng(sh.name)).Shapes.Paste
.Top = ch.Top
.Left = ch.Left
.Width = ch.Width
.Height = ch.Height
End With
Next
End If
Next
End Sub

Error when pasting slide: The specified data type is unavailable

I am getting following error while pasting a slide in PowerPoint in the following line:
PPApp.ActiveWindow.View.PasteSpecial ppPasteOLEObject, msoFalse
Run-time error -2147188160 (80048240):View (unknown member) : Invalid request. The specified data type is Unavailable
I have run this code multiple times and it was running fine before.
Also, once the object/chart is copying; I am switching to PowerPoint to see if I can paste.
I can paste with all the options (As picture, As Embedded Image, etc.).
Here is the full code till I am getting error as it was not coming in comment section
Here is the code : Till the line where I get error
Sub export_to_ppt()
Set objExcel = CreateObject("Excel.Application")
'Keep the Importing master sheet address here:
Set objWorkbook = objExcel.Workbooks.Open("d:\Documents and Settings \Export to ppt.xlsm")
'Keep all the worksheets which you want to import from here:
Path = "D:\Office Documents\2013\ Latest Xcel\"
Filename = Dir(Path & "*.xlsm")
Do While Filename <> ""
Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
Dim sht As Workbooks
Set Sheet = Workbooks(Filename).Sheets("Issues Concern")
Sheet.Copy After:=ThisWorkbook.Sheets(1)
Set Sheet = Workbooks(Filename).Sheets("Key Initiatives Update")
Sheet.Copy After:=ThisWorkbook.Sheets(1)
Set Sheet = Workbooks(Filename).Sheets("Solution Update")
Sheet.Copy After:=ThisWorkbook.Sheets(1)
Set Sheet = Workbooks(Filename).Sheets("Overall Practice Status")
Sheet.Copy After:=ThisWorkbook.Sheets(1)
Set Sheet = Workbooks(Filename).Sheets("Practice Financials")
Sheet.Copy After:=ThisWorkbook.Sheets(1)
Workbooks(Filename).Close
Filename = Dir()
Loop
Dim PPApp As PowerPoint.Application
Dim PPPres As PowerPoint.Presentation
Dim PPSlide As PowerPoint.Slide
Dim SlideCount As Integer
Dim shptbl As Table
Dim oShape As PowerPoint.Shape
Dim SelectRange As Range
Dim SelectCell As Range
Set PPApp = CreateObject("PowerPoint.Application")
PPApp.Visible = msoTrue
'opening an existing presentation
Filename = "D:\Office Documents\Presentation1.pptx"
Set PPPres = PPApp.Presentations.Open(Filename)
Dim s As String
Dim i As Integer
i = 2
Line3:
MsgBox (ActiveSheet.Name)
If ActiveSheet.Name Like ("*Solution Update*") Then
GoTo Line1
ElseIf ActiveSheet.Name Like ("*Key Initatives Update*") Then
GoTo Line4
ElseIf ActiveSheet.Name Like ("*Issues Concern*") Then
GoTo Line13
End If
Set PPSlide = PPPres.Slides.Add(SlideCount + 1, ppLayoutTitleOnly)
PPSlide.Shapes(1).TextFrame.TextRange.Text = "Practice Financials - " & Sheets(i).Range("AH1").Value & " "
'PPSlide.Shapes(1).TextFrame.TextRange.Text = Sheets(1).Range("B1").Value
'format header
With PPSlide.Shapes(1).TextFrame.TextRange.Characters
.Font.Size = 24
.Font.Name = "Arial Heading"
'.Font.Color = vbBlue
End With
Range("A1:K7").Select
Selection.Copy
PPApp.ActiveWindow.View.GotoSlide PPSlide.SlideIndex ' activate the slide no
'PPApp.Activate
PPApp.ActiveWindow.View.PasteSpecial ppPasteOLEObject, msoFalse ' paste using pastespecial method
'PPApp.ActiveWindow.View.PasteSpecial ppPasteEnhancedMetafile
'PPApp.ActiveWindow.View.PasteSpecial (ppPasteMetafilePicture)
I was having the same problem and it happened as I was trying to export from Excel to PowerPoint without the PowerPoint reference, using it as object. The tricky thing was that sometimes it worked, other times it won´t. So after some testing I found out that it depends on the state of the PowerPoint View, if it is showing Thumbnails or a normal Slide view.
To fix it, set the ViewType as normal before pasting.
PPAP.ActiveWindow.ViewType = ppViewNormal
or
PPAP.ActiveWindow.ViewType = 9
PPAP stands for power point application object.
Further to my comments above, this works for me. Let's say your sheet1 looks like this
Paste this code in a module.
Option Explicit
Sub Sample()
Dim PPApp As PowerPoint.Application
Dim PPPres As PowerPoint.Presentation
Dim PPSlide As PowerPoint.Slide
Dim SlideCount As Long
Dim ws As Worksheet
Dim rng As Range
Dim Filename As String
Set ws = ThisWorkbook.Sheets("Sheet1")
Set rng = ws.Range("A1:K7")
Set PPApp = CreateObject("PowerPoint.Application")
PPApp.Visible = msoTrue
'opening an existing presentation
Filename = "C:\Presentation1.pptx"
Set PPPres = PPApp.Presentations.Open(Filename)
SlideCount = PPPres.Slides.count
Set PPSlide = PPPres.Slides.Add(SlideCount + 1, ppLayoutTitleOnly)
With PPSlide.Shapes(1).TextFrame.TextRange
.Text = "Practice Financials - " & _
ws.Range("AH1").Value & " "
With .Characters.Font
.Size = 24
.Name = "Arial Heading"
End With
End With
rng.Copy
DoEvents
PPSlide.Shapes.PasteSpecial DataType:=ppPasteOLEObject, Link:=msoFalse
End Sub
OUTPUT

Resizing a excel pasted object in powerpoint with vba

I've cobbled together a VBA script (I'm no expert, but thanks to the kind folks around here, I've been able to get something together and mostly working) to copy from multiple excel sheets into a powerpoint file (used a template, as you will see from the code.
Sub ATestPPTReport()
Dim PPApp As PowerPoint.Application
Dim PPSlide As PowerPoint.Slide
Dim PPPres As PowerPoint.Presentation
Set PPApp = CreateObject("Powerpoint.Application")
Dim SlideNum As Integer
Dim PPShape As PowerPoint.Shape
Set XLApp = GetObject(, "Excel.Application")
''define input Powerpoint template
Dim strPresPath As String, strExcelFilePath As String, strNewPresPath As String
''# Change "strPresPath" with full path of the Powerpoint template
strPresPath = "C:\template.ppt"
''# Change "strNewPresPath" to where you want to save the new Presentation to be created
strNewPresPath = "C:\macro_output-" & Format(Date, "dd-mmm-yyyy") & ".ppt"
Set PPPres = PPApp.Presentations.Open(strPresPath)
PPPres.Application.Activate
PPApp.Visible = True
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''define destination slide
SlideNum = 1
PPPres.Slides(SlideNum).Select
Set PPShape = PPPres.Slides(SlideNum).Shapes("slide1box")
Set PPSlide = PPPres.Slides(PPApp.ActiveWindow.Selection.SlideRange.SlideIndex)
''define source sheet
Sheets("Info1").Activate
'copy/paste from
XLApp.Range("Info1Block").Copy
PPSlide.Shapes.PasteSpecial DataType:=ppPasteOLEObject, Link:=msoFalse
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''define destination slide
SlideNum = 2
PPPres.Slides(SlideNum).Select
' Set PPShape = PPPres.Slides(SlideNum).Shapes("slide2box")
Set PPSlide = PPPres.Slides(PPApp.ActiveWindow.Selection.SlideRange.SlideIndex)
''define source sheet
Sheets("Info2").Activate
'copy/paste from
XLApp.Range("Info2Block").Copy
PPSlide.Shapes.PasteSpecial DataType:=ppPasteOLEObject, Link:=msoFalse
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Close presentation
PPPres.SaveAs strNewPresPath
'PPPres.Close
'Quit PowerPoint
'PPApp.Quit
' MsgBox "Presentation Created", vbOKOnly + vbInformation
' Clean up
Set PPSlide = Nothing
Set PPPres = Nothing
Set PPApp = Nothing
End Sub
My problem is: how do I resize/reposition the object once it's been pasted?
The function "PasteSpecial" returns a shape object, which you can use to resize or reposition.
For example:
Dim ppShape as PowerPoint.Shape
set ppShape = PPSlide.Shapes.PasteSpecial(DataType:=ppPasteOLEObject, Link:=msoFalse)
Then you can use this shape object to resize it. For example:
ppShape.Height = xyz
ppShape.Top = abc
etc etc.
Hope this helps.
Vikas B
This has been working for me:
Set shp = myPresentation.Slides(x).Shapes.PasteSpecial(DataType:=2)
shp.Left = topLeft + 1
shp.Top = midTop + 1
shp.Width = midLeft - topLeft - 1
Note the variables are set locally to place the image where I want it in relation to the slide. You can easily replace with integers.
It also works for DataType:=10 items as well

Resources