我正在尝试打开由用户在Excel中确定的特定PowerPoint幻灯片。以下是将Powerpoint打开到特定幻灯片的代码(targ是类似于的字符串"Slide:12"
):
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
当演示文稿关闭并且必须将其打开时,它可以很好地工作。我也想在Powerpoint演示文稿也已打开的情况下将其设置为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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句