我在 excel 中编写 VBA 代码,它尝试打开并保存一份 Powerpoint 文件的副本,然后在幻灯片中添加一个文本框。
在Mac环境下运行,下面的代码成功打开并保存了ppt文件,但是在With语句中尝试调用AddTextbox方法,导致Powerpoint应用程序崩溃。 (EXC_BAD_ACCESS 错误)
我打开 Powerpoint 文件没有问题,只有在我运行此代码时它才会崩溃。我的怀疑是 Shapes.AddTextbox 方法或其某些参数不受支持,但我在互联网上找不到相关信息/引用。
非常感谢,如果有人可以阐明这一点。此外,鉴于 VBA 在 Mac 中的支持不如在 Windows 中那么好,是否有其他替代 Powerpoint 自动化的方法(比如 Applescript)?
Option Explicit
Public Const COVER_SLIDE = 3
Dim param_ws As Worksheet
Sub Main()
Set param_ws = ThisWorkbook.Worksheets("Param")
'Open the template Powerpoint, save as a copy
Dim PowerPointApp As PowerPoint.Application
Dim myPresentation As PowerPoint.Presentation
Set PowerPointApp = New PowerPoint.Application
Set myPresentation = PowerPointApp.Presentations.Open(param_ws.Range("wk_dir").Value & "/" & param_ws.Range("ppt_temp_fileName").Value)
myPresentation.SaveAs (param_ws.Range("wk_dir").Value & "/" & param_ws.Range("myfolder").Value & "/" & param_ws.Range("myfile").Value)
PowerPointApp.Visible = True
' ################## All good until here ##################
With myPresentation.Slides(COVER_SLIDE).Shapes.AddTextbox(msoTextOrientationHorizontal, Left:=Application.InchesToPoints(5.71), Top:=Application.InchesToPoints(3.8), Width:=Application.InchesToPoints(3.47), Height:=Application.InchesToPoints(1.04))
.TextFrame.TextRange.Text = "Hello"
.TextFrame.TextRange.Font.Size = 28
.TextFrame.TextRange.Font.Name = "Arial Narrow"
.TextFrame.HorizontalAnchor = msoAnchorCenter
.TextFrame.TextRange.Paragraphs.ParagraphFormat.Alignment = ppAlignRight
End With
End Sub
最佳答案
这更像是一个评论而不是一个答案,但是评论格式会对其进行散列,所以我们在这里。
首先,我会改变
公共(public)常量 COVER_SLIDE = 3
至
公共(public)常量 COVER_SLIDE 为 Long = 3
通常最好告诉 vba 你想要什么,而不是让它猜测。在这种情况下,它不应该有任何区别,但是在 Mac VBA 的幕后运行有一些奇怪的错误。
我还将验证 Mac Excel(在您正在运行的版本中)实际上确实有一个 Application.InchesToPoints 方法。如果没有,在添加文本框时将英寸值乘以 72 很简单。
最后,ppAlignRight 是 PowerPoint 常量,而不是 Office 范围或 Excel 常量,因此不会定义它,事实上,在 IDE 中编译代码时应该会导致错误(在尝试运行之前总是一个好主意它)。要么将其定义为:
Public Const ppAlignRight as Long = 3
或在您的代码中使用 3 而不是 ppAlignRight。
关于vba - Mac VBA | Excel/Powerpoint 2016(版本 15.33)| Shapes.AddTextBox 方法上的 Powerpoint 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44747072/