如何使用VBA在PowerPoint演示文稿中添加文本框

4

我正在编写一个宏,创建一个 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

这是一个简单的示例,通过宏将便笺样式的文本框添加到PowerPoint中:https://stackoverflow.com/a/58205047/470749 - Ryan
1个回答

3

Excel和PowerPoint都可以拥有形状对象。您的:

Dim myTextbox As Shape

将Excel准备好以接受Excel形状。将其改为

Dim myTextbox As PowerPoint.Shape

因此,在您尝试将PowerPoint属性和方法应用于Excel形状时,Excel不会出现错误提示。

谢谢!现在完美运行。知道这只是一些简单的东西。 - R. Patrick-White

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接