Copy data from Excel to powerpoint with vba - vba

I have created a code with vba which copy data from excel sheet and paste the same as picture in powerpoint slide, but its not working exactly as per my need.
It should copy data from each worksheets and paste it in a given powerpoint slide worksheet wise. Measn worksheet 1 data should be copied in slide 1 followed by worksheet 2 data in slide 2 and so on and at the end it should save the created ppt file.
But my code is copying and pasting all worksheets data overlaping each other in all the slides of the powerpoint.
Since i am new to vba i am not sure where i am going wrong with the below code:
Sub WorkbooktoPowerPoint()
Dim xlwksht As Worksheet
Dim MyRange As String
Dim MyRange1 As String 'Define another Range
Dim MyTitle As String
Dim oPPTApp As PowerPoint.Application
Dim oPPTShape As PowerPoint.Shape
Dim oPPTFile As PowerPoint.Presentation
Dim SlideNum As Integer
Dim oSlide As Slide
Dim strPresPath As String, strExcelFilePath As String, strNewPresPath As String
strPresPath = "C:\Users\FYI\PPT1.pptx"
strNewPresPath = "C:\Users\FYI\new1.pptx"
Set oPPTApp = CreateObject("PowerPoint.Application")
oPPTApp.Visible = msoTrue
Set oPPTFile = oPPTApp.Presentations.Open(strPresPath)
For Each oSlide In oPPTFile.Slides
i = oSlide.SlideNumber
oSlide.Select
MyRange = "B2:B5"
MyRange1 = "B8:B11"
For Each xlwksht In ActiveWorkbook.Worksheets
xlwksht.Select Application.Wait(Now + TimeValue("0:00:1"))
xlwksht.Range(MyRange).CopyPicture Appearance:=xlScreen, Format:=xlPicture
oSlide.Shapes.Paste.Select
oPPTApp.ActiveWindow.Selection.ShapeRange.Align msoAlignTops, True
oPPTApp.ActiveWindow.Selection.ShapeRange.Top = 65
oPPTApp.ActiveWindow.Selection.ShapeRange.Left = 7.2
oPPTApp.ActiveWindow.Selection.ShapeRange.Width = 400
xlwksht.Range(MyRange1).CopyPicture Appearance:=xlScreen, Format:=xlPicture
oSlide.Shapes.Paste.Select
oPPTApp.ActiveWindow.Selection.ShapeRange.Align msoAlignBottoms, True
oPPTApp.ActiveWindow.Selection.ShapeRange.Top = 250
oPPTApp.ActiveWindow.Selection.ShapeRange.Left = 7.2
oPPTApp.ActiveWindow.Selection.ShapeRange.Width = 400
Next xlwksht
Next
oPPTApp.Activate
oPPTFile.SaveAs strNewPresPath
oPPTFile.Close
oPPTApp.Quit
Set oPPTShape = Nothing
Set oPPTFile = Nothing
Set oPPTApp = Nothing
MsgBox "Presentation Created", vbOKOnly + vbInformation
End Sub

