Excel VBA Error 467 when trying to open a PowerPoi

2019-09-13 18:49发布

Objective of the Code: if PowerPoint is open and the searched Presentation is open, then update it. If the presentation is closed, then open it. In case PowerPoint is closed, then create a new instance.

Error: after multiple users have ran it on on mulitple computers the past 2 weeks, today one of the users get the following error message:

Run-Time Error 467: The Remote Server machine does not exist or is unavailable

Line code highlightned on debug mode:

Set ppPres = ppProgram.Presentations.Item(i)

Relevant section of the Module Code:

Public Sub UpdatePowerPoint(PowerPointFile)   

Dim ppProgram                           As PowerPoint.Application
Dim ppPres                              As PowerPoint.Presentation
Dim ppFullPath                          As String
Dim ppName                              As String
Dim activeSlide                         As PowerPoint.Slide

Dim cht                                 As Excel.ChartObject
Dim myShape                             As Object
Dim myChart                             As Object
Dim SlideNum, GPLRank                   As Integer
Dim ShapeNum                            As Integer
Dim shapeStageStat                      As Shape

On Error Resume Next
Set ppProgram = GetObject(, "PowerPoint.Application")
On Error GoTo 0

ppFullPath = PowerPointFile
PPT_Export_Success = True

' check if PowerPoint instance is open
If ppProgram Is Nothing Then
    Set ppProgram = New PowerPoint.Application
    i = 1
Else
    If ppProgram.Presentations.count > 0 Then
        ppName = Mid(ppFullPath, InStrRev(ppFullPath, "\") + 1, Len(ppFullPath))
        i = 1
        ppCount = ppProgram.Presentations.count
        Do Until i = ppCount + 1
            If ppProgram.Presentations.Item(i).Name = ppName Then
                Set ppPres = ppProgram.Presentations.Item(i)
                GoTo OnePager_Pres_Found
            Else
                i = i + 1
            End If
        Loop
    End If
End If

ppProgram.Presentations.Open Filename:=PowerPointFile

' *** Getting the ERROR at the line below ***
Set ppPres = ppProgram.Presentations.Item(i)

OnePager_Pres_Found:
ppPres.Windows(1).Activate  ' activate the One-Pager Presentation in case you have several open, and the One_pager is currently not the app "on-focus"

' --- Added Class script to allow PowerPoint ScreenUpdating set to FALSE ---
Dim myClass_PPT                         As Class_PPT

Set myClass_PPT = New Class_PPT
myClass_PPT.ScreenUpdating = False

' loop through all PowerPoint Slides, and copy all Chart objects from Excel
For ProjectCounter = 0 To NumberofProjectShts
    ' copying charts, shapes and other objects

Next ' ProjectCounter = 0 To NumberofProjectShts

AppActivate ("Microsoft PowerPoint")
Set activeSlide = Nothing
Set ppPres = Nothing
Set ppProgram = Nothing

End Sub

1条回答
叛逆
2楼-- · 2019-09-13 19:32

Your code - extract below - looks a bit weird to me:

' check if PowerPoint instance is open
If ppProgram Is Nothing Then
    Set ppProgram = New PowerPoint.Application
    i = 1
Else
    If ppProgram.Presentations.count > 0 Then
        ppName = Mid(ppFullPath, InStrRev(ppFullPath, "\") + 1, Len(ppFullPath))
        i = 1
        ppCount = ppProgram.Presentations.count
        Do Until i = ppCount + 1
            If ppProgram.Presentations.Item(i).Name = ppName Then
                Set ppPres = ppProgram.Presentations.Item(i)
                GoTo OnePager_Pres_Found
            Else
                i = i + 1
            End If
        Loop
    End If
End If

ppProgram.Presentations.Open Filename:=PowerPointFile

' *** Getting the ERROR at the line below ***
Set ppPres = ppProgram.Presentations.Item(i)

OnePager_Pres_Found:
ppPres.Windows(1).Activate  ' activate the One-Pager Presentation in case you have several open, and the One_pager is currently not the app "on-focus"

In the case Powerpoint is opened with some presentations but not the one you want (PowerPointFile), on the line which gives you the error, what are you trying to do? (i is equal to the Presentations.count)

I think it is wrong and shall be replaced by the ActivePresentation just opened on the line before.

Maybe you can restructure the code a little bit to have a clearer structure / case handling.

查看更多
登录 后发表回答