我正在尝试获取 Excel 单元格中的值并填充 PowerPoint 文本框。我不想将 PowerPoint 表格链接到 Excel 电子表格,因为电子表格会不断变化,并且值并不总是在相同的行或相同的顺序中。
所以我正在编写这个 VBA 代码来尝试填充文本框。我做过很多 VBA,但从未尝试过这种组合。以下是我迄今为止所拥有的(更多的代码将被放入额外的文本框,但需要先让一个工作)。我意识到这个问题与未正确处理的对象有关,但不知道如何纠正它。
我正在使用 Excel 和 PowerPoint 2007。粗体语句是我收到错误的地方 - 438 对象不支持此属性或方法。
谢谢!
Sub valppt()
Dim PPT As PowerPoint.Application
Dim newslide As PowerPoint.Slide
Dim slideCtr As Integer
Dim tb As PowerPoint.Shape
Set PPT = CreateObject("PowerPoint.Application")
PPT.Visible = True
PPT.Presentations.Open "C:\Documents\createqchart.pptx"
Range("F2").Activate
slideCtr = 1
Set newslide = ActivePresentation.Slides(slideCtr).Duplicate
Set tb = newslide.Shapes("TextBox1")
slideCtr = slideCtr + 1
' Do Until ActiveCell.Value = ""
Do Until slideCtr > 2
If slideCtr = 2 Then
tb.TextFrame2.TextRange.Characters.Text = ActiveCell.Value
End If
ActiveCell.Offset(0, 1).Activate
slideCtr = slideCtr + 1
If slideCtr = 38 Then
Set newslide = PPT.ActivePresentation.Slides(slideCtr).Duplicate
ActiveCell.Offset(1, -25).Activate
End If
Loop
End Sub
更新 5/17
虽然幻灯片的复制有效,但我仍然无法评估文本框。在将值分配给文本框的语句之前,我无法提出正确的 set 语句。现在我什至没有一个固定的声明,因为我无法得到正确的声明。任何帮助表示赞赏。下面是最新的代码。
Sub shptppt()
'
' shptppt Macro
'
Dim PPT As PowerPoint.Application
Dim pres As PowerPoint.Presentation
Dim newslide As PowerPoint.Slide
Dim slideCtr As Integer
Dim tb As PowerPoint.Shape
Set PPT = CreateObject("PowerPoint.Application")
PPT.Visible = True
Set pres = PPT.Presentations.Open("C:\Documents\createqchart.pptx")
Range("F2").Activate
slideCtr = 1
'Set newslide = ActivePresentation.Slides(slideCtr).Duplicate
' Set tb = newslide.Shapes("TextBox1")
pres.Slides(slideCtr).Copy
pres.Slides.Paste
Set newslide = pres.Slides(pres.Slides.Count)
newslide.MoveTo slideCtr + 1
slideCtr = slideCtr + 1
' Do Until ActiveCell.Value = ""
Do Until slideCtr > 2
If slideCtr = 2 Then
tb.Slides.TextFrame2.TextRange.Characters.Text = ActiveCell.Value
End If
ActiveCell.Offset(0, 1).Activate
slideCtr = slideCtr + 1
If slideCtr = 38 Then
Set newslide = PPT.ActivePresentation.Slides(slideCtr).Duplicate
ActiveCell.Offset(1, -25).Activate
End If
Loop
End Sub