使用 VBA excel 在 ppt 中创建文本框

create text box in ppt using VBA excel

我想通过 VBA excel 在 Powerpoint 中创建一个文本框,这是我的代码:

   Set Sh = PptDoc.Shapes.AddLabel(Orientation:=msoTextOrientationHorizontal, _
       Left:=100, Top:=100, Width:=150, Height:=60)
    Sh.TextFrame.TextRange.Text = Worksheets("Source").Range("D14")
    Sh.TextFrame.TextRange.Font.Color = RGB(255, 100, 255)

但是当我 运行 时,它说 Active X 有问题?

如果 Pptdoc 已经像下面的示例那样定义为 PowerPoint 幻灯片,则您的代码可以正常工作

Dim pp As PowerPoint.Application, pptdoc As Slide, pptLayout As CustomLayout
Set pp = CreateObject("PowerPoint.Application")
pp.Visible = True

'If you are creating a new Presentation and New slide the
pp.Presentations.Add
Set pptLayout = pp.ActivePresentation.Designs(1).SlideMaster.CustomLayouts(1)
Set pptdoc = pp.ActivePresentation.Slides.AddSlide(1, pptLayout)

'If you are using an existing presentation then delete above 3 lines use the 2 lines below
'pp.Presentations.Open ("C:\users\User\desktop\test.pptm")
'Set pptdoc = pp.ActivePresentation.Slides(1)

Set Sh = pptdoc.Shapes.AddLabel(Orientation:=msoTextOrientationHorizontal, _
       Left:=100, Top:=100, Width:=150, Height:=60)
    Sh.TextFrame.TextRange.Text = Worksheets("Source").Range("D14").Value
    Sh.TextFrame.TextRange.Font.Color = RGB(255, 100, 255)

在使用 PowerPoint 时,也总是尝试在工具参考中添加 Microsoft PowerPoint 对象库。当您只想写入值时,使用 .Value 和 excel 范围总是安全的。