简体   繁体   English

在Excel VBA中保存PowerPoint

[英]Saving a PowerPoint in Excel VBA

I have code that creates a new powerpoint consisting of some images from an excel file. 我有创建从PowerPoint文件中的一些图像组成的新PowerPoint的代码。 I want to save the file using a string variable to define its name. 我想使用字符串变量保存文件以定义其名称。 I've done my due diligence searching for solutions with no success, which surprises me based on how basic of a task I'm trying to complete. 我已经完成了尽职调查,没有找到成功的解决方案,这让我惊讶于基于我要完成的任务的基础。 As of now, I have... 截至目前,我有...

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

But I keep getting a whole host of error messages. 但是我不断收到大量错误消息。 I have newPowerPoint defined as public in another module 我在另一个模块中将newPowerPoint定义为public

Public newPowerPoint As powerpoint.Application

Any suggestions? 有什么建议么?

I think you are dimensioning the variable oPPTApp without actually creating an instance of Powerpoint.Application. 我认为您在确定变量oPPTApp的尺寸时并未实际创建Powerpoint.Application实例。

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

EDIT 编辑

As you're adding slides using the AddSlide method, you need to refer to a CustomLayout . 当你要添加使用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

Alternatively, you can use the old .Add method which accepts the Layout argument, instead of .AddSlide (which requires a CustomLayout): 或者,您可以使用旧的.Add方法接受Layout参数,而不是.AddSlide (需要.AddSlide ):

ppPres.Slides.Add sldCount + 1, ppLayoutBlank

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM