Hello Excel Gurus,
This is the first time I am trying to make a VBA macro which generates PDF presentations automatically, so I am a newbie to this. It works to run once, but I get an error message when the macro runs a second time in a loop.
I get the following two error messages:
- Method 'Open' of object 'Presentations' failed
- The remote server machine does not exist or is unavailable (run-time error '462')
I have Googled around a bit to figure out what the issue is, but not found a solution which I fully understand, i.e. I still do not know what is missing in my code.
I am using the below code:
PHP Code:
Sub generateReport()
Dim PPT As PowerPoint.Application
Set PPT = New PowerPoint.Application
PPT.Visible = True
Dim objReport As Presentation
Dim objSlide As Slide
Dim objShape As Object
excelPath = Application.ActiveWorkbook.Path
pptTemplateName = "Report.pptx"
'THIS IS WHERE THE ERROR APPEARS IN THE SECOND LOOP'
Set objReport = PPT.Presentations.Open(Filename:=excelPath & "\" & pptTemplateName)
'Defining presentation version'
versionNumber = Sheets("Analysis").Range("D4").Value
'Inputting text to presentation from Excel'
Dim i As Integer
firstInputRow = 38
inputLength = Sheets("Analysis").Range("D" & firstInputRow - 3).Value
For i = 1 To inputLength
slideNum = Sheets("Analysis").Range("B" & firstInputRow + i - 1).Value
boxName = Sheets("Analysis").Range("C" & firstInputRow + i - 1).Value
textInput = Sheets("Analysis").Range("D" & firstInputRow + i - 1).Value
Set objSlide = objReport.Slides(slideNum)
objSlide.Shapes(boxName).TextFrame.TextRange.Text = textInput
Next i
'Updating charts in presentation'
inputLength = Sheets("Analysis").Range("J" & firstInputRow - 3).Value
For i = 1 To inputLength
slideNum = Sheets("Analysis").Range("I" & firstInputRow + i - 1).Value
boxName = Sheets("Analysis").Range("J" & firstInputRow + i - 1).Value
objReport.Slides(slideNum).Shapes(boxName).LinkFormat.Update
Next i
'Saving presentation as PDF'
FileName2 = Replace(excelPath & "\" & versionNumber & " " & pptTemplateName, "pptx", "pdf")
objReport.SaveAs FileName2, ppSaveAsPDF
objReport.Close
Set objShape = Nothing
Set objSlide = Nothing
Set objReport = Nothing
PPT.Quit
Set PPT = Nothing
End Sub
Any help would be greatly appreciated.
Bookmarks