Please give this a shot. The main change is that I removed the For Each loop. You are already looping through the slides of the deck and can use the slide number to reference the Excel worksheet (they are numbered, as well). It was creating a mess, now it runs smoothly.
Sub WorkbooktoPowerPoint()
Dim xlwksht As Worksheet
Dim MyRange As String
Dim MyRange1 As String 'Define another Range
Dim oPPTApp As PowerPoint.Application
Dim oPPTShape As PowerPoint.Shape
Dim oPPTFile As PowerPoint.Presentation
Dim SlideNum As Integer
Dim oSlide As Slide
Dim strPresPath As String, strExcelFilePath As String, strNewPresPath As String
strPresPath = "C:\Users\FYI\PPT1.pptx"
strNewPresPath = "C:\Users\FYI\new1.pptx"
Set oPPTApp = CreateObject("PowerPoint.Application")
oPPTApp.Visible = msoTrue
Set oPPTFile = oPPTApp.Presentations.Open(strPresPath)
For Each oSlide In oPPTFile.Slides
i = oSlide.SlideNumber
' The following line was added after the OPs follow-up
If i > ActiveWorkbook.Sheets.Count Then Exit For
oSlide.Select
MyRange = "B2:B5"
MyRange1 = "B8:B11"
With ActiveWorkbook.Sheets(i)
.Range(MyRange).CopyPicture Appearance:=xlScreen, Format:=xlPicture
oSlide.Shapes.Paste.Select
With oPPTApp
.ActiveWindow.Selection.ShapeRange.Align msoAlignTops, True
.ActiveWindow.Selection.ShapeRange.Top = 65
.ActiveWindow.Selection.ShapeRange.Left = 7.2
.ActiveWindow.Selection.ShapeRange.Width = 400
End With
.Range(MyRange1).CopyPicture Appearance:=xlScreen, Format:=xlPicture
oSlide.Shapes.Paste.Select
With oPPTApp
.ActiveWindow.Selection.ShapeRange.Align msoAlignBottoms, True
.ActiveWindow.Selection.ShapeRange.Top = 250
.ActiveWindow.Selection.ShapeRange.Left = 7.2
.ActiveWindow.Selection.ShapeRange.Width = 400
End With
End With
Next
oPPTApp.Activate
oPPTFile.SaveAs strNewPresPath
oPPTFile.Close
oPPTApp.Quit
Set oPPTShape = Nothing
Set oPPTFile = Nothing
Set oPPTApp = Nothing
MsgBox "Presentation Created", vbOKOnly + vbInformation
End Sub

Related

Copying Charts from Excel to PowerPoint with Special Paste doesn't work anymore

