繁体   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