Excel VBA更新powerpoint文本框

时间:2017-07-12 11:48:24

标签: excel-vba powerpoint-vba vba excel

在以下编码的帮助下,我可以打开powerpoint文件,但它没有更新文本框。

我收到错误为“对象变量或未设置块变量”。

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:\Users\GShaikh\Desktop\Process Coach certificate template.pptx")
slideCtr = 1
Set tb = newslide.Shapes("TextBox1")
tb.TextFrame2.TextRange.Characters.Text = "OK"

2 个答案:

答案 0 :(得分:1)

Set tb行发生错误,因为您从未初始化newslide,或者至少您没有在此处显示。

假设您的文本框位于幻灯片上,您可以执行以下操作(在Set tb之前添加):

Set newslide = pres.Slides(1)

还要确保所需的文本框实际上是" TextBox1"。默认情况下,名称通常在数字之前有一个空格,例如" TextBox 1"。

我使用此更改测试了您的代码以验证其是否有效。完整代码:

Sub test()
    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:\Users\GShaikh\Desktop\Process Coach certificate template.pptx")
    slideCtr = 1

    Set newslide = pres.Slides(1)

    Set tb = newslide.Shapes("TextBox1")
    tb.TextFrame2.TextRange.Characters.Text = "OK"
End Sub

答案 1 :(得分:0)

尝试:

    PPT.ActivePresentation.Slides(2).Shapes("TextBox1").TextFrame.TextRange.Characters.Text = "qwerty"

要获得正确的形状名称(在输入框中以便您可以复制它),请选择它并运行:

a = InputBox("The name of the selected shape is:", "Name of the Shape", PPT.ActiveWindow.Selection.ShapeRange.Name)

更改它,当您选择它时,请尝试:

PPT.ActiveWindow.Selection.ShapeRange.Name = "TextBox2"

希望这有帮助。