【问题标题】:Add reference to Excel object library from Powerpoint从 Powerpoint 添加对 Excel 对象库的引用
【发布时间】:2018-03-01 10:39:40
【问题描述】:

我正在尝试以编程方式从 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 中做类似的事情吗?

【问题讨论】:

  • 在 VBE >> 从工具菜单 >> 参考 >> 检查选项“Microsoft Visual Basic for Application Extensibility 5.3” .. 您必须将 ActiveWorkbook 更改为 ActivePresentation,因为您将使用 Powerpoint 中的代码您应该检查宏设置>>信任对 VBA 项目模型的访问

标签: vba excel reference powerpoint


【解决方案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

【讨论】:

  • 谢谢,这可行,但是,有没有办法自动添加可扩展性库?这解决了一个问题,但现在又产生了另一个问题。
猜你喜欢
  • 1970-01-01
  • 2017-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多