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

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
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,224,869
Messages
6,181,484
Members
453,046
Latest member
Excelvbaexpert

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