【问题标题】:Saving a PowerPoint in Excel VBA在 Excel VBA 中保存 PowerPoint
【发布时间】:2013-03-15 07:38:50
【问题描述】:

我的代码可以创建一个由 excel 文件中的一些图像组成的新 powerpoint。我想使用字符串变量来保存文件以定义其名称。我已经尽职尽责地寻找解决方案,但没有成功,这让我感到惊讶,因为我试图完成一项任务的基础性。到目前为止,我已经...

newPowerPoint.ActivePresentations.SaveAs filenamestring, 1
newPowerPoint.ActivePresentations.Close

但我不断收到一大堆错误消息。我在另一个模块中将 newPowerPoint 定义为 public

Public newPowerPoint As powerpoint.Application

有什么建议吗?

【问题讨论】:

  • 您在哪里得到错误(SaveAs.Close)?你遇到了什么错误?
  • 由于需要对象错误,使用 oPPTApp 切换了 newPowerPoint。 OPPTApp 也被声明为 public。因此,我现在使用 oPPTApp 收到的错误消息是 SaveAs 行上的“对象变量或未设置块变量”。我在另一个模块上的公共声明是.... Public newPowerPoint As powerpoint.Application Public oPPTApp As Object
  • Debug.Print oPPTApp Is Nothing。即时窗口会发生什么?

标签: excel powerpoint vba


【解决方案1】:

我认为您在没有实际创建 Powerpoint.Application 实例的情况下确定变量 oPPTApp 的尺寸。

Public ppApp As PowerPoint.Application

Sub PPTFile()

Dim ppPres As Presentation
Dim fileNameString As String

fileNameString = "C:\testPPT.pptx" '<change to your file path/name

'Create an instance of PPT to work with
Set ppApp = CreateObject("Powerpoint.Application")
ppApp.Visible = True

'Create a new presentation (or you can access an existing file with ppApp.Presentations.Open
Set ppPres = ppApp.Presentations.Add

'Save:
ppPres.SaveAs fileNameString, 1

'Quit the instance of PPT that you initiated above.
ppApp.Quit

End Sub

编辑

当您使用 AddSlide 方法添加幻灯片时,您需要参考CustomLayout

Dim sldCount As Integer

sldCount = ppPres.Slides.count
ppPres.Slides.AddSlide sldCount + 1, ppPres.Slides(sldCount).CustomLayout
'Once you've added the slide, then set using Layout:
ppPres.Slides(sldCount + 1).Layout = ppLayoutBlank

或者,您可以使用旧的.Add 方法,它接受Layout 参数,而不是.AddSlide(需要自定义布局):

ppPres.Slides.Add sldCount + 1, ppLayoutBlank

【讨论】:

  • 在设置 ppPres = ppApp.Presentations.Add 时出现“编译错误:未找到方法或数据成员”
  • 您使用的是什么操作系统和 Excel 版本?在 Win 7 XP 上的 Excel 2010 中工作正常。如果您手动输入该行,脚本辅助应该向您显示ppApp 的可用方法,并且只要您启用了对 Powerpoint 对象模型的引用(没有它,此代码将无法初始化).Add 应该是ppApp.Presentations 的可用方法。 imgur.com/k5jj3wO你不是这样吗??
  • 嗨,大卫-抱歉耽搁了……被另一个项目牵制了。我正在使用启用了 ppt 对象模型引用的 Windows 7 Excel 2007。 .Add 是我屏幕上的一种可用方法。运行时,我得到这个确切的错误:i.imgur.com/G5NI6KM.png?1 知道为什么吗?
  • ActivePresentation 不是ppPres 的有效成员,它是Presentation。试试ppPres.Slides.AddSlide ppPres.Slides.count+1 ...
  • 另外,这不是同一个错误,也不在你之前描述的同一个地方。你之前说过ppApp.Presentations.Add 有错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-25
  • 2018-02-23
  • 2021-03-19
  • 2017-08-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多