vba 从 Excel 创建 Powerpoint 并插入文本框失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18504859/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 23:03:21  来源:igfitidea点击:

Creating Powerpoint from Excel and inserting textbox fails

excelvbapowerpoint

提问by red

I'm trying to create a powerpoint (with templates) from Excel (VBA) and add a textbox to every slide.

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

The code line, where I want to add the textbox fails with Index out of bounds/No active presentation. What is here wrong? The index of the slide should be ok - there is no change if I set the index manually.

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

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

回答by Kazimierz Jawor

The problem in your situation stems from type of binding you use- late binding. In such situations some of VBA constants are not recognised and they are treated as variables.

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

First- if you set you VBE editor to require variable declarationmode then you would recognise that problem earlier because all three vba constants which I can find in your code would have been marked as variables:

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

   ppLayoutTitle
   ppLayoutBlank
   msoTextOrientationHorizontal

Second- to avoid the problem you need to convert all above constants into numbers which are:

其次- 为了避免这个问题,您需要将上述所有常量转换为数字:

   ppLayoutTitle    =1
   ppLayoutBlank    =12
   msoTextOrientationHorizontal    =1

in this way:

通过这种方式:

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"

Third- why it was working for first of two constants? Because both were recognized as variable with value equals to 0. And in both situation 0 was accepted parameter for slide types. But 0 was not accepted value for TextBox type..

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