i´m using VBA in Excel to go through all Chart-Sheets and copy them to a existing PowerPoint-presentation.
Until today the program worked fine. But since today it doesn´t copy the Charts to PowerPoint anymore.
The program works like: go through all Chart-Sheets and call a Helpfunction.
The helpfunction copys the ChartArea and pastes it with:
With pptApp.ActiveWindow
.ViewType = ppViewNormal
.View.PasteSpecial DataType:=ppPasteOLEObject, link:=msoTrue
End With
on the PowerPoint.
But the Problem here is that the PasteSpecial doesn´t work anymore and i don´t understand why.
Thank you for your help.
Here is the full code:
'Declaring the necessary Power Point variables (are used in both subs).
Dim pptApp As PowerPoint.Application
Dim pptPres As PowerPoint.Presentation
Dim pptSlide As PowerPoint.Slide
Dim pptSlideCount As Integer
Sub ChartsToPowerPoint()
Dim ws As Worksheet
Dim intChNum As Integer: intChNum = 0
Dim objCh As Object
Dim ppPres As String
Dim counter As Integer
Dim rng As Range
Dim oChart As Chart
Dim zähler As Integer
Set rng = ActiveWorkbook.Sheets("Daten").Range("A1:Z200").Find("Pfad für die Powerpoint")
ppPres = rng.Offset(1, 0).Value
counter = 4
For Each ws In ActiveWorkbook.Worksheets
intChNum = intChNum + ws.ChartObjects.Count
Next ws
zähler = ActiveWorkbook.Charts.Count
'Count the embedded charts.
'For Each ws In ActiveWorkbook.Worksheets
' intChNum = intChNum + ws.ChartObjects.Count
'Next ws
'Check if there are chart (embedded or not) in the active workbook.
If intChNum + ActiveWorkbook.Charts.Count < 1 Then
MsgBox "Sorry, there are no charts to export!", vbCritical, "Ops"
Exit Sub
End If
'Open PowerPoint and create a new presentation.
Set pptApp = New PowerPoint.Application
Set pptPres = pptApp.Presentations.Open(ppPres)
'Loop through all the embedded charts in all worksheets.
For Each ws In ActiveWorkbook.Worksheets
For Each objCh In ws.ChartObjects
Call pptFormat(objCh.Chart, counter)
Next objCh
Next ws
'Loop through all the chart sheets.
For Each objCh In ActiveWorkbook.Charts
Call pptFormat(objCh, counter)
counter = counter + 1
Next objCh
'Show the power point.
pptApp.Visible = True
'Cleanup the objects.
Set pptSlide = Nothing
Set pptPres = Nothing
Set pptApp = Nothing
'Infrom the user that the macro finished.
'MsgBox "The charts were copied successfully to the new presentation!", vbInformation, "Done"
End Sub
Private Sub pptFormat(xlCh As Chart, i As Integer)
'Formats the charts/pictures and the chart titles/textboxes.
Dim chTitle As String
Dim j As Integer
Dim tempName As String
Dim oLayout As CustomLayout
Dim counter As Integer
On Error Resume Next
'Get the chart title and copy the chart area.
xlCh.ChartArea.Copy
'Count the slides and add a new one after the last slide.
pptSlideCount = pptPres.Slides.Count
'tempName = GetLayout("Layout für QGs")
counter = i
'Set pptSlide = pptPres.Slides.Add(pptSlideCount + 1, Layout:=ppLayoutVerticalTitleAndTextOverChart)
pptApp.ActivePresentation.Slides(counter).Select
'pptApp.ActivePresentation.Slides(counter).Shapes.PasteSpecial DataType:=ppPasteOLEObject, link:=msoTrue
With pptApp.ActiveWindow
.ViewType = ppViewNormal
.View.PasteSpecial DataType:=ppPasteOLEObject, link:=msoTrue
End With
With pptApp.ActiveWindow.Selection.ShapeRange
.LockAspectRatio = msoFalse
'Oberer Rand 1 cm unter Standardtitel
.Top = 3.92 * 28.38
'Linker Rand 1.5 cm von linkem Folienrand
.Left = 4.51 * 28.38
'Eingefügte Tabelle auf Links und rechts 1,5 cm Rand skalieren
.Width = 24.23 * 28.38
'Bei Bedarf Höhe noch einstellen
'Hier ist jedoch zu beachten, dass das Object skaliert wird !!!
'Die Breite verändert sich dann
.Height = 12.7 * 28.38
.Line.Visible = msoFalse
End With
End Sub
Try using this code
Function PasteChartIntoSlide(theSlide As Object) As Object
Sleep 100
On Error Resume Next
theSlide.Shapes.Paste.Select
PPT.ActiveWindow.Selection.ShapeRange.LockAspectRatio = msoFalse
End Function
Function CopyChartFromExcel(theSlide As Object, cht As Chart) As Object
cht.CopyPicture Appearance:=xlScreen, Format:=xlPicture, Size:=xlScreen
End Function
Function PositionChart(leftPos As Integer, rightPos As Integer, widthPos As Integer, heightPos As Integer) As Object
Sleep 50
PPT_pres.Windows(1).Selection.ShapeRange.Left = leftPos
PPT_pres.Windows(1).Selection.ShapeRange.Top = rightPos
PPT_pres.Windows(1).Selection.ShapeRange.Width = widthPos
PPT_pres.Windows(1).Selection.ShapeRange.Height = heightPos
End Function
Function CopyPasteChartFull(Sld As Integer, cht As Chart, leftPos As Integer, rightPos As Integer, widthPos As Integer, heightPos As Integer) As Object
If PPT Is Nothing Then Exit Function
If PPT_pres Is Nothing Then Exit Function
Dim mySlide As Object
Dim myShape As Object
PPT_pres.Slides(Sld).Select 'Pointless line, just lets the user see what is happening
Set mySlide = PPT_pres.Slides(Sld)
With mySlide
.Select
'copy chart
CopyChartFromExcel mySlide, cht
'Paste chart
PasteChartIntoSlide mySlide
'Position Chart
PositionChart leftPos, rightPos, widthPos, heightPos
End With
'Clear The Clipboard
Application.CutCopyMode = False
End Function

Copy tables from different worksheet in excel and paste it in existing presentation

