2013-08-29 29 views
0

我試圖從Excel(VBA)創建一個powerpoint(包含模板)並向每張幻燈片添加一個文本框。從Excel創建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 
+0

什麼是例外編號和描述? –

+0

東西更多 - 我的關閉一些本地設置(如模板)後,您的代碼正常工作。但是你向我們展示的是不完整的代碼(例如'With語句'開頭的地方)。在模板或代碼的其他代碼段中可能存在某種外部特徵。但我不這麼認爲... –

+0

運行時錯誤'-2147024809(80070057)' 出境異常 對不起 - 我忘了刪除聲明結束。 – red

回答

3

您的情況中的問題源於您使用的綁定類型 - 後期綁定。在這種情況下,一些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都是滑動類型的可接受參數。但0未被TextBox類型的值接受。

+0

+1的詳細解釋。 –