简体   繁体   English

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

[英]create text box in ppt using VBA excel

I want to create a text box in Power Point via VBA excel, this is my code :我想通过 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)

But when I am running, it say there is a problem with Activ X ?但是当我运行时,它说 Activ X 有问题?

Your code is working properly if Pptdoc is already defined as PowerPoint slide like the example below如果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)

Also always try add Microsoft PowerPoint Object library in the tools-references while working with PowerPoint.在使用 PowerPoint 时,也总是尝试在工具参考中添加 Microsoft PowerPoint 对象库。 It is always safe to use .Value with excel ranges when you intent to write values only.当您只打算写入值时,将.Value与 excel 范围一起使用总是安全的。

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

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