vba 如何使用vba向powerpoint演示文稿添加文本框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40908904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to add a textbox to a powerpoint presentation using vba
提问by R. Patrick-White
I'm writing a macro which creates a powerpoint presentation and then copys data from a spreadsheet and adds a title and a textbox. I've been able to add the data and tile and format both however I'm struggling to add a textbox. When I run the code below it returns the error 'ActiveX component can't create object'. I feel like over looking something simple. Any help would be greatly appreciated! (Error occurs on the line after the first set of '-------')
我正在编写一个宏,它创建一个 powerpoint 演示文稿,然后从电子表格中复制数据并添加一个标题和一个文本框。我已经能够添加数据和磁贴和格式,但是我正在努力添加一个文本框。当我运行下面的代码时,它返回错误“ActiveX 组件无法创建对象”。我想看一些简单的东西。任何帮助将不胜感激!(错误发生在第一组'-------'之后的行)
Sub Create_Presentation()
Dim rng As Range
Dim PowerPointApp As PowerPoint.Application
Dim myPresentation As PowerPoint.Presentation
Dim mySlide As PowerPoint.Slide
Dim myShape As PowerPoint.Shape
Dim myTextbox As Shape
On Error Resume Next
Set PowerPointApp = CreateObject(class:="PowerPoint.Application")
Err.Clear
If PowerPointApp Is Nothing Then Set PowerPointApp = CreateObject(class:="PowerPoint.Application")
If Err.Number = 429 Then
MsgBox "PowerPoint could not be found, aborting."
Exit Sub
End If
On Error GoTo 0
Application.ScreenUpdating = True
Set myPresentation = PowerPointApp.Presentations.Add
Set mySlide = myPresentation.Slides.Add(1, 11) '11 = ppLayoutTitleOnly
Set rng = Range("PL_Tot")
rng.Copy
mySlide.Shapes.PasteSpecial DataType:=xlBitmap
Set myShape = mySlide.Shapes(mySlide.Shapes.Count)
myShape.Left = 0.3
myShape.Top = 67
myShape.Width = 430
myShape.Height = 406.4
mySlide.Shapes.Title.TextFrame.TextRange.Text = Range("TotalTitle").Value
Set sldTitle = mySlide.Shapes.Title
With sldTitle
With .TextFrame.TextRange
With .Font
.Bold = msoTrue
.Size = 22
.Color = RGB(0, 0, 200)
End With
End With
End With
sldTitle.Top = -30
'------------------------------------
Set myPresentation = ActivePresentation
Set mySlide = myPresentation.Slides(1)
Set myTextbox = mySlide.Shapes.AddTextbox(msoTextOrientationHorizontal, _
Left:=0, Top:=10, Width:=200, Height:=50)
With myTextbox.TextFrame.TextRange
.Text = Range("PPTextbox").Value
With .Font
.Size = 12
.Name = "Arial"
End With
End With
'-----------------------------------
PowerPointApp.Visible = True
PowerPointApp.Activate
Application.CutCopyMode = False
回答by Steve Rindsberg
Excel and PowerPoint can both have Shape objects. Your:
Excel 和 PowerPoint 都可以有 Shape 对象。您的:
Dim myTextbox As Shape
prepares Excel to expect an Excel shape. Change it to
准备 Excel 以期待 Excel 形状。将其更改为
Dim myTextbox As PowerPoint.Shape
so Excel doesn't bark when you try to apply PowerPoint properties and methods to an Excel shape.
因此,当您尝试将 PowerPoint 属性和方法应用于 Excel 形状时,Excel 不会吠叫。