简体   繁体   English

使用 VBA 从 Excel 中打开 PowerPoint 演示文稿,然后将该演示文稿设置为变量

[英]Open a PowerPoint presentation from Excel with VBA and then set that presentation to a variable

I have to post a lot of Excel charts to a specific PowerPoint document and I'm building out a macro in Excel VBA to do it for me.我必须将大量 Excel 图表发布到特定的 PowerPoint 文档中,我正在 Excel VBA 中构建一个宏来为我做这件事。

I'm able to correctly open the PowerPoint presentation that I want to update, however I don't know how to set the presentation I just opened to a variable called MyPresentation .我能够正确打开要更新的 PowerPoint 演示文稿,但是我不知道如何将刚刚打开的演示文稿设置为名为MyPresentation的变量。

Dim myPresentation As PowerPoint.Presentation
Dim PowerPointApp As PowerPoint.Application

PowerPointApp.Presentations.Open Filename:="obscured filepath and name"`

Obviously there's some additional code, but I'm trying to set the Presentation I just opened in line 3 set to the MyPresentation variable so I can reference the document I just opened.显然还有一些额外的代码,但我试图将我刚刚在第 3 行打开的 Presentation 设置为MyPresentation变量,以便我可以引用我刚刚打开的文档。

First you have to pave the way for using ppt files, what I did:首先你必须为使用ppt文件铺平道路,我所做的:

Dim DestinationPPT As String
Dim PowerPointApp As PowerPoint.Application
Dim myPresentation As PowerPoint.Presentation

Set PowerPointApp = CreateObject("PowerPoint.Application")

DestinationPPT = "path"
PowerPointApp.Presentations.Open (DestinationPPT)

I ended up finding a solution by the MVP Andy Pope.我最终找到了 MVP Andy Pope 的解决方案。

Some relevant code snippets for future users.一些相关的代码片段供未来用户使用。 (FYI My PPT was already visible when I ran into the problem) (仅供参考,当我遇到问题时,我的 PPT 已经可见)

Dim DestinationPPT As String
Dim PowerPointApp As PowerPoint.Application
Dim myPresentation As PowerPoint.Presentation

'Easier to define manually set links up front so it's easier to change/modify
DestinationPPT = "C:\yourfilepath\yourfilename.pptx"`

Lookup the Spreadsheet Guru's guide to opening PPT from Excel VBA查找电子表格大师的指南,从 Excel VBA 打开 PPT

Then:然后:

Set myPresentation = PowerPointApp.Presentations.Open(DestinationPPT)

I was trying to create an excel file that can generate a predefined powerpoint presentation on the basis of a specific worksheet, with different values within the excel-file.我试图创建一个 excel 文件,该文件可以根据特定工作表生成预定义的 powerpoint 演示文稿,在 excel 文件中具有不同的值。

My issue was that every person in my department should be able to download the excel-file and run it.我的问题是我部门的每个人都应该能够下载 excel 文件并运行它。

Defining Presentations within vba doesn't work for everyone since the reference that is required for this action isn't activated on every computer/excel-programm.在 vba 中定义演示文稿并不适合所有人,因为此操作所需的参考并未在每台计算机/excel 程序上激活。

Createobject did the trick for me to get around the problem. Createobject 帮我解决了这个问题。 Here the code i am talking about:这是我正在谈论的代码:

Dim pptApp as object
Dim strpath as string

strpath = ThisWorkbook.Path & "\Test_Dummy.pptx"
Set pptApp =  createobject("Powerpoint.application")

pptApp.Presentations.Open Filename:=strpath, readonly:=false, untitled:=true, withwindow:=true

What didn't work was stuff like:什么都没有的工作是这样的东西:

Dim pptApp as Powerpoint.Application

or或者

Dim pptApp as object
Set pptApp = New Powerpoint.Application

without setting the required references in vba无需在 vba 中设置所需的引用

This one worked for me:这个对我有用:

'devlare variables
Dim MyPPT As Object

'create PowerPoint
Set MyPPT = CreateObject("Powerpoint.application")
'make it visible
MyPPT.Visible = True
'path to powerpoint
MyPPT.presentations.Open "path\powerpoint.pptx"

Set visible to true, before open it.在打开它之前将其设置为 true。 otherwise it did not worked for me否则它对我不起作用

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

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