在 Excel VBA 中保存 PowerPoint

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15621318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 15:15:23  来源:igfitidea点击:

Saving a PowerPoint in Excel VBA

excelexcel-vbapowerpointpowerpoint-vbavba

提问by broconnor1

I have code that creates a new powerpoint consisting of some images from an excel file. 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...

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

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?

有什么建议?

回答by David Zemens

I think you are dimensioning the variable oPPTApp without actually creating an instance of Powerpoint.Application.

我认为您是在未实际创建 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

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 .Addmethod which accepts the Layoutargument, instead of .AddSlide(which requires a CustomLayout):

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

ppPres.Slides.Add sldCount + 1, ppLayoutBlank

ppPres.Slides.Add sldCount + 1, ppLayoutBlank