使用VBA Excel在PPT中创建文本框

时间:2018-11-09 09:36:18

标签: excel vba excel-vba excel-2016

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

   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)

但是当我跑步时,它说Activ X有问题吗?

1 个答案:

答案 0 :(得分:2)

如果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范围结合使用始终是安全的。