excel - 如何在 MacOS 上打开 PowerPoint 演示文稿
问题描述
我有一个在 Excel 2016 (Windows) 下运行的 VBA 代码,可将图表导出到 PowerPoint 文件 (.pptx)。
我试图让它在 MacOS 上运行。
我首先为 MacOS 重写了 FileDialogOpen 函数来浏览并选择一个 .pptx 文件以打开进行编辑。
在 MacOS(以及 Office 2016)上打开选择的文件时,我收到以下错误:
似乎 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
解决方案
尝试这个:
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
推荐阅读
- ruby-on-rails - 日志规范中错误数量的参数(给定 1,预期 2)的 Rspec 错误
- c# - SSIS:从网站获取报告的脚本
- java - 如何使用坐标检查一个点是否位于三角形内?
- python - 当您使用 python 输入 DOB 时,创建一个以年为单位打印您的年龄的函数
- html - 如何使滚动条覆盖内容
- python - Pandas 删除所有 0,直到多索引的每个级别中的第一个非零值
- ocaml - 如何从 Windows 的命令行运行 Ocaml 函数
- c - 在任何情况下将 const 多次放在变量上会有用吗?
- python - 拉直图像中的最大线
- c# - 如何一次销毁多个游戏对象?