Runtime error 1004 when trying to create a PowerPoint presentation

Waimea

Active Member
Joined
Jun 30, 2018
Messages
465
Office Version
  1. 365
Platform
  1. Windows
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?
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
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.
 
Upvote 0

Forum statistics

Threads
1,214,780
Messages
6,121,525
Members
449,037
Latest member
tmmotairi

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top