I have a specific excel workbook which has tables in different worksheets in different range.I want tables should be automatically copied from all the worksheet of my excel workbook and should be pasted in different slides of my existing ppt template.
I have created a code but giving error on range which I want to copy:
Sub newpp()
Dim pptapp As PowerPoint.Application
Dim pres As PowerPoint.Presentation
Dim preslide As PowerPoint.Slide
Dim shapepp As PowerPoint.Shape
Dim exappli As Excel.Application
Dim exworkb As Workbook
Dim xlwksht As Worksheet
Dim rng As Range
Dim myshape As Object
Dim mychart As ChartObject
Dim lastrow1 As Long
Dim lastcolumn1 As Long
Dim slidecount As Long
'Open powerpoint application
Set exappli = New Excel.Application
exappli.Visible = True
'activate powerpoint application
Set pptapp = New PowerPoint.Application
pptapp.Visible = True
pptapp.Activate
'open the excel you wish to use
Set exworkb = exappli.Workbooks.Open("C:\Users\ap\Desktop\Macro\Reference Sheet.xlsm")
'open the presentation you wish to use
Set pres = pptapp.Presentations.Open("C:\Users\ap\Desktop\Macro\new template.pptx")
'Add title to the first slide
With pres.Slides(1)
If Not .Shapes.HasTitle Then
Set shapepp = .Shapes.AddTitle
Else: Set shapepp = .Shapes.Title
End If
With shapepp
.TextFrame.TextRange.Text = "Gulf+ Market Segment Analysis Report" & vbNewLine & "P5 Week 04 FY17"
.TextFrame.TextRange.Font.Name = "Arial Black"
.TextFrame.TextRange.Font.Size = 24
.TextEffect.FontBold = msoTrue
.TextFrame.TextRange.Paragraphs.ParagraphFormat.Alignment = ppAlignLeft
End With
End With
'set the range
lastrow1 = exworkb.ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
lastcolumn1 = exworkb.ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
For Each xlwksht In exworkb.Worksheets
xlwksht.Select Application.Wait(Now + TimeValue("0.00:1"))
**'getting error in this line-------**
exworkb.ActiveSheet.Range(Cells(1, 1), Cells(lastrow1, lastcolumn1)).CopyPicture appearance:=xlScreen, Format:=xlPicture
slidecount = pres.Slides.Count
Set preslide = pres.Slides.Add(slidecount + 1, 12)
preslide.Select
preslide.Shapes.Paste.Select
pptapp.ActiveWindow.Selection.ShapeRange.Align msoAlignTops, msoTrue
pptapp.ActiveWindow.Selection.ShapeRange.Top = 65
pptapp.ActiveWindow.Selection.ShapeRange.Left = 72
pptapp.ActiveWindow.Selection.ShapeRange.Width = 700
Next xlwksht
End Sub
Replace your For Each xlwksht In exworkb.Worksheets loop with the modified loop below.
I made the following modifications to your code (so it will work):
Instead of Selecting the worksheet and then use ActiveSheet, use xlwksht, I've added the With xlwksht.
You need to search for the last row and column for each worksheet, so I've moved it inside the With statement.
There is no need to Select the slide every time in order to paste.
Some other modifications...
Modified For loop Code
For Each xlwksht In exworkb.Worksheets
With xlwksht
lastrow1 = .Cells(.Rows.Count, "A").End(xlUp).Row
lastcolumn1 = .Cells(1, .Columns.Count).End(xlToLeft).Column
' set the range
.Range(.Cells(1, 1), .Cells(lastrow1, lastcolumn1)).CopyPicture appearance:=xlScreen, Format:=xlPicture
Set preslide = pres.Slides.Add(pres.Slides.Count + 1, 12) ' <-- set the Slide
preslide.Shapes.Paste
With preslide.Shapes(preslide.Shapes.Count) '<-- modify the pasted shape properties
.Top = 65
.Left = 72
' etc...
End With
End With
Next xlwksht

How to paste excel data into powerpoint and still allow the user to edit data

