通过 Access 中的 VBA 代码退出 Powerpoint 需要什么?

问题描述 投票:0回答:1

我怀疑我在这里遗漏了一些非常基本的东西,但从 Access 中操作 Powerpoint 显然非常罕见,以至于我在盲目伪造。

情况如下:我需要能够选择一个 Powerpoint 文件,将第一张幻灯片导出为 JPG,然后将副本保存到存档文件夹中。所有这些都已实现。问题是让 Powerpoint 之后就消失了。

关闭和退出都不执行任何操作。尝试将应用程序的可见性设置为 false 以使其能够在后台完成该过程也无济于事。

这是当前的代码。

Sub ExportPresentation(strPath As String)
        
    Dim objPPT As Object
       
    On Error Resume Next
    
    Set objPPT = GetObject(, "PowerPoint.Application")

    If Err.Number <> 0 Then
        Err.Clear
        Set objPPT = CreateObject("PowerPoint.Application")
    End If
    
    With objPPT
        .Presentations.Open strPath
        .Application.Visible = False
        .ActivePresentation.Slides(1).Export ArchiveFolderLocation & "Test" & ".jpg", "JPG"
    
        .ActivePresentation.SaveCopyAs2 ArchiveFolderLocation & "Test"
        .Presentation.Close
        .Application.Quit
    End With

End Sub

我也尝试过不使用 .Application,猜测这是 objPPT 固有的,但仍然一无所获。理论上,我可以做一些事情来调用导入文件中的宏,但这在逻辑上不可行。

我错过了什么?

vba ms-access powerpoint
1个回答
0
投票

与 Access 相比,PowerPoint

Application
对象没有引用自身的
Application
成员。

objPPT
是一个 Powerpoint Application 对象,意味着
.Application.Visible = False
引用应用程序对象的应用程序成员。这不是 PowerPoint 的有效代码。

相应调整:

Sub ExportPresentation(strPath As String)
        
    Dim objPPT As Object
       
    On Error Resume Next
    
    Set objPPT = GetObject(, "PowerPoint.Application")

    If Err.Number <> 0 Then
        Err.Clear
        Set objPPT = CreateObject("PowerPoint.Application")
    End If
    On Error GoTo 0 'As Black cat mentioned, would've made this error easy to spot
    With objPPT
        .Presentations.Open strPath
        .Visible = False
        .ActivePresentation.Slides(1).Export ArchiveFolderLocation & "Test" & ".jpg", "JPG"
    
        .ActivePresentation.SaveCopyAs2 ArchiveFolderLocation & "Test"
        .Presentation.Close
        .Quit
    End With

End Sub

在 Access 中,您可以执行类似

Application.Application.Application.Quit
的操作。 PowerPoint 中并非如此

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.