簡體   English   中英

在Excel VBA中,如何打開PowerPoint幻燈片並替換文本值?

[英]In Excel VBA, how do I open a PowerPoint slide and replace a text value?

在這個社區的幫助下以及我對VBA編碼的有限了解,我編寫了一個Excel VBA宏,它可以執行以下操作: 整理一個復雜的報告,然后生成一個折線圖和一些關鍵數據點,作為最后一步,我要將它們放入PowerPoint幻燈片中。

但是我似乎無法擺脫打開我的幻燈片的麻煩。 我要做的是創建一個包含要替換元素的模板。 這是我擁有的PowerPoint幻燈片的示例; 我已全部選中,因此您可以看到我的文本框在哪里。

myTemplate.pptx

這是一張幻燈片,我要做的是用大量已知錯誤替換“:KE:”。

這是我到目前為止的代碼;


   Sub pptTest()

   Dim PowerPointApp As Object
   Dim myPresentation As Object
   Dim pptTemplate As String

   Dim myShape As PowerPoint.Shape
   Dim mySlide As PowerPoint.Slide


   pptTemplate = "c:\z_scripts\MyTemplate.pptx"

   Set PowerPointApp = CreateObject(Class:="PowerPoint.Application")

   Set myPresentation = PowerPointApp.Presentations.Open(pptTemplate)

   'Make PowerPoint Visible and Active
     PowerPointApp.Visible = True
     PowerPointApp.Activate

   Set mySlide = myPresentation.Slides(1)

   'Sample Data
   myKE = "6"

   myShape.TextFrame.TextRange.Text = Replace(myShape.TextFrame.TextRange.Text, "KE", myKE)


   End Sub

這段代碼打開了我的PowerPoint幻燈片,但是隨后抱怨沒有設置With Block或Object變量。

任何幫助,將不勝感激。

您沒有設置myShape變量,因此它為Nothing 您得到的錯誤等效於空指針異常

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM