![](/img/trans.png)
[英]Open a Powerpoint Presentation and update the Excel links in VBA
[英]Excel VBA Error 467 when trying to open a PowerPoint presentation
代碼的目標 :如果PowerPoint已打開並且搜索的演示文稿已打開,則對其進行更新。 如果演示文稿已關閉,則將其打開。 如果PowerPoint關閉,則創建一個新實例。
錯誤 :過去兩個星期內,多個用戶在多台計算機上運行了該文件,今天,其中一位用戶收到以下錯誤消息:
運行時錯誤467:遠程服務器計算機不存在或不可用
在調試模式下突出顯示的行代碼 :
Set ppPres = ppProgram.Presentations.Item(i)
模塊代碼的相關部分 :
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
您的代碼-以下摘錄-對我來說有點奇怪:
' 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"
如果打開了Powerpoint並顯示了一些演示文稿,但沒有打開所需的演示文稿(PowerPointFile),則在出現錯誤的那一行上,您要做什么? (我等於Presentations.count)
我認為這是錯誤的,應該由之前在網上打開的ActivePresentation代替。
也許您可以對代碼進行一些重組,以使結構/案例處理更加清晰。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.