繁体   English   中英

Excel VBA更新PowerPoint文本框

[英]Excel VBA to update powerpoint text box

借助下面的编码,我能够打开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"

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

假设您的文本框在幻灯片1上,您可以执行以下操作(在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

尝试:

    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"

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM