繁体   English   中英

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

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

我必须将大量 Excel 图表发布到特定的 PowerPoint 文档中,我正在 Excel VBA 中构建一个宏来为我做这件事。

我能够正确打开要更新的 PowerPoint 演示文稿,但是我不知道如何将刚刚打开的演示文稿设置为名为MyPresentation的变量。

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

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

显然还有一些额外的代码,但我试图将我刚刚在第 3 行打开的 Presentation 设置为MyPresentation变量,以便我可以引用我刚刚打开的文档。

首先你必须为使用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)

我最终找到了 MVP Andy Pope 的解决方案。

一些相关的代码片段供未来用户使用。 (仅供参考,当我遇到问题时,我的 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"`

查找电子表格大师的指南,从 Excel VBA 打开 PPT

然后:

Set myPresentation = PowerPointApp.Presentations.Open(DestinationPPT)

我试图创建一个 excel 文件,该文件可以根据特定工作表生成预定义的 powerpoint 演示文稿,在 excel 文件中具有不同的值。

我的问题是我部门的每个人都应该能够下载 excel 文件并运行它。

在 vba 中定义演示文稿并不适合所有人,因为此操作所需的参考并未在每台计算机/excel 程序上激活。

Createobject 帮我解决了这个问题。 这是我正在谈论的代码:

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

什么都没有的工作是这样的东西:

Dim pptApp as Powerpoint.Application

或者

Dim pptApp as object
Set pptApp = New Powerpoint.Application

无需在 vba 中设置所需的引用

这个对我有用:

'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"

在打开它之前将其设置为 true。 否则它对我不起作用

暂无
暂无

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

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