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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从Powerpoint添加对Excel对象库的引用相关的知识,希望对你有一定的参考价值。

我正在尝试以编程方式从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 FilesMicrosoft OfficeRootOffice 16EXCEL.EXE"

CleanUp:

Set vbProj = Nothing
Set VBAEditor = Nothing

End Sub

Adapted from here - Siddharth Rout

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

答案

这是代码

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 FilesMicrosoft OfficeRootOffice16EXCEL.EXE"

CleanUp:
Set vbProj = Nothing
Set VBAEditor = Nothing
End Sub

以上是关于从Powerpoint添加对Excel对象库的引用的主要内容,如果未能解决你的问题,请参考以下文章

求救!!..Winform 对 Excel的操作问题 ..!!!!!急急急!!!!

PowerPoint 中的交互式 Excel 工作表

Excel VBA后期绑定Microsoft DAO 3.6对象库

从 Excel 复制到 Powerpoint 错误

如何将图表从 Excel 复制到 PowerPoint?

使用 Python 将图表从 Excel 转换为 PowerPoint