Intermittent run-time error "The specific value is out of range" when writing to existing ppt shape

Valx553 1 Reputation point
2022-08-25T20:39:56.797+00:00

Hi,
Hoping someone can help. I have code that I have written that when a command button is clicked it opens a specific ppt presentation (stored in the same folder) and writes values from specific cells to named shapes within the presentation. When I run the code, sometimes it works and sometimes it gives an out of range error. When running Debug, the error occurs on a different line every time. I can't figure out what is causing the error. Below is a portion of the code. The rest of the code reads the same but for different shapes and cells.

Private Sub CommandButton2_Click()

Dim ppt As PowerPoint.Application  
Dim pres As PowerPoint.Presentation  
Dim sl As PowerPoint.Slide  
Dim sh As PowerPoint.Shape  
  
Set ppt = New PowerPoint.Application  
Set pres = ppt.Presentations.Open(ThisWorkbook.Path & "\For Macro Delo 600 ADF Off HIGHWAY Presentation Template.pptx")  
  
 
Range("B2").Copy  
pres.Slides(3).Shapes("Competitor Cost").TextFrame.TextRange.Paste  
           
Range("B3").Copy  
pres.Slides(4).Shapes("Total Number of Units Under Evaluation").TextFrame.TextRange.Paste  
  
Range("B4").Copy  
pres.Slides(4).Shapes("Average Annual Operating Miles").TextFrame.TextRange.Paste  
  
Range("B5").Copy  
pres.Slides(4).Shapes("Equipment Downtime Cost Estimate - $/hour").TextFrame.TextRange.Paste  
  
Range("B6").Copy  
pres.Slides(4).Shapes("Operating Load Conditions").TextFrame.TextRange.Paste  
  
Range("B7").Copy  
pres.Slides(4).Shapes("Average Working Fuel Burn Estimate, miles per gallon").TextFrame.TextRange.Paste  
  

235013-runtime-error.png

Developer technologies Visual Basic for Applications
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.