2016-12-01 221 views
0

我正在寫一個宏,它創建一個PowerPoint演示文稿,然後從電子表格複製數據並添加標題和文本框。我已經能夠添加數據和平鋪和格式,但我努力添加一個文本框。當我運行下面的代碼時,它返回錯誤'ActiveX組件不能創建對象'。我覺得看起來很簡單。任何幫助將不勝感激! (錯誤發生在第一組''後面的行上)如何使用vba添加文本框到PowerPoint演示文稿

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 

回答

1

Excel和PowerPoint都可以有Shape對象。您的:

Dim myTextbox As Shape 

準備Excel以期望Excel形狀。將其更改爲

Dim myTextbox As PowerPoint.Shape 

因此,當您嘗試將PowerPoint屬性和方法應用於Excel形狀時,Excel不會出現吠叫聲。

+0

謝謝!現在完美運作。新的事情會變得簡單。 –

相關問題