I have a data set that Im trying to tur into automatic PowerPoint slides.The number of rows changes weekly so the range has to be variable.
this is how my data looks like
So far i've been able to create a slide for each title, copy the headers as an image and add copy the value of the 16th cell to each slide, but now i want to copy the values of each row its looping as an image but only from columns B to O.
So that the First slide would have (B1:O1)
The second would have (B2:O2)
But i haven figured out how to do it.
I wanted to use "rowShape" as the name for the rows image
Here's my code so far:
Option Explicit
Sub Data_to_PowerPoint()
Dim newPowerPoint As PowerPoint.Application
Dim activeSlide As PowerPoint.Slide
Dim ExcelRow As Range
Dim CellRange As Range
Dim SlideText As Variant
Dim lr As Long
Dim hdr As Range
Dim myShape As Object
Dim rowShape As Object
'The first range of cells in the table.
lr = Cells(Rows.Count, "A").End(xlUp).Row
Set CellRange = Sheets("TicketSummary").Range("A1:A" & lr)
'Determine header range.
Set hdr = Sheets("TicketSummary").Range("B1:O1")
'Look for existing powerpoint instance
On Error Resume Next
Set newPowerPoint = GetObject(, "PowerPoint.Application")
On Error GoTo 0
'Create a PowerPoint
If newPowerPoint Is Nothing Then
Set newPowerPoint = New PowerPoint.Application
End If
'Setup the presentation in PowerPoint
If newPowerPoint.Presentations.Count = 0 Then
newPowerPoint.Presentations.Add
End If
'Make PowerPoint visible
newPowerPoint.Visible = True
'Loop through each chart in the Excel worksheet and paste them into the PowerPoint
For Each ExcelRow In CellRange
'Add a new slide
newPowerPoint.ActivePresentation.Slides.Add newPowerPoint.ActivePresentation.Slides.Count + 1, ppLayoutText
newPowerPoint.ActiveWindow.View.GotoSlide newPowerPoint.ActivePresentation.Slides.Count
Set activeSlide = newPowerPoint.ActivePresentation.Slides(newPowerPoint.ActivePresentation.Slides.Count)
'Create the body text for the slide
SlideText = Cells(ExcelRow.Row, 16)
'Input the title of the slide
activeSlide.Shapes(1).TextFrame.TextRange.Text = ExcelRow.Value
'Input the body text for the slide
activeSlide.Shapes(2).TextFrame.TextRange.Text = SlideText
'Copy Header.
hdr.Copy
'Paste header to PowerPoint and position
activeSlide.Shapes.PasteSpecial DataType:=2 '2 = ppPasteEnhancedMetafile
Set myShape = activeSlide.Shapes(activeSlide.Shapes.Count)
'Set position:
myShape.Left = 60
myShape.Top = 152
Next
Set activeSlide = Nothing
Set newPowerPoint = Nothing
End Sub
Option Explicit
Sub Data_to_PowerPoint()
Dim pp As PowerPoint.Application, pps As PowerPoint.Slide
Dim lr As Long, i As Long, n As Long
'Look for existing powerpoint instance
On Error Resume Next
Set pp = GetObject(, "PowerPoint.Application")
On Error GoTo 0
'Create a PowerPoint
If pp Is Nothing Then
Set pp = New PowerPoint.Application
End If
'Setup the presentation in PowerPoint
If pp.Presentations.Count = 0 Then
pp.Presentations.Add
End If
'Make PowerPoint visible
pp.Visible = True
'The first range of cells in the table.
With Sheets("TicketSummary")
lr = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = 2 To lr
' create slide
pp.ActivePresentation.Slides.Add i - 1, ppLayoutText
pp.ActiveWindow.View.GotoSlide i - 1
Set pps = pp.ActivePresentation.Slides(i - 1)
'Input the title of the slide
pps.Shapes(1).TextFrame.TextRange.Text = .Cells(i, "A")
'Input the body text for the slide
pps.Shapes(2).TextFrame.TextRange.Text = .Cells(i, "P") ' col 16
' copy header
' Paste to PowerPoint and position
' paste 2 = ppPasteEnhancedMetafile 3 ppPasteMetafilePicture
n = pps.Shapes.Count
.Range("B1:O1").Copy
Application.Wait Now + TimeSerial(0, 0, 1) ' 1 second wait
pps.Shapes.PasteSpecial DataType:=2
' wait for shape to be pasted
Do
DoEvents
Loop Until pps.Shapes.Count > n
Application.CutCopyMode = False
'Set position:
With pps.Shapes(n + 1)
.Left = 60
.Top = 182
End With
' copy row
n = pps.Shapes.Count
.Range("B1:O1").Offset(i - 1).Copy
Application.Wait Now + TimeSerial(0, 0, 1) ' 1 second wait
pps.Shapes.PasteSpecial DataType:=2
' wait for shape to be pasted
Do
DoEvents
Loop Until pps.Shapes.Count > n
Application.CutCopyMode = False
'Set position:
With pps.Shapes(n + 1)
.Left = 60
.Top = 202
End With
Next
End With
MsgBox lr - 1 & " slides created"
End Sub
Related
I copy paste my ranges from XL to PPT.
It creates new slides and adds range as picture.
How can I make it paste range to existing slide, instead of creating slide?
Right now it loops and paste copied picture to new slide.
I get error in this line: "Set PPslide = ActiveWindow.Selection.SlideRange(1)"
Thanks.
Dim PP As PowerPoint.Application
Dim PPpres As PowerPoint.Presentation
Dim PPslide As Object
Dim k As Long, i As Long
Dim PpShape As PowerPoint.Shape
Dim SlideTitle As String
Dim SlideNum As Integer
Dim WSrow As Long
Dim Sh As Shape
Dim Rng As Range
Dim myShape As Object
'Open PowerPoint and create new presentation
Set PP = GetObject(class, "PowerPoint.Application")
PP.Visible = True
Set PPpres = PP.Presentations.Open(Filename:="C:\Users\Mac\Desktop\test\PPT.pptx")
k = 1
For i = 6 To Cells(70, Columns.Count).End(xlToLeft).Column Step 10
With Cells(70, i)
.Resize(1, 10).CopyPicture Appearance:=xlPrinter, Format:=xlPicture
DoEvents
DoEvents
.Offset(15, 0).PasteSpecial
DoEvents
DoEvents
End With
'Give the last pasted picture a name.
ActiveSheet.Pictures(ActiveSheet.Pictures.Count).Name = "Element" & k
' Here you're adding a new slide, which you've said you don't want.
' Comment it out:
Set PPslide = PPpres.Slides.Add(1, ppLayoutBlank)
' Assuming the active slide, no need to anywhere:
PP.ActiveWindow.View.GotoSlide (1)
' Change this to use the currently active slide
Set PPslide = PPpres.Slides(1)
Set PPslide = ActiveWindow.Selection.SlideRange(1) '<-- I get error here.
'Paste to PowerPoint and position
PPslide.Shapes.PasteSpecial DataType:=2 '2 = ppPasteEnhancedMetafile
Set myShape = PPslide.Shapes(PPslide.Shapes.Count)
'Set position:
myShape.Left = 40
myShape.Top = 180
myShape.Height = 220
myShape.Width = 850
'Make PowerPoint Visible and Active
PP.Visible = True
PP.Activate
'Clear The Clipboard
Application.CutCopyMode = False
Next i
k = k + 1
Assuming you want to paste into the currently active slide:
' Here you're adding a new slide, which you've said you don't want.
' Comment it out:
' Set PPslide = PPpres.Slides.Add(1, ppLayoutBlank)
' Assuming the active slide, no need to anywhere:
' PP.ActiveWindow.View.GotoSlide (1)
' Change this to use the currently active slide
' Set PPslide = PPpres.Slides(1)
Set PPslide = ActiveWindow.Selection.SlideRange(1)
'Paste to PowerPoint and position
PPslide.Shapes.PasteSpecial DataType:=2 '2 = ppPasteEnhancedMetafile
Set myShape = PPslide.Shapes(PPslide.Shapes.Count)
Hello I am using VBA to loop for ranges and copy as picture and paste to PPT slide.
I want to know is there possibility to include 1 more range to code.
So VBA will copy paste 2 ranges as picture.
range is in following code. Works perfectly fine.
range is single cell ( B1 )
Can anyone give me solution how can I add multiple range ( as picture ) to the slide?
Dim PP As PowerPoint.Application
Dim PPpres As PowerPoint.Presentation
Dim PPslide As Object
Dim PpShape As PowerPoint.Shape
Dim SlideTitle As String
Dim SlideNum As Integer
Dim WSrow As Long
Dim Sh As Shape
Dim Rng As Range
Dim myShape As Object
'Open PowerPoint and create new presentation
Set PP = GetObject(class, "PowerPoint.Application")
PP.Visible = True
Set PPpres = PP.Presentations.Open(Filename:="C:\Users\Mac\Desktop\test\PPT.pptx")
'Specify the chart to copy and copy it
For i = 6 To Cells(70, Columns.Count).End(xlToLeft).Column Step 10
With Cells(70, i)
.Resize(1, 10).CopyPicture Appearance:=xlPrinter, Format:=xlPicture
DoEvents
DoEvents
.Offset(15, 0).PasteSpecial
Range("B1").CopyPicture Appearance:=xlPrinter, Format:=xlPicture
DoEvents
DoEvents
.Offset(25, 0).PasteSpecial
End With
'Give the last pasted picture a name.
ActiveSheet.Pictures(ActiveSheet.Pictures.Count).Name = "Chart" & k
'Increase the count for naming pictures by 1
k = k + 1
Set PPslide = PPpres.Slides.Add(1, 10)
PP.ActiveWindow.View.GotoSlide (1)
Set PPslide = PPpres.Slides(1)
'Paste to PowerPoint and position
PPslide.Shapes.PasteSpecial DataType:=2 '2 = ppPasteEnhancedMetafile
Set myShape = PPslide.Shapes(PPslide.Shapes.Count)
'Set position:
myShape.Left = 20
myShape.Top = 180
myShape.Height = 250
myShape.Width = 950
'Make PowerPoint Visible and Active
PP.Visible = True
PP.Activate
'Clear The Clipboard
Application.CutCopyMode = False
Next i
The function .CopyPicture does not work on multiple ranges that aren't connected to each other - trying it would return the error message:
Run-time error 1004: this action won't work on multiple selections
So you'll need to execute a separate .CopyPicture for your separate Range (B1), maybe something like (details depending on what exactly you're trying to do):
With Cells(70, i)
.Resize(1, 10).CopyPicture Appearance:=xlPrinter, Format:=xlPicture
.Offset(150, 0).PasteSpecial
Range("B1").CopyPicture Appearance:=xlPrinter, Format:=xlPicture
.Offset(140, 0).PasteSpecial
End With
If you want the multiple ranges to return just one single image, you'll probably have to merge the resulting images in a separate step.
I am trying to prepare a presentation from Excel. As of now VBA code is preparing "n number of "presentations as per no of times Loop runs. I want Code to generate just 1 presentation with all slides combined. Fist Macro "Addnumber" is run, which run Macro "ExcelRangeToPowerPoint". Its Macro "ExcelRangeToPowerPoint"which need to add slides for every loop of Macro "Addnumber"
Please Support
Sub AddNumber()
Dim Ws As Worksheet
Dim rngSel As Range
Dim rng As Range
Dim Num As Double
Dim i As Long
Dim j As Long
Dim lAreas As Long
Dim lRows As Long
Dim lCols As Long
Dim Arr() As Variant
Set rngSel = Worksheets("Sheet1").Range("A5:A30")
Do Until Range("A30") = Range("A3")
Num = 26
For Each rng In rngSel.Areas
If rng.Count = 1 Then
rng = rng + Num
Else
lRows = rng.Rows.Count
lCols = rng.Columns.Count
Arr = rng
For i = 1 To lRows
For j = 1 To lCols
Arr(i, j) = Arr(i, j) + Num
Next j
Next i
rng.Value = Arr
End If
Call ExcelRangeToPowerPoint
Next rng
Loop
End Sub
Sub ExcelRangeToPowerPoint()
'PURPOSE: Copy/Paste An Excel Range Into a New PowerPoint Presentation
Dim rng As Range
Dim rng2 As Range
Dim PowerPointApp As Object
Dim myPresentation As Object
Dim mySlide As Object
Dim myShape As Object
Dim mySize As PageSetup
Dim Addtitle As Shape
Dim DateT As String
'Copy Range from Excel
Set rng = Worksheets("Sheet1").Range("E2:M30")
Set rng2 = Worksheets("Sheet1").Range("F2")
Set rng3 = Worksheets("Sheet1").Range("B3")
'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, 11)
'11 = ppLayoutTitleOnly
'Change Theme and Layout
mySlide.ApplyTheme "C:\Users\davinder.sond\AppData\Roaming\Microsoft\Templates\Document Themes\DefaultTheme.thmx"
myPresentation.PageSetup.SlideSize = 3
myPresentation.Slides(1).Shapes.Title.TextFrame.TextRange.Text = rng2
myPresentation.Slides(1).Shapes.Title.Left = 59
myPresentation.Slides(1).Shapes.Title.Top = 10
myPresentation.Slides(1).Shapes.Title.Height = 30
myPresentation.Slides(1).Shapes.Title.Width = 673
With myPresentation.Slides(1).Shapes.Title
With .TextFrame.TextRange.Font
.Size = 24
.Name = "Arial"
.Bold = True
.Color.RGB = RGB(255, 255, 255)
End With
End With
'Copy Excel Range
rng.Copy
'Paste to PowerPoint and position
mySlide.Shapes.PasteSpecial DataType:=2 '2 = ppPasteEnhancedMetafile
Set myShape = mySlide.Shapes(mySlide.Shapes.Count)
'Set position:
myShape.LockAspectRatio = 0
myShape.Left = 12
myShape.Top = 55
myShape.Height = 475
myShape.Width = 756
'Make PowerPoint Visible and Active
PowerPointApp.Visible = True
PowerPointApp.Activate
DateT = Format("h:mm:ss")
'Clear The Clipboard
Application.CutCopyMode = False
myPresentation.SaveAs "C:\Project Control CCJV\ExperimentsPunch\" & rng3 & ".pptm"
PowerPointApp.Quit
End Sub
You are creating a new presentation everytime you call Set myPresentation = PowerPointApp.Presentations.Add within ExcelRangeToPowerPoint().
You can either try to open/close the Presentation outside of ExcelRangeToPowerPoint() and add a parameter to the function like ExcelRangeToPowerPoint(myPresentationObject) then you can simple add the slides there
or
you call the function AddNumber() within ExcelRangeToPowerPoint() of coarse the you need to loop there...
e.g.
Sub ExcelRangeToPowerPoint()
' some preparative code
'Create a New Presentation
Set myPresentation = PowerPointApp.Presentations.Add
'Add a slide to the Presentation
Set mySlide = myPresentation.Slides.Add(1, 11)
For Each rng in rngSel.Areas
'Filling the presentation one slide at a time
AddSlide(rng)
Next
'Clear The Clipboard
Application.CutCopyMode = False
myPresentation.SaveAs "C:\Project Control CCJV\ExperimentsPunch\" & rng3 &
".pptm"
PowerPointApp.Quit
'some more code
End Sub
I'm running vba to copy and paste ranges from Excel to PPT and I just changed the paste type to Bitmap and now I keep getting this error:
- selection.shaperange: invalid request . nothing appropriate is being
selected
on this line of code:
- PPApp.ActiveWindow.Selection.ShapeRange.Left = 0
I have Excel 2010 and need to use late binding as you can see from the code below.I have searched far and wide but can't seem to find a solution. Any help would be greatly appreciated.
'variables for referencing PPT
Dim PPApp As Object
Dim PPPres As Object
Dim PPSlide As Object
' And another variable to hold a shape
Dim PPTShp as Shape
Dim PresentationFileName As Variant
Dim SlideCount As Long
Dim lngSlideHeight As Long
Dim lngSlideWidth As Long
' reference existing instance of PowerPoint
Set PPApp = GetObject(, "Powerpoint.Application")
' reference active presentation
' 1 = PPViewSlide
Set PPPres = PPApp.ActivePresentation
PPApp.ActiveWindow.ViewType = 1
' Determine height and width of slide
lngSlideHeight = PPPres.PageSetup.SlideHeight
lngSlideWidth = PPPres.PageSetup.SlideWidth
'select the object / table
Range("H13:K24").Select
'copy the object / table
Selection.CopyPicture appearance:=xlScreen, Format:=xlBitmap
'Selection.Copy
'add a new slide and paste in the table
'12 = ppLayoutBlank
If (SheetMap(h) = "Charts") Then
SlideCount = PPPres.Slides.Count
Set PPSlide = PPPres.Slides.Add(SlideCount + 1, 12)
PPApp.ActiveWindow.View.GotoSlide PPSlide.SlideIndex
End If
'paste and select the table
'1 = PPpasteBitmap
' It's best NEVER to rely on selection if you can avoid it, so
' instead of this:
PPSlide.Shapes.PasteSpecial DataType:=1
' do this picks up the shape rather than the shaperange:
Set PPTShape = PPSlide.Shapes.PasteSpecial(1)(1)
'Conditional formatting of slide (based on step)
If (SheetMap(h) = "Charts") Then
' You don't need to go to the slide to work with its shapes.
' Use the shape reference created above instead:
' PPApp.ActiveWindow.View.GotoSlide PPSlide.SlideIndex
PPTShp.Left = 0
PPTShp.Top = 50
PPTShp.LockAspectRatio = msoFalse
PPTShp.ScaleHeight 0.9322, msoTrue, msoScaleFromTop
PPTShp.ScaleWidth 0.6954, msoTrue, msoScaleFromLeft
PPApp.ActiveWindow.Selection.ShapeRange.IncrementTop 16#
End If
'add titles
'1 = PPAlignLeft
If (SheetMap(h) = "Charts") Then
Set osh = PPSlide.Shapes.AddTextbox(msoTextOrientationHorizontal, Left:=8, Top:=15, Width:=lngSlideWidth, Height:=10)
osh.TextFrame.TextRange.Text = TitleMap(h, 1)
osh.TextFrame.TextRange.Font.Color.RGB = RGB(0, 0, 255)
osh.TextFrame.TextRange.Font.Size = 12
osh.TextFrame.TextRange.Font.Bold = True
osh.TextFrame.TextRange.Font.Italic = False
osh.TextFrame.TextRange.ParagraphFormat.Alignment = 1
osh.TextFrame.TextRange.ParagraphFormat.Bullet = False
End If
End If
I'm trying to create an excel macro that copies charts displayed on an excel sheet, and pastes them (paste special) into a PowerPoint. The problem I'm having is how do I paste each chart on a different slide? I do not know the syntax at all..
This is what I have so far (it works but it only pastes to the first sheet):
Sub graphics3()
Sheets("Chart1").Select
ActiveSheet.ChartObjects("Chart1").Activate
ActiveChart.ChartArea.Copy
Sheets("Graphs").Select
range("A1").Select
ActiveSheet.Paste
With ActiveChart.Parent
.Height = 425 ' resize
.Width = 645 ' resize
.Top = 1 ' reposition
.Left = 1 ' reposition
End With
Dim PPT As Object
Set PPT = CreateObject("PowerPoint.Application")
PPT.Visible = True
PPT.Presentations.Open Filename:="locationwherepptxis"
Set PPApp = GetObject("Powerpoint.Application")
Set PPPres = PPApp.activepresentation
Set PPSlide = PPPres.slides _
(PPApp.ActiveWindow.Selection.SlideRange.SlideIndex)
' Copy chart as a picture
ActiveChart.CopyPicture Appearance:=xlScreen, Size:=xlScreen, _
Format:=xlPicture
' Paste chart
PPSlide.Shapes.Paste.Select
' Align pasted chart
PPApp.ActiveWindow.Selection.ShapeRange.Align msoAlignCenters, True
PPApp.ActiveWindow.Selection.ShapeRange.Align msoAlignMiddles, True
Given I dont have your file locations to work with I have attached a routine below that
Created a new instance of PowerPoint (late binding, hence the need to define constants for ppViewSlide etc)
Loops through each chart in a sheet called Chart1 (as per your example)
Adds a new slide
Pastes each chart, then repeats
Did you need to format each chart picture before exporting for size, or can you change your default chart size?
Const ppLayoutBlank = 2
Const ppViewSlide = 1
Sub ExportChartstoPowerPoint()
Dim PPApp As Object
Dim chr
Set PPApp = CreateObject("PowerPoint.Application")
PPApp.Presentations.Add
PPApp.ActiveWindow.ViewType = ppViewSlide
For Each chr In Sheets("Chart1").ChartObjects
PPApp.ActivePresentation.Slides.Add PPApp.ActivePresentation.Slides.Count + 1, ppLayoutBlank
PPApp.ActiveWindow.View.GotoSlide PPApp.ActivePresentation.Slides.Count
chr.Select
ActiveChart.CopyPicture Appearance:=xlScreen, Size:=xlScreen, Format:=xlPicture
PPApp.ActiveWindow.View.Paste
PPApp.ActiveWindow.Selection.ShapeRange.Align msoAlignCenters, True
PPApp.ActiveWindow.Selection.ShapeRange.Align msoAlignMiddles, True
Next chr
PPApp.Visible = True
End Sub
Code with function for plotting 6 charts from Excel to PPT
Option Base 1
Public ppApp As PowerPoint.Application
Sub CopyChart()
Dim wb As Workbook, ws As Worksheet
Dim oPPTPres As PowerPoint.Presentation
Dim myPPT As String
myPPT = "C:\LearnPPT\MyPresentation2.pptx"
Set ppApp = CreateObject("PowerPoint.Application")
'Set oPPTPres = ppApp.Presentations("MyPresentation2.pptx")
Set oPPTPres = ppApp.Presentations.Open(Filename:=myPPT)
ppApp.Visible = True
Set wb = ThisWorkbook
Set ws = wb.Sheets(1)
i = 1
For Each shp In ws.Shapes
strShapename = "C" & i
ws.Shapes(shp.Name).Name = strShapename
'shpArray.Add (shp)
i = i + 1
Next shp
Call Plot6Chart(oPPTPres, 2, ws.Shapes(1), ws.Shapes(2), ws.Shapes(3), ws.Shapes(4), ws.Shapes(5), ws.Shapes(6))
End Sub
Function Plot6Chart(pPres As Presentation, SlideNo As Long, ParamArray cCharts())
Dim oSh As Shape
Dim pSlide As Slide
Dim lLeft As Long, lTop As Long
Application.CutCopyMode = False
Set pSlide = pPres.Slides(SlideNo)
For i = 0 To UBound(cCharts)
cCharts(i).Copy
ppApp.ActiveWindow.View.GotoSlide SlideNo
pSlide.Shapes.Paste
Application.CutCopyMode = False
If i = 0 Then ' 1st Chart
lTop = 0
lLeft = 0
ElseIf i = 1 Then ' 2ndChart
lLeft = lLeft + 240
ElseIf i = 2 Then ' 3rd Chart
lLeft = lLeft + 240
ElseIf i = 3 Then ' 4th Chart
lTop = lTop + 270
lLeft = 0
ElseIf i = 4 Then ' 5th Chart
lLeft = lLeft + 240
ElseIf i = 5 Then ' 6th Chart
lLeft = lLeft + 240
End If
pSlide.Shapes(cCharts(i).Name).Left = lLeft
pSlide.Shapes(cCharts(i).Name).Top = lTop
Next i
Set oSh = Nothing
Set pSlide = Nothing
Set oPPTPres = Nothing
Set ppApp = Nothing
Set pPres = Nothing
End Function