如何获取Excel VBA到运行一个带有参数的PPT宏?

How to get Excel VBA to Run a PPT Macro WITH Parameters?

我正在尝试通过 Excel VBA 运行 一个 PowerPoint 宏,我曾经能够轻松地 运行 powerpoint 文件上的一个宏,但我在 excel.

中传递参数时遇到问题
Sub Test()
    Dim arr(1 To 1), macname As String, objPP As Object, PPTFilePath As String, ObjPPFile As Object, 
    PPtFileName As String
    PPTFileName  ="Report.pptm"
    PPTFilePath ThisWorkbook.Path & PPTFileName
    Set objPP = CreateObject("PowerPoint.Application")
    objPP.Visible = True
    Set objPPFile = objPP.Presentations.Open(PPTFilePath)
    
    Application.EnableEvents = False
    
    arr(1) = ThisWorkbook.Path
    macname = "'" & PPTFileName & "'!Module3.UpdateSpecificLinks"
    objPP.Run macname, arr
    objPPFile.Save
    waiting (3)
    
    Application.EnableEvents = True
End Sub

我在 objPP.Run macname, arr 上收到错误,它是:Run-time error '-2147188160 (80048240)': Application.Run :Invalid request. Sub or Function not defined.

如何正确地将参数传递给 powerpoint 宏:Sub UpdateSpecificLinks(LNK as String)

如果您的 SubUpdateSpecificLinks 是例如在私有模块中,调用它会失败;必须是 public.

我认为这是问题所在:

macname = "'" & PPTFileName & "'!Module3.UpdateSpecificLinks"

试试这个:

macname = PPTFileName & "!Module3.UpdateSpecificLinks"

几个例子,从一个 PPTM 文件调用另一个(关闭的)PPTM 文件:

调用宏如下:

Sub TestWithString()

    Dim sFileName As String
    Dim oPres As Presentation
    
    sFileName = "C:\temp\runme.pptm"
    Set oPres = Presentations.Open(sFileName, , , False)

    Application.Run "C:\temp\RunMe.pptm!RunMe", "This is the passed parameter"
    
    oPres.Close

End Sub

Sub TestWithArray()

    Dim sFileName As String
    Dim oPres As Presentation
    Dim aStrings(1 To 3) As String
    
    sFileName = "C:\temp\runme.pptm"
    Set oPres = Presentations.Open(sFileName, , , False)
    aStrings(1) = "String 1"
    aStrings(2) = "String 2"
    aStrings(3) = "String 3"

    Application.Run "C:\temp\RunMe.pptm!HowAboutAnArray", aStrings
    
    oPres.Close

End Sub

下面是他们调用的宏:

Sub RunMe(sMsg As String)
    MsgBox "You said " & sMsg
End Sub

Sub HowAboutAnArray(vParm As Variant)
    Dim x As Long
    
    For x = 1 To ubound(vParm)
        MsgBox vParm(x)
    Next

End Sub