在 Excel VBA 中,如何打开 PowerPoint 幻灯片并替换文本值?
In Excel VBA, how do I open a PowerPoint slide and replace a text value?
在这个社区的帮助下以及我有限的编码知识 VBA,我编写了一个 Excel VBA 宏来执行以下操作;清理复杂的报告,然后生成折线图和一些关键数据点,我想作为最后一步将其放入 PowerPoint 幻灯片中。
但是我似乎无法打开我的幻灯片。我所做的是用我想要替换的元素创建一个模板。这是我拥有的 PowerPoint 幻灯片示例;我已经全选了,所以你可以看到我的文本框在哪里。
这是一张幻灯片,我想做的是将“: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
。你得到的错误相当于一个空指针异常
在这个社区的帮助下以及我有限的编码知识 VBA,我编写了一个 Excel VBA 宏来执行以下操作;清理复杂的报告,然后生成折线图和一些关键数据点,我想作为最后一步将其放入 PowerPoint 幻灯片中。
但是我似乎无法打开我的幻灯片。我所做的是用我想要替换的元素创建一个模板。这是我拥有的 PowerPoint 幻灯片示例;我已经全选了,所以你可以看到我的文本框在哪里。
这是一张幻灯片,我想做的是将“: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
。你得到的错误相当于一个空指针异常