【问题标题】:Creating Powerpoint from Excel and inserting textbox fails从 Excel 创建 Powerpoint 并插入文本框失败
【发布时间】:2013-08-29 17:33:06
【问题描述】:

我正在尝试从 Excel (VBA) 创建一个 powerpoint(带有模板)并为每张幻灯片添加一个文本框。

我要在其中添加文本框的代码行失败,索引超出范围/没有活动演示。这里有什么问题?幻灯片的索引应该没问题 - 如果我手动设置索引没有任何变化。

Dim PowerPointApp As Object
Set PowerPointApp = CreateObject("PowerPoint.Application")
PowerPointApp.Visible = True


Set objP = PowerPointApp.Presentations.Add
objP.ApplyTemplate "" & Table1.Range("A1").Value & "draft.pptx"

PowerPointApp.ActivePresentation.Slides.Add 1, ppLayoutTitle

For i = 1 To 10

 objP.ApplyTemplate "" & Table2.Range("A1").Value & "template.pptx"
 PowerPointApp.ActivePresentation.Slides.Add i + 1, ppLayoutBlank
 PowerPointApp.ActivePresentation.Slides(i + 1).Select

 Table3.ChartObjects(i).CopyPicture

 PowerPointApp.ActivePresentation.Slides(i + 1).Shapes.Paste
 PowerPointApp.ActivePresentation.Slides(i + 1).Shapes(1).Top = 150
 PowerPointApp.ActivePresentation.Slides(i + 1).Shapes(1).Left = 50
 PowerPointApp.ActivePresentation.Slides(i + 1).Shapes(1).Width = 400
 PowerPointApp.ActivePresentation.Slides(i + 1).Shapes(1).Height = 300

     'Exception occurs here                            
 PowerPointApp.ActivePresentation.Slides(i + 1).Shapes.AddTextbox(msoTextOrientationHorizontal, Left:=100, Top:=100, Width:=200, Height:=50).TextFrame.TextRange.Text = "Text"
Next i

【问题讨论】:

  • 什么是异常编号和描述?
  • 更多——在我关闭一些本地设置(如模板)后,你的代码对我来说运行良好。但是您向我们展示的是不完整的代码(例如,With statement 的开头在哪里)。模板或代码的其他 sn-p 中可能有一些东西。但我不这么认为......
  • RunTime Error '-2147024809 (80070057)' Out of bounds exception 抱歉 - 我忘了删除 end with 语句。
  • 太棒了!在英语中是...?
  • 尝试将问题行中的这个:msoTextOrientationHorizontal 更改为 1(是的,单个数字 = 1)

标签: excel vba powerpoint


【解决方案1】:

您遇到的问题源于您使用的绑定类型- 后期绑定。在这种情况下,一些 VBA 常量无法识别,它们被视为变量。

首先- 如果您将 VBE 编辑器设置为 require variable declaration 模式,那么您会更早发现这个问题,因为我在您的代码中可以找到的所有三个 vba 常量都将被标记为变量:

   ppLayoutTitle
   ppLayoutBlank
   msoTextOrientationHorizontal

第二- 为避免出现问题,您需要将上述所有常量转换为以下数字:

   ppLayoutTitle    =1
   ppLayoutBlank    =12
   msoTextOrientationHorizontal    =1

这样:

PowerPointApp.ActivePresentation.Slides.Add 1, 1 'ppLayoutTitle
PowerPointApp.ActivePresentation.Slides.Add i + 1, 12 'ppLayoutBlank
PowerPointApp.ActivePresentation.Slides(i + 1).Shapes.AddTextbox(1, Left:=100, Top:=100, Width:=200, Height:=50).TextFrame.TextRange.Text = "Text"

第三-为什么它适用于两个常量中的第一个?因为两者都被识别为值等于 0 的变量。在这两种情况下,0 都是幻灯片类型的可接受参数。但是 TextBox 类型不接受 0..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-30
    相关资源
    最近更新 更多