我正在尝试创建一个 MS Word 宏来检查特定的 powerpoint 文件是否打开。如果是,那么我希望它转到下一个,但如果不是,则打开文件。
Public Sub CommandButton1_Click()
Dim pptApp As Object
Dim pptPres As String
'Dim nSlide As PowerPoint.Presentation
Dim folderPath, file As String
folderPath = ActiveDocument.Path & Application.PathSeparator
file = "Huntington_Template.pptx"
Set pptApp = CreateObject("PowerPoint.Application")
If pptApp.presentations(file).Enabled = True Then
GoTo cont
Else
pptApp.Visible = True
pptApp.presentations.Open (folderPath & file)
End If
续:
End Sub
最佳答案
Steve 代码的一个小变体,以防您不仅要测试演示文稿是否打开,还要直接使用它:
Function GetPowerpointFileIfOpen(pptApp As Object, sFullname As String) As Object
For Each p In pptApp.Presentations
If p.FullName = sFullname Then
Set GetPowerpointFileIfOpen = p
Exit Function
End If
Next p
End Function
然后您可以测试演示文稿是否打开 - 或者以其他方式打开它:
Set ppt = GetPowerpointFileIfOpen(pptApp, sFullName)
If ppt Is Nothing Then
Set ppt = pptApp.Presentations.Open(sFullName, False)
End If
关于vba - 如何检查 PowerPoint 文件是否打开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24637429/