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.
Related
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 try to apply a powerpoint table style from vba excel but i had an issue, someone can help to try how apply a style in table object ?
My macro work fine expect the style part ....
this code go to error 400 -- execution error --> Method 'table' of the object 'Shape' has failed
Set otbl = PPT_Shape.Table
This is a sample of my code here below
Sub ExcelRangeToPowerPoint()
'PURPOSE: Copy/Paste An Excel Range Into a New PowerPoint Presentation
Dim rng As Range
Dim PowerPointApp As Object
Dim myPresentation As Object
Dim mySlide As Object
Dim myShape As Object
Dim sPath As String
Dim project As String
Dim otbl As TableObject
'Set Title
project = Feuil1.Cells("2", "C")
'Set the template
sPath = "C:\Users\E049XXXX\OneDrive - XXX\XXX\"
'Copy Range from Excel
Set rng = ThisWorkbook.ActiveSheet.Range("A1:N34")
'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
'Add a slide to the Presentation
PowerPointApp.ActivePresentation.ApplyTemplate "C:\Users\E049XXXX\OneDrive - XXXX\Documents\XXXXX.thmx"
'Copy Excel Range
rng.Copy
'Paste to PowerPoint and position
mySlide.Shapes.PasteSpecial DataType:=0 '0 = ppPasteDefault
Set myShape = mySlide.Shapes(mySlide.Shapes.Count)
'Set position:
myShape.Left = 66
myShape.Top = 152
'Add Title
Set myTitle = mySlide.Shapes.Title
myTitle.TextFrame.TextRange.Characters.Text = project
'Add style
Set PPT_Shape = myShape
Set otbl = PPT_Shape.Table
With otbl
.ApplyStyle "{C083E6E3-FA7D-4D7B-A595-EF9225AFEA82}", True
End With
'Make PowerPoint Visible and Active
PowerPointApp.Visible = True
PowerPointApp.Activate
'Clear The Clipboard
Application.CutCopyMode = False
End Sub
I have got the below code to copy the image from range of cells of excel to PPT, but the image pasted on the PPT does is of different dimensions.
Can someone tell me how can I fix size of the image when pasted in PPT.
I am pasting my code below for your reference.
Private Sub CommandButton1_Click()
Dim rng As Range
Dim PowerPointApp As Object
Dim myPresentation As Object
Dim mySlide As Object
Dim myShape As Object
'Unhiding the sheets
Worksheets("Sheet4").Visible = xlSheetVisible
'Copy Range from Excel
Set rng = ThisWorkbook.ActiveSheet.Range("A1:C12")
'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
'Mupltiple Slides
'List of PPT Slides to Paste to
MySlideArray = Array(1, 2, 3)
'List of Excel Ranges to Copy from
MyRangeArray = Array(Sheet2.Range("A1:AB71"), Sheet1.Range("A1:AL70"), Sheet5.Range("A1:AE56"))
'Loop through Array data
For x = LBound(MySlideArray) To UBound(MySlideArray)
'Add a slide to the Presentation
Set mySlide = myPresentation.Slides.Add(1, 12) '12 = ppLayoutBlank 'https://learn.microsoft.com/en-us/office/vba/api/powerpoint.ppslidelayout
'Copy Excel Range
'rng.Copy
MyRangeArray(x).Copy
Application.Wait (Now + TimeValue("0:00:03"))
'Paste to PowerPoint and position
mySlide.Shapes.PasteSpecial DataType:=2 '2 = ppPasteEnhancedMetafile
Set myShape = mySlide.Shapes(mySlide.Shapes.count)
'Set position:
myShape.Left = 0
myShape.Top = 0
Next x
'Message Box
MsgBox ("Please is ready !!")
'Make PowerPoint Visible and Active
PowerPointApp.Visible = True
PowerPointApp.Activate
'Clear The Clipboard
Application.CutCopyMode = False
'Hiding the sheets back
Worksheets("Sheet4").Visible = xlSheetHidden
End Sub
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 want to create a macro which opens an existing powerpoint template for me, copy data from a certain sheet of excel and then paste it in a specific slide in powerpoint.
I tried googling it online and created something but it doesnt work. The macro runs but I do not see any output. Please help. Below is the code I am working on:
Sub Excelrangetopowerpoint()
Dim rng As Range
Dim Powerpointapp As PowerPoint.Application
Dim myPresentation As PowerPoint.Application
Dim DestinationPPT As String
Dim myShape As Object
Dim myslide As Object
Set rng = Worksheets("regions").Range("B1:N18")
On Error Resume Next
Set Powerpointapp = CreateObject("Powerpoint.application")
DestinationPPT = ("C:\Users\OLX-Admin\Dropbox (Corporate Finance)\Naspers Monthly Reporting\Prep for call\From teams\FY2019\OLX Group Monthly Report_Sep'18_Macro.pptx")
Powerpointapp.Presentations.Open (DestinationPPT)
If Err.Number = 429 Then
MsgBox "Powerpoint could not be found.aborting."
Exit Sub
On Error GoTo 0
Application.ScreenUpdating = False
rng.Copy
Set myslide = PowerPoint.ActivePresentation.Slides(4)
myslide.Shapes.PasteSpecial DataType:=2 '2 = ppPasteEnhancedMetafile
Set myShape = myslide.Shapes(myslide.Shapes.Count)
myShape.Left = 152
myShape.Top = 152
Powerpointapp.Visible = True
Powerpointapp.Activate
activation.CutCopyMode = False
End If
End Sub
This should work.
There were some missing parts of your modified code.
Notice that if the presentation is already open, this code will open the existing file in "read-only" mode... (so it doesn't care if powerpoint file is already opened or not).
VBA Code
Sub Excelrangetopowerpoint()
Dim rng As Range
Dim Powerpointapp As Object
Dim myPresentation As Object
Dim DestinationPPT As String
Dim myShape As Object
Dim mySlide As Object
'Copy Range from Excel
Set rng = Worksheets("regions").Range("B1:N18")
'Create an Instance of PowerPoint
On Error Resume Next
'Set your destination path for the powerpoint presentation and open the file
Set Powerpointapp = CreateObject("Powerpoint.application")
DestinationPPT = ("C:\Test\My Powerpoint\Presentation1.pptx")
Powerpointapp.Presentations.Open (DestinationPPT)
'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
'Set my current Powerpoint window as activated
Set myPresentation = Powerpointapp.ActivePresentation
'Set which slide to paste into
Set mySlide = myPresentation.Slides(4)
'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.Left = 152
myShape.Top = 152
'Make PowerPoint Visible and Active
Powerpointapp.Visible = True
Powerpointapp.Activate
'Clear The Clipboard
Application.CutCopyMode = False
End Sub
Source: The code is a combination of the work by Chris Newman: "Copy & Paste An Excel Range Into PowerPoint With VBA" & "Copy & Paste Multiple Excel Ranges To Separate PowerPoint Slides With VBA" but with the modification that you add a path to an already existing PowerPoint file.
you were getting that error because of the below line.
Set myslide = PowerPoint.ActivePresentation.Slides(4)
The correct code is
Set myslide = PowerPointapp.ActivePresentation.Slides(4)