I was wondering if there was a way of exporting/pasting an excel range into powerpoint, while still allowing the user to edit the result. The code I keep seeing on the internet pastes data from excel into powerpoint as a picture. Below is an example:
Sub export_to_ppt(ByVal sheetname As String, ByVal initialSelection As String) ', ByVal cols As Integer, ByVal rows As Integer)
Application.ScreenUpdating = False
Dim rng As Range
Dim PowerPointApp As Object
Dim myPresentation As Object
Dim mySlide As Object
Dim myShape As Object
'Copy Range from Excel
'Set rng = ThisWorkbook.ActiveSheet.Range("B17:D50")
'Create an Instance of PowerPoint
On Error Resume Next
'Is PowerPoint already opened?
Set PowerPointApp = GetObject(class:="PowerPoint.Application")
'Clear the error between errors
Err.Clear
'If PowerPoint is not already open then open PowerPoint
If PowerPointApp Is Nothing Then Set PowerPointApp = CreateObject(class:="PowerPoint.Application")
'Handle if the PowerPoint Application is not found
If Err.Number = 429 Then
MsgBox "PowerPoint could not be found, aborting."
Exit Sub
End If
On Error GoTo 0
'Optimize Code
Application.ScreenUpdating = False
'Create a New Presentation
Set myPresentation = PowerPointApp.Presentations.Add
'Add a slide to the Presentation
Set mySlide = myPresentation.Slides.Add(1, 12) '12 = ppLayoutBlank
'Copy Excel Range
Dim rowCount As Integer
Dim colcount As Integer
Dim i As Integer
Dim No_sheets As Integer
No_sheets = Worksheets("Control_Sheet").Range("AP2").Value + 2
For i = 3 To No_sheets
Worksheets("Control_Sheet").Activate
Worksheets("Control_Sheet").Cells(i, 42).Select
If Worksheets("Control_Sheet").Cells(i, 42).Value = sheetname Then
rowCount = Worksheets("Control_Sheet").Cells(i, 44).Value
colcount = Worksheets("Control_Sheet").Cells(i, 43).Value
GoTo resume_copy
End If
Next i
resume_copy:
Worksheets(sheetname).Activate
Worksheets(sheetname).Range(initialSelection).Select
Selection.Resize(rowCount, colcount).Select
Selection.Copy
'Paste to PowerPoint and position
Application.Wait Now + TimeValue("00:00:01")
mySlide.Shapes.PasteSpecial DataType:=2 '2 = ppPasteEnhancedMetafile
Set myShape = mySlide.Shapes(mySlide.Shapes.Count)
'Set position:
myShape.Left = 1
myShape.Top = 1
myShape.Width = 950
PowerPointApp.Visible = True
PowerPointApp.Activate
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
Replace:
mySlide.Shapes.PasteSpecial DataType:=2
With:
mySlide.Shapes.PasteSpecial DataType:=ppPasteOLEObject, DisplayAsIcon:=msoFalse, link:=msoFalse
Hope this helps,
TheSilkCode
You can, but this process was very buggy for me when running a large ppt deck. This works by using the ppt shapes position as the location of the paste. Use a template ppt slide to test, you can paste tables and graphs this way.
Dim myApp As PowerPoint.Application
Dim myPres As PowerPoint.Presentation
Dim myStatsSlide As PowerPoint.Slide
Set myApp = New PowerPoint.Application
Set myPres = myApp.ActivePresentation
Set myStatsSlide = myPres.Slides(1)
Dim mySheet As Worksheet
Set mySheet = ActiveSheet
'Copy table as table, not image
Dim mySumTable As Range
Set mySumTable = mySheet.Range("A1:C5")
mySumTable.Copy
myStatsSlide.Shapes.Placeholders(1).Select
myPres.Windows(1).View.Paste
'Copy Chart, as chart not image
Dim monoChart As ChartObject
'MONO CHART
monoChart.Select
ActiveChart.ChartArea.Copy
Debug.Print monoChart.Name
myStatsSlide.Shapes.Placeholders(2).Select
myPres.Windows(1).View.Paste
Debug.Print monoChart.Name

Run time error 9: subscript out of range (error while pasting)

