0
votes

I'm trying to programmatically reference the Excel 16.0 Object Library from a Powerpoint macro. I can't seem to find any information on how to do this.

I think this is the code to add library references within an Excel Workbook:

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

However, I get a User-defined type not defined in Powerpoint. I assume it is because the Objects at the start of the Sub are different. Anyone know how to do something similar in Powerpoint?

1
In the VBE >> From Tools Menu >> References >> Check the option "Microsoft Visual Basic for Application Extensibility 5.3" .. You have to change ActiveWorkbook to ActivePresentation because you would use the code in Powerpoint you should check from Macro settings >> Trust access to the VBA project modelYasserKhalil

1 Answers

1
votes

Here's the code

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