从Powerpoint添加对Excel对象库的引用

问题描述 投票:0回答:1

我正在尝试以编程方式从Powerpoint宏引用Excel 16.0对象库。我似乎无法找到有关如何执行此操作的任何信息。

我认为这是在Excel工作簿中添加库引用的代码:

Sub AddReference()

Dim VBAEditor As VBIDE.VBE
Dim vbProj As VBIDE.VBProject
Dim chkRef As VBIDE.Reference
Dim BoolExists As Boolean

Set VBAEditor = Application.VBE
Set vbProj = ActiveWorkbook.VBProject

'~~> Check if reference is already added
For Each chkRef In vbProj.References
    If chkRef.Name = "Microsoft Excel 16.0 Object Library" Then
        BoolExists = True
        GoTo CleanUp
    End If
Next

vbProj.References.AddFromFile "C:\Program Files\Microsoft Office\Root\Office 16\EXCEL.EXE"

CleanUp:

Set vbProj = Nothing
Set VBAEditor = Nothing

End Sub

Adapted from here - Siddharth Rout

但是,我得到了一个未在Powerpoint中定义的用户定义类型。我认为这是因为Sub开头的对象是不同的。任何人都知道如何在Powerpoint中做类似的事情?

vba excel-vba reference powerpoint powerpoint-vba
1个回答
1
投票

这是代码

Sub Add_References_Programmatically()
Dim VBAEditor       As Object
Dim vbProj          As Object
Dim chkRef          As Object

Set VBAEditor = Application.VBE
Set vbProj = ActivePresentation.VBProject

On Error Resume Next
    vbProj.References.AddFromGuid "{0002E157-0000-0000-C000-000000000046}", 5, 0
On Error GoTo 0

For Each chkRef In vbProj.References
    If chkRef.Name = "Excel" Then
        MsgBox "The Reference Is Already Added", 64
        GoTo CleanUp
    End If
Next chkRef

vbProj.References.AddFromFile "C:\Program Files\Microsoft Office\Root\Office16\EXCEL.EXE"

CleanUp:
Set vbProj = Nothing
Set VBAEditor = Nothing
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.