从Excel创建Powerpoint并插入文本框失败 [英] Creating Powerpoint from Excel and inserting textbox fails
问题描述
我正在尝试从Excel(VBA)创建PowerPoint(带有模板),并向每张幻灯片添加一个文本框.
I'm trying to create a powerpoint (with templates) from Excel (VBA) and add a textbox to every slide.
我要在其中添加文本框的代码行失败,索引超出范围/无活动演示.这是怎么了幻灯片的索引应该可以-如果手动设置索引,则没有任何变化.
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
推荐答案
您所处的问题源于您使用的后期绑定类型.在这种情况下,某些VBA常量无法识别,因此将其视为变量.
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.
首先-如果将VBE编辑器设置为要求变量声明
模式,那么您会更早地意识到该问题,因为我可以在代码中找到的所有三个vba常数已被标记为变量:
First- if you set you VBE editor to require variable declaration
mode 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:
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
以这种方式:
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.
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..
这篇关于从Excel创建Powerpoint并插入文本框失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!