VBA code for PowerPoint works when I step through it, but breaks when run

Green_Square_Window

New Member
Joined
Feb 6, 2024
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
The macro below opens an existing powerpoint presentation, finds the appropriate slide, duplicates it and moves it to the end of the deck, then pastes across a chart from Excel to the slide. It does this for various slides and charts using for loops. When I step through the code it works fine, but it breaks when I run it on this line: Set sld = PowerPointPresentation.Slides(PowerPointPresentation.Slides.Count)
The error message is: "The remote server machine doe not exist or is unavailable." The run-time error is 462. Any help with this would be much appreciated - I am new to manipulating PowerPoint with VBA.

Thanks very much

VBA Code:
Sub Duplicate_Slide()

    Dim PowerPointApp As Object
    Dim PowerPointPresentation As Object
    Dim PresentationPath As String
   
    ' Set the path to the PowerPoint presentation
    PresentationPath = "C:\Users\XXXX\Desktop\XXXX.pptx"

    ' Create a new instance of PowerPoint application
    Set PowerPointApp = CreateObject("PowerPoint.Application")

    ' Open the PowerPoint presentation
    Set PowerPointPresentation = PowerPointApp.Presentations.Open(PresentationPath)
 
 
    Dim i As Integer
    Dim j As Integer
       
    For i = 1 To ThisWorkbook.Sheets("Create_Slides").Range("Strategies_2D").rows.Count
   
        Dim max As Integer
        max = ThisWorkbook.Sheets("Create_Slides").Range("Count").rows(i).Value
       
        Dim chartRef As String
        chartRef = ThisWorkbook.Sheets("Create_Slides").Range("Charts").rows(i).Value
       
        Dim slideIndex As Integer
        slideIndex = ThisWorkbook.Sheets("Create_Slides").Range("Slide").rows(i).Value
       
        For j = 1 To max
       
            Dim slideToDuplicate As PowerPoint.Slide
            Set slideToDuplicate = PowerPointPresentation.Slides(slideIndex)
            slideToDuplicate.Copy
            PowerPointPresentation.Slides.Paste PowerPointPresentation.Slides.Count + 1

            Dim sld As PowerPoint.Slide
            Dim ExcelChart As chartObject
           
            Dim sheetRef As String
            Dim strat As Integer
           
            strat = ThisWorkbook.Sheets("Create_Slides").Range("Strategies_2D").Cells(i, j).Value
           
            sheetRef = "Strategy_" & strat
           
            Set ExcelChart = ThisWorkbook.Sheets(sheetRef).ChartObjects(chartRef)
            ExcelChart.chart.ChartArea.Copy
   
            Set sld = PowerPointPresentation.Slides(PowerPointPresentation.Slides.Count)
            sld.Shapes.Paste
           
            Set sld = Nothing
            Set ExcelChart = Nothing
            Set slideToDuplicate = Nothing
           
        Next j
       
    Next i
   
PowerPointApp.Visible = True

End Sub
 
Last edited by a moderator:

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
This might be a timing thing. You could try adding a delay before your Next J statement. For example:

VBA Code:
Application.Wait (Now + (TimeValue("0:00:02")))
 
Upvote 0
This might be a timing thing. You could try adding a delay before your Next J statement. For example:

VBA Code:
Application.Wait (Now + (TimeValue("0:00:02")))

Thanks for your input. This didn't work, unfortunately. The error is the same. Do you have any other ideas? Thanks
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,958
Members
449,096
Latest member
Anshu121

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