Runtime error 1004 when trying to create a PowerPoint presentation

Waimea

Active Member
Joined
Jun 30, 2018
Messages
372
I am using the following code to create a PowerPoint presentation.

Code:
Sub PowerPoint()  Dim ar As Range, R As Range
 Dim PowerPointApp As Object, myPresentation As Object
  Dim mySlide As Object, myShape As Object
  'Dim PowerPointApp As PowerPoint.Application, myPresentation As Presentation
  'Dim mySlide As Slide, myShape As PowerPoint.Shape

'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
  'Create a New Presentation
  Set myPresentation = PowerPointApp.Presentations.Add
    
  ' Add slides in reverse order, last entry is first slide
  Set R = Sheets("Data").Range("B747:S796 , B694:S743 , B641:S690 , B588:S637 , B535:S584, B482:S531 ,B429:S478, B376:S425, B323:S372, B270:S319 , B217:S266 , B164:S213, B111:S160 , B58:S107 , B5:S54")
  For Each ar In R.Areas
    'Add a slide to the Presentation
    'Set mySlide = myPresentation.Slides.Add(1, 11) '11 = ppLayoutTitleOnly
    Set mySlide = myPresentation.Slides.Add(1, ppLayoutBlank) '7 = ppLayoutBlank
    'Copy Excel Range
    ar.Copy
    
    On Error Resume Next
    '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 ar
 
  'Make PowerPoint Visible and Active
  PowerPointApp.Visible = True
  PowerPointApp.Activate

  'Clear The Clipboard
  Application.CutCopyMode = False
  Application.ScreenUpdating = True
End Sub
When I run this code I get a run-time error '1004'. Application-defined or object defined error.

The error occurs in this line?

Code:
Set R = Sheets("Data").Range("B747:S796 , B694:S743 , B641:S690 , B588:S637 , B535:S584, B482:S531 ,B429:S478, B376:S425, B323:S372, B270:S319 , B217:S266 , B164:S213, B111:S160 , B58:S107 , B5:S54")
1. How can I fix this?
2. Can this code be improved upon?
 

Some videos you may like

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

Waimea

Active Member
Joined
Jun 30, 2018
Messages
372
Hi Kenneth Hobson,

thank you for your reply. I restarted my computer and now it works again!

I think you have helped me with this code earlier by adding bindings.
 

Watch MrExcel Video

Forum statistics

Threads
1,095,678
Messages
5,445,923
Members
405,370
Latest member
Miguel_Rojas

This Week's Hot Topics

Top