首页 > 解决方案 > 如何在 MacOS 上打开 PowerPoint 演示文稿

问题描述

我有一个在 Excel 2016 (Windows) 下运行的 VBA 代码,可将图表导出到 PowerPoint 文件 (.pptx)。
我试图让它在 MacOS 上运行。

我首先为 MacOS 重写了 FileDialogOpen 函数来浏览并选择一个 .pptx 文件以打开进行编辑。
在 MacOS(以及 Office 2016)上打开选择的文件时,我收到以下错误:
在 MacOS 上从 VBA 打开 .pptx 文件时出现错误消息

似乎 PowerPoint 应用程序已启动,但未加载指定的 .pptx 文件。我检查了文件名变量是否分配给拾取的完整路径文件名(包括扩展名 .pptx)。

代码在打开时失败。

Sub Export_Charts_To_PPT_Presentation()
    
    Dim PptApp As PowerPoint.Application
    Dim PptDoc As PowerPoint.Presentation
    
    Set PptApp = New PowerPoint.Application
    
    PptPresPath = FileDialogOpen
    If PptPresPath = "" Then Exit Sub
    Set PptDoc = PptApp.Presentations.Open(PptPresPath, WithWindow:=msoTrue)

End Sub

Function FileDialogOpen() As String

    mypath = MacScript("return (path to desktop folder) as String")

    sMacScript = "set applescript's text item delimiters to "","" " & vbNewLine & _
      "try " & vbNewLine & _
      "set theFiles to (choose file " & _
      "with prompt ""Please select a file or files"" default location alias """ & _
      mypath & """ multiple selections allowed false) as string" & vbNewLine & _
      "set applescript's text item delimiters to """" " & vbNewLine & _
      "on error errStr number errorNumber" & vbNewLine & _
      "return errorNumber " & vbNewLine & _
      "end try " & vbNewLine & _
      "return theFiles"

    FileDialogOpen = MacScript(sMacScript)
End Function

标签: excelvbamacospowerpoint

解决方案


尝试这个:

Sub Export_Charts_To_PPT_Presentation()
    
    Dim PptApp As PowerPoint.Application
    Dim PptDoc As PowerPoint.Presentation
    Dim PptPresPath As String
    
    PptPresPath = FileDialogOpen
    
    If PptPresPath = "" Or Right(PptPresPath, 5) <> ".pptx" Then Exit Sub
    
    Set PptApp = New PowerPoint.Application
    Set PptDoc = PptApp.Presentations.Open(PptPresPath, WithWindow:=msoTrue)

End Sub

Function FileDialogOpen() As String

    Dim iPathStartPosition As Integer
    
    mypath = MacScript("return (path to desktop folder) as String")
    sMacScript = "set applescript's text item delimiters to "","" " & vbNewLine & _
        "try " & vbNewLine & _
        "set theFiles to (choose file " & _
        "with prompt ""Please select a file or files"" default location alias """ & _
        mypath & """ multiple selections allowed false) as string" & vbNewLine & _
        "set applescript's text item delimiters to """" " & vbNewLine & _
        "on error errStr number errorNumber" & vbNewLine & _
        "return errorNumber " & vbNewLine & _
        "end try " & vbNewLine & _
        "return theFiles"
  
    FileDialogOpen = Replace(MacScript(sMacScript), ":", "/")
    
    If Val(FileDialogOpen) = -128 Then
        FileDialogOpen = ""
    Else
        iPathStartPosition = InStr(1, FileDialogOpen, "/Users")
        If iPathStartPosition > 0 Then
            FileDialogOpen = Right(FileDialogOpen, Len(FileDialogOpen) - iPathStartPosition + 1)
        End If
    End If
End Function

推荐阅读