I'm relatively new to VBA. I'm trying out the following VBA code but its throwing an error: 'Runtime error 09: subscript out of range'. This error occurs when i'm trying the paste operation in Graph 1 section of the code..
can someone help with figuring out as to where i'm going wrong. I have declared the presentation/slide etc. still i'm facing this problem..
Sub UK()
Dim oPPTApp As PowerPoint.Application
Dim oPPTFile As PowerPoint.Presentation
Dim oPPTShape As PowerPoint.Shape
Dim oPPTSlide As PowerPoint.Slide
Dim SlideNum As Integer
Dim mycells As Range
Set oPPTApp = CreateObject("PowerPoint.Application")
srcdir = "D:\WBR\Week 2"
srcfile = srcdir & "\" & Dir(srcdir + "\*.pptx")
Set oPPTFile = oPPTApp.Presentations.Open(srcfile)
Set oPPTSlide = oPPTFile.Slides(2)
' for graph 1
Set oPPTShape = oPPTFile.Slides(2).Shapes("Picture 3")
oPPTShape.Delete
ThisWorkbook.Sheets("New Charts").Activate
Sheets("New Charts").Shapes.Range(Array("Group 21")).Select
Selection.CopyPicture
oPPTApp.ActivePresentation.Slides(2).Select
Set Picture = oPPTSlide.Shapes.Paste
Picture.Name = "Picture 3"
With oPPTApp.ActivePresentation.Slides(2).Shapes("Picture 3")
.Top = Application.InchesToPoints(3)
.Left = Application.InchesToPoints(0.22)
End With
If I understand you correctly, you want to:
Open a saved presentation
Delete "Picture 3" from Slide 2
Copy Chart/Range from your excel sheet
Paste it in Slide 2
Name it as "Picture 3"
Set it's position on the slide
Well the below code does exactly that:
'Make Sure to load the PowerPoint Object Library
'Tools ---> References ---> Microsoft PowerPoint xx.x Object Library
Dim pptApp As PowerPoint.Application
Dim pptPres As PowerPoint.Presentation
Dim pptSlide As PowerPoint.Slide
Dim objChart As Chart
Set pptApp = New PowerPoint.Application
'presentation path here
srcdir = "C:\"
Set pptPres = pptApp.Presentations.Open(srcdir & "Presentation" & ".pptx")
Set pptSlide = pptPres.Slides(2)
For j = 1 To pptSlide.Shapes.Count
With pptSlide.Shapes(j)
If .Name = "Picture 3" Then
.Delete
End If
End With
Next j
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Change "Chart 1" to the name of your chart if you are copying a chart
Worksheets("New Charts").ChartObjects("Chart 1").Activate
Set objChart = Worksheets("New Charts").ChartObjects("Chart 1").Chart
objChart.CopyPicture
'If you are copying a range of cells then use
Worksheets("New Charts").Range("A1:A10").Copy
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Set MyPic = pptSlide.Shapes.PasteSpecial(DataType:=ppPasteEnhancedMetafile)
With MyPic
.Name = "Picture 3"
End With
With pptSlide.Shapes("Picture 3")
.Top = Application.InchesToPoints(3)
.Left = Application.InchesToPoints(0.22)
End With
'use this line to set focus to slide 2 if you want to
pptPres.Slides(2).Select
pptPres.Save 'use this line to save if you want to
Set pptSlide = Nothing
Set pptPres = Nothing
Set pptApp = Nothing

Unable to copy data from Excel to PPT using Macro

