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.
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)
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
I have specific columns' names in Excel that I want to copy and paste into PowerPoint but I can't run the code because I get "Run Error 424." I have tried using ("B3:Q3") for the columns and that works. However, I don't want all those columns, I only want the columns that are listed below ("b3,f3,l3,n3,p3,q3").
Can anyone assist? Thank you so much!
Sub ExcelRangeToPowerPoint()
Dim rng As Range
Dim rng1 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("b3,f3,l3,n3,p3,q3").Select 'THIS IS THE ERROR
Set rng1 = ThisWorkbook.ActiveSheet.Range("G4:I4")
'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
'Copy Excel Range
rng.Copy
'Paste to PowerPoint and position
mySlide.Shapes.PasteSpecial DataType:=7 '7 = ppPasteText
Set myShape = mySlide.Shapes(mySlide.Shapes.Count)
'Set position:rng
myShape.Left = 70
myShape.Top = 150
myShape.Width = 800
myShape.Height = 100
'Copy Excel Range
rng1.Copy
'Paste to PowerPoint and position
mySlide.Shapes.PasteSpecial DataType:=7 '7 = ppPasteText
Set myShape = mySlide.Shapes(mySlide.Shapes.Count)
'Set position:rng
myShape.Left = 70
myShape.Top = 200
myShape.Width = 800
myShape.Height = 300
'Insert the tile on the ppt
mySlide.Shapes.Title.TextFrame.TextRange.Text = "Insert Title Here"
'Make PowerPoint Visible and Active
PowerPointApp.Visible = True
PowerPointApp.Activate
'Clear The Clipboard
Application.CutCopyMode = False
End Sub
Take off the .Select.
1) You can't Set Rng = [whatever].Select. You want to do Set Rng = [whatever] then Rng.Select on a new line, but more importantly,
2) It's best to Avoid using .Select/.Activate. Although you don't seem to use it elsewhere (good!), so I bet this is just a "typo".
Also, if you want the Columns then you would do:
Set rng = ThisWorkbook.ActiveSheet.Range("b3,f3,l3,n3,p3,q3").EntireColumn
Edit: This won't solve the issue of it pasting the in-between columns, but this (admittedly a little klunky) code will select just the data used (including headers), instead of the entire columns:
'Copy Range from Excel
Dim lastRow As Long
With ThisWorkbook.ActiveSheet
lastRow = .Cells(Rows.Count, 2).End(xlUp).Row
' I assume your headers actually are in row 3, and the data is in row 4 on ward:
Set rng = ThisWorkbook.ActiveSheet.Range("b3:B" & lastRow & ",f3:F" & lastRow & ",l3:l" & lastRow & ",n3:N" & lastRow & ",p3:P" & lastRow & ",q3:Q" & lastRow)
Set rng1 = ThisWorkbook.ActiveSheet.Range("G4:I4")
End With
'Create an Instance of PowerPoint
On Error Resume Next
' Etc. etc.
I am transferring data from excel to powerpoint slides with an automated script by using EXcel VBA. I'm trying to copy the usedrange of a excel worksheet and paste it to as a image in a powerpoint Template of 4th slide and from there on it should add new slides and copy the remaining worksheets to the next further slides.
The code which i'm currently using is getting the following error "öbject variable or with block variable not set"
Can anyone suggest me the code for the following.
Hope this is clearly explained. If not please ask for more clarification.
Thanks
Private Sub CommandButton2_Click()
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
PP.Presentations.Open FileName:=("\\C:\Users\Templates)"
'Specify the chart to copy and copy it
For Each WS In Worksheets
If (WS.Name) <> "EOS" Then
ThisWorkbook.Worksheets(WS.Name).Activate
ThisWorkbook.ActiveSheet.UsedRange.CopyPicture
'pSlide.Shapes.Paste
'Copy Range from Excel
Set Rng = ThisWorkbook.ActiveSheet.Range("A1:I8")
'Copy Excel Range
Rng.Copy
'Set PPslide = PPpres.Slides.Add(5, 33)
PP.ActiveWindow.View.GotoSlide (4)
Set PPslide = PPpres.Slides(4).Shapes.Paste
'Paste to PowerPoint and position
PPslide.Shapes.PasteSpecial DataType:=2 '2 = ppPasteEnhancedMetafile
Set myshape = PPslide.Shapes(PPslide.Shapes.Count)
'Set position:
myshape.Left = 66
myshape.Top = 152
End If
Next
'Make PowerPoint Visible and Active
PowerPointApp.Visible = True
PowerPointApp.Activate
'Clear The Clipboard
Application.CutCopyMode = Falseenter code here`
End Sub
Try changing:
'Set PPslide = PPpres.Slides.Add(5, 33)
PP.ActiveWindow.View.GotoSlide (4)
Set PPslide = PPpres.Slides(4).Shapes.Paste '<< CHANGING THIS LINE ONLY
'Paste to PowerPoint and position
PPslide.Shapes.PasteSpecial DataType:=2 '2 = ppPasteEnhancedMetafile
To:
'Set PPslide = PPpres.Slides.Add(5, 33)
PP.ActiveWindow.View.GotoSlide (4)
Set PPslide = PPpres.Slides(4)
'Paste to PowerPoint and position
PPslide.Shapes.PasteSpecial DataType:=2 '2 = ppPasteEnhancedMetafile
Also, as per my comment, you'll need to change the following last few lines of your code:
'Make PowerPoint Visible and Active
PowerPointApp.Visible = True
PowerPointApp.Activate
'Clear The Clipboard
Application.CutCopyMode = Falseenter code here`
End Sub
To:
'Make PowerPoint Visible and Active
PP.Visible = True
PP.Activate
'Clear The Clipboard
Application.CutCopyMode = False
'enter code here
End Sub
I have the code below which I found online to copy paste a tble from excel to powerpoint. After pasting the table into the slide, it fails on PPSlide.Shapes(1).Select with a Run-time error '-2147188160 (80048240)': Shape.Select : Invalide request. To select a shape, its view must be active.
I've been searching and trying different things but can't seem to figure it out.. I thought after the paste that the table would be active and the code would jsut continue but it doesn't unless I activate/select the table in the slide and then click Run. Any help is appreciated. Thanks.
Dim pp As Object
Dim PPPres As Object
Dim PPSlide As Object
Dim Rng As Range
DestinationPPT = "C:\Users\username\Desktop\Data_Display.pptx"
Set pp = CreateObject("PowerPoint.Application")
Set PPPres = pp.Presentations.Open(DestinationPPT)
pp.Visible = True
Set Rng = ActiveSheet.Range("CA1:CJ" & Count + 1)
Rng.Copy
SlideCount = PPPres.Slides.Count
Set PPSlide = PPPres.Slides.Add(SlideCount, 12)
pp.ActivePresentation.Application.CommandBars.ExecuteMso ("PasteSourceFormatting")
PPSlide.Shapes(1).Select
pp.ActiveWindow.Selection.ShapeRange.Align msoAlignTops, True
pp.ActiveWindow.Selection.ShapeRange.Top = 65
pp.ActiveWindow.Selection.ShapeRange.Left = 7.2
pp.ActiveWindow.Selection.ShapeRange.Width = 700
Set PPSlide = Nothing
Set PPPres = Nothing
Set pp = Nothing
Try the code below:
Set pp = CreateObject("PowerPoint.Application")
Set ppPres = pp.Presentations.Open(DestinationPPT)
pp.Visible = True
' first set the Slide and select it
SlideCount = ppPres.Slides.Count
Set ppSlide = ppPres.Slides.Add(SlideCount, 12)
ppSlide.Select
' have the Copy part right beofre youe Paste it to PowerPoint
Set Rng = ActiveSheet.Range("CA1:CJ" & Count + 1)
Rng.Copy
pp.ActivePresentation.Application.CommandBars.ExecuteMso ("PasteSourceFormatting")
Dim myShape As Object
' set up the Pasted shape to an object
Set myShape = ppSlide.Shapes(ppSlide.Shapes.Count)
With myShape
' set-up the shape properties
End With