簡體   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