I have a macro that basically is supposed to copy ranges from excel spreadsheets and then paste them into a powerpoint file. So one excel sheet per slide.
Here is my macro so far:
Option Explicit
Sub ExportToPPT()
Dim PPAPP As PowerPoint.Application
Dim PPRES As PowerPoint.Presentation
Dim PPSlide As PowerPoint.Slide
Dim ppSRng As PowerPoint.ShapeRange
Dim XLAPP As Excel.Application
Dim XLwbk As Excel.Workbook
Dim xlWst As Excel.Worksheet
Dim XLRng As Excel.Range
Dim ppPathFile As String
Dim ppNewPathFile
Dim chartNum As Integer
Dim maxCharts As Integer
Debug.Print vbCrLf & " ---- EXPORT EXCEL RANGES POWERPOINT ----"
Debug.Print Now() & " - Exporting ranges to .ppt"
'CHANGE WHEN ADDING CHARTS - MUST ALSO ADD SLIDE to .PPT and change loop
Dim chartRng(1 To 9) As Excel.Range
Dim SlideNum As Integer
Dim SlideOffset As Integer
Set XLwbk = Excel.ActiveWorkbook
Set xlWst = XLwbk.Sheets("Test1")
'This accounts for the title slide and any others before the automatedpaste
SlideOffset = 1
Set chartRng(1) = XLwbk.Sheets("Test1").Range("A1:B15")
Set chartRng(2) = XLwbk.Sheets("Test2").Range("A1:E33")
Set chartRng(3) = XLwbk.Sheets("Test3").Range("A1:E33")
Set chartRng(4) = XLwbk.Sheets("Test4").Range("A1:E4")
Set chartRng(5) = XLwbk.Sheets("Test5").Range("A1:J14")
Set chartRng(6) = XLwbk.Sheets("Test6").Range("A1:I33")
Set chartRng(7) = XLwbk.Sheets("Test7").Range("A1:I11")
Set chartRng(8) = XLwbk.Sheets("Test8").Range("A1:I8")
' Create instance of PowerPoint
Set PPAPP = CreateObject("Powerpoint.Application")
PPAPP.Visible = True
' Open the presentation (Same folder as the Excel file)
ppPathFile = ActiveWorkbook.Path + "TestPPT.pptx"
Debug.Print ppPathFile
Set PPRES = PPAPP.Presentations.Open(ppPathFile)
PPAPP.ActiveWindow.ViewType = ppViewSlide
chartNum = 1
'Loop through all chart ranges
'CHANGE WHEN ADDING CHARTS
For chartNum = 1 To 9
SlideNum = chartNum + SlideOffset
Debug.Print "Chart number " & chartNum & " to slide number " & SlideNum
' Copy the range as a picture
chartRng(chartNum).CopyPicture Appearance:=xlScreen, Format:=xlPicture
'PowerPoint operations
Set PPSlide = PPAPP.ActivePresentation.AddSlide(1, _ **//New code**
PPAPP.ActivePresentation.SlideMaster.CustomLayouts.Item(2))
Debug.Print PPSlide.Name
PPSlide.Select
PPAPP.ActiveWindow.ViewType = ppViewSlide
'ppapp.ActivePresentation.Slides.
' Paste the range
'PPAPP.ActiveWindow.View.Slide (SlideNum)
PPAPP.ActiveWindow.View.Paste
'PPSlide.Shapes.Paste
'PPSlide.Shapes(0).Select
'PPSlide.Shapes.Paste.Select
' Align the pasted range
Set ppSRng = PPAPP.ActiveWindow.Selection.ShapeRange
With ppSRng
.LockAspectRatio = msoTrue
If (.Width / .Height) > 1.65 Then
.Width = 650
Else
.Height = 400
End If
End With
With ppSRng
'.Width = 650
.Align msoAlignCenters, True
.Align msoAlignMiddles, True
.IncrementTop 1.5
End With
Next chartNum
PPAPP.ActivePresentation.Slides(1).Select
PPAPP.ActiveWindow.ViewType = ppViewNormal
PPAPP.Activate
ppNewPathFile = ActiveWorkbook.Path & "\Test\TestPPT.pptx" & Format(Now(), "yyyymmdd_hhmmss")
PPAPP.ActivePresentation.SaveAs ppNewPathFile, ppSaveAsDefault
Debug.Print Now() & " - Finished"
End Sub
When I run the Macro it opens PowerPoint but stops and I get the following Error:
And when I debug it stops at this line:
Set PPSlide = PPAPP.ActivePresentation.Slides(SlideNum)
Any help on how to fix this would be great guys.
The error points to a counting problem that you've introduced in your code. Apparently, during the first iteration, it attempts to choose the second slide of a one-slide presentation (the second slide does not exist) and throwing an error.
I would assume this occurs because of your SlideOffset variable. Consider first adding a slide using before running Set PPSlide = PPAPP.ActivePresentation.Slides(SlideNum). Something like this:
Set pptLayout = PPAPP.ActivePresentation.Slides(1).CustomLayout
Set pptSlide = PPAPP.ActivePresentation.Slides.AddSlide(2, pptLayout)
Try using this
Set PPSlide = PPAPP.ActivePresentation.AddSlide(1, _
PPAPP.ActivePresentation.SlideMaster.CustomLayouts.Item(2))