Macro to open Powerpoint and paste ranges from Excel - Troubleshoot error in code

onthegreen03

Board Regular
Joined
Jun 30, 2016
Messages
61
Hi -

I have some code that I copied from other users on line which opens PP (Procedure1) and then copies and pastes ranges from Excel (Procedure2) into that PP. When I run the code below it opens the Powerpoint no problem but it breaks down in Procedure2. Can someone help me figure out what needs to be fixed in Procedure 2 so that it copies/pastes the selected range into the PP opened in Procedure1? The code breaks at the "Add a slide to the presentation" step. Hopefully this makes sense. Many thanks for your help!

---------------------------------------------------------------------

Sub RunAllMacros()
Procedure1
Procedure2
End Sub

Sub Procedure1()

Dim objPPT As Object

Set objPPT = CreateObject("PowerPoint.Application")
objPPT.Visible = True

objPPT.Presentations.Open "C:\users\migreen\AppData\Roaming\Microsoft\Templates\Blank.potx"

End Sub

Sub Procedure2()

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("A1:C12")

'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:=2 '2 = ppPasteEnhancedMetafile
Set myShape = mySlide.Shapes(mySlide.Shapes.Count)

'Set position:
myShape.Left = 66
myShape.Top = 152

'Make PowerPoint Visible and Active
PowerPointApp.Visible = True
PowerPointApp.Activate

'Clear The Clipboard
Application.CutCopyMode = False

End Sub
 

Some videos you may like

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

Colorations

Board Regular
Joined
Jun 13, 2016
Messages
61
Your myPresentation variable isn't set to anything. I'm not familiar with powerpoint vba but maybe replace
Code:
Set mySlide = myPresentation.Slides.Add(1, 11) '11 = ppLayoutTitleOnly
with
Code:
Set mySlide = Application.ActivePresentation.Slides.Add(1, 11) '11 = ppLayoutTitleOnly
 

onthegreen03

Board Regular
Joined
Jun 30, 2016
Messages
61
Thanks for the reply Colorations. I tried your suggestion and it didn't work. Still stopping at that line of code. If you have any other recommendations I would love to hear them.
 

Colorations

Board Regular
Joined
Jun 13, 2016
Messages
61
It seems none of your variables have been instantiated so that's probably why this error and errors further down occur
 
Last edited:

onthegreen03

Board Regular
Joined
Jun 30, 2016
Messages
61
Yeah. If there are any VBA freaks of nature out there willing to re-write the Procedure2 code so that it works with Procedure1 I would be very grateful. Seems like I am close but I don't have the VBA chops to link the two procedures together.
 

Worf

Well-known Member
Joined
Oct 30, 2011
Messages
4,004
Like this:

Code:
' Excel module
Dim objppt As PowerPoint.Application


Sub RunAllMacros()
Procedure1
Procedure2
MsgBox "Sub successfully executed!", 64
End Sub


Sub Procedure1()
Set objppt = CreateObject("PowerPoint.Application")
objppt.Visible = True
objppt.Presentations.Open "C:\Users\Eddie\Documents\PowerPoint\dt.potx"
End Sub


Sub Procedure2()
Dim rng As Range, mypres As PowerPoint.Presentation, mySlide As Object, myShape As Object
'Copy Range from Excel
Set rng = ThisWorkbook.ActiveSheet.[A1:C12]
Set mypres = objppt.ActivePresentation
Set mySlide = mypres.Slides.Add(1, 11) '11 = ppLayoutTitleOnly
'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 = 66
myShape.Top = 152
'Make PowerPoint Visible and Active
objppt.Visible = 1
objppt.Activate
'Clear The Clipboard
Application.CutCopyMode = False
End Sub
 

onthegreen03

Board Regular
Joined
Jun 30, 2016
Messages
61
Thanks. I will try this and let you know if it works. Appreciate the help!
 

onthegreen03

Board Regular
Joined
Jun 30, 2016
Messages
61
THANK YOU Worf! You fixed it and it works all the way through. I realy appreciate the assistance on that one!
 

onthegreen03

Board Regular
Joined
Jun 30, 2016
Messages
61
Hey Worf - I was wondering if you could help me one more time with this code? Let me know if you have some time to hear my plea. Thanks!
 

Watch MrExcel Video

Forum statistics

Threads
1,102,776
Messages
5,488,778
Members
407,658
Latest member
Arias610

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...
Top