簡體   English   中英

當我嘗試使用VBA將文本框從Excel添加到PowerPoint幻燈片時出現運行時錯誤5

[英]Runtime error 5 when I try to add a textbox to a powerpoint slide from excel using vba

我需要從Mac excel 2011向PowerPoint幻燈片添加一個文本框。我試圖使用PowerPoint.Slide.Shapes.AddTextbox函數來執行此操作。 我收到的完整錯誤消息是:

Run-time error '5': Invalid procedure call or argument

導出的初始設置工作正常,我已經驗證了所有變量都已初始化/具有數據,並且雖然可以在Windows上創建文本框,但在Mac上卻無法使用。 我已經包含了所有參考庫。

Dim ObjPPT As PowerPoint.Application
Dim ObjPresentation As PowerPoint.Presentation
Public ObjSlide As PowerPoint.Slide

Sub BtnExportTest_Click()
    ExportDataToPowerPoint
End Sub


Public Sub ExportDataToPowerPoint()
    ' Setup the new powerpoint.
    Set ObjPPT = CreateObject("PowerPoint.Application")
    Set ObjPresentation = ObjPPT.Presentations.Add

    GenerateSlide
End Sub

這是負責創建幻燈片的代碼。

' Generate the slides.
Private Sub GenerateSlide()
    ' Create new slide.
    Set ObjSlide = ObjPresentation.Slides.Add(ObjPPT.ActivePresentation.Slides.Count + 1, ppLayoutBlank)
    ObjPPT.ActiveWindow.View.GotoSlide ObjSlide.slideIndex

    ' Here we attempt to create a textbox for a title.
    setSlideTitle "Cogito Ergo Sum.", ObjSlide.slideIndex

    ....

End Sub

這是將文本框添加到幻燈片的代碼。

' Places the title textbox at the top of the slide.
Sub setSlideTitle(titleText As String, activeSlideIndex As Integer)
    Dim slideW As Integer
    slideW = ObjPresentation.PageSetup.slideWidth - 100

    createSlideTextBoxOfSize titleText, activeSlideIndex, 50, 10, slideW, 64
End Sub

' Create a textbox on a slide with various criteria.
Sub createSlideTextBoxOfSize(textToShow As String, activeSlideIndex As Integer, leftDim As Integer, topDim As Integer, widthDim As Integer, heightDim As Integer)
    'Dim myTextBox As Shape
    Dim myTextBox As PowerPoint.Shape

    ' Create the textbox
    ' Failed attempts
    ' Set myTextBox = ObjSlide.Shapes.AddTextbox(msoTextOrientationHorizontal, 100, 100, 100, 100)
    ' ObjPPT.ActivePresentation.Slides(activeSlideIndex).Shapes.AddTextbox(1, Left:=leftDim, Top:=topDim, Width:=widthDim, Height:=heightDim).Visible = msoCTrue

    With ObjPPT.ActivePresentation.Slides(activeSlideIndex)
        ' Create the textbox
        ' More failed attempts.
        'Set myTextBox = .Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, Left:=leftDim, Top:=topDim, Width:=widthDim, Height:=heightDim)
        'Set myTextBox = .Shapes.AddTextbox(1, leftDim, topDim, widthDim, heightDim)
        'Set myTextBox = .Shapes.AddTextbox(1, Left:=leftDim, Top:=topDim, Width:=widthDim, Height:=heightDim)
        'Set myTextBox = .Shapes.AddTextbox(1, 100, 100, 400, 100)
        'Set myTextBox = .Shapes.AddTextbox(msoTextOrientationHorizontal, leftDim, topDim, widthDim, heightDim)
        'Set myTextBox = .Shapes.AddTextbox(msoTextOrientationHorizontal, Left:=leftDim, Top:=topDim, Width:=widthDim, Height:=heightDim)

        'This is the Error point; it works on windows, but not on mac.   <~~~~
        Set myTextBox = .Shapes.AddTextbox(msoTextOrientationHorizontal, leftDim, topDim, widthDim, heightDim)

        ' More unrelated formatting code.
        ...

    End With
End Sub

我查看了我能找到的每篇文檔和示例代碼,確保工作表和Powerpoint處於不受保護的狀態,並且它根本不適用於mac excel2011。這時的任何幫助都將是不勝感激。

為了解決這個問題,我最終只創建了帶有預制幻燈片布局的模板文件,然后在需要特定類型的幻燈片時引用它們。

    ' Create new slide from a copy of the first slide in the powerppoint template file.
    ObjTemplatePresentation.Slides(slideIndexYouWantToCopy).Copy
    ' This is called to make sure it finishes coping the slide to the clipboard before we try and paste it.
    DoEvents
    ObjPresentation.Slides.Paste

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM