设置PowerPoint演示文稿时已经打开(从Excel)

我试图打开一个由用户在Excel中决定的特定幻灯片幻灯片。 打开Powerpoint到特定幻灯片的代码如下(targ是一个像"Slide:12"的string):

 Function rcFollowSlide(targ As String) Dim PptPath As String Dim pptApp As PowerPoint.Application Dim pptPres As PowerPoint.Presentation targ = Mid(targ, InStr(targ, ":") + 1) targ = Left(targ, Len(targ) - 1) PptPath = wsSettings.Range("PPTPath").Value If IsPPTOpen(PptPath) Then MsgBox "Already opened" Exit Function 'Set ppres = Else Set pptApp = CreateObject("Powerpoint.Application") Set pptPres = pptApp.Presentations.Open(PptPath) End If If targ > 0 And targ <= pptPres.Slides.Count Then pptPres.Slides(CInt(targ)).Select Else MsgBox "Image " & targ & " N/A." End If End Function 

当演示文稿被closures并且必须打开它时,它工作得非常好。 我想在已经打开的时候将PPT演示文稿设置为pptPres,所以我可以在不打开演示文稿的新实例的情况下继续运行代码。 我怎样才能访问应用程序,并设置演示文稿?

作为参考,这里是用来检查PPT是否已经打开的函数。

 Function IsPPTOpen(FileName As String) Dim ff As Long, ErrNo As Long On Error Resume Next ff = FreeFile() Open FileName For Input Lock Read As #ff Close ff ErrNo = Err On Error GoTo 0 Select Case ErrNo Case 0: IsPPTOpen = False Case 70: IsPPTOpen = True Case Else: Error ErrNo End Select End Function 

我认为这应该做到这一点:

 If IsPPTOpen(PptPath) Then Set pptPres = pptApp.Presentations(Dir(PptPath)) 'Set ppres = Exit Function Else 

如果您需要激活演示文稿,请尝试:

 VBA.AppActivate (Dir(PptPath)) 

正如你所指出的,这在某些情况下也可能起作用(参见下面的Thierry评论)。

 PPTApp.Activate PPTPres.Activate 

我正在使用一个稍微不同的代码:

ppProgramPowerPoint.Application

ppPresPowerPoint.Presentation

ppFullPath是完整path(path和文件名)

ppName是请求演示文稿的“干净”名称

 ' more than 1 Presentstion open If ppProgram.Presentations.Count > 0 Then ppName = Mid(ppFullPath, InStrRev(ppFullPath, "\") + 1, Len(ppFullPath)) i = 1 Do Until i = ppProgram.Presentations.Count + 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 OnePager_Pres_Found: ppPres.Windows(1).Activate ' activate the Presentation in case you have several open