【问题标题】:Possible to modify Excel VBA References at runtime (after runtime loading of an Add-In)?可以在运行时修改 Excel VBA 引用(在运行时加载加载项之后)?
【发布时间】:2012-02-29 06:39:55
【问题描述】:

动态加载 Excel 2010 加载项时,还必须在将新添加的加载项加载到工作簿后更改 VBA 引用以包含新添加的加载项。

此代码适用于以编程方式加载加载项:

Function LoadAddin(strFilePath As String) As Boolean
   ' Checks whether add-in is in collection, and
   ' then loads it. To call this procedure, pass
   ' in add-in's path and file name.

   Dim addXL            As Excel.AddIn
   Dim strAddInName     As String

   On Error Resume Next
   ' Call ParsePath function to return file name only.
   'strAddInName = ParsePath(strFilePath, FILE_ONLY)    'not available in VBA...so it seems to always physically load it below, which seems to work fine.
   ' Remove extension from file name to get add-in name.
   strAddInName = Left(strAddInName, Len(strAddInName) - 4)
   ' Attempt to return reference to add-in.
   Set addXL = Excel.AddIns(strAddInName)
   If err <> 0 Then
      err.Clear
      ' If add-in is not in collection, add it.
      Set addXL = Excel.AddIns.Add(strFilePath)
      If err <> 0 Then
         ' If error occurs, exit procedure.
         LoadAddin = False
         GoTo exit_function
      End If
   End If
   ' Load add-in.
   If Not addXL.Installed Then addXL.Installed = True
   LoadAddin = True

exit_function:
   Exit Function
End Function

那么现在有没有办法将它添加到引用中,以便主机电子表格中引用这个新包含的加载项中的 VBA 的 VBA 代码能够正确执行?

看来要走的路线可能是这样的:

ThisWorkbook.VBProject.References.AddFromFile ("C:\MyFiles\MyAddin.xlam")

...但这给了我错误:

Microsoft Visual Basic for Applications
Run-time error '32813':
Application-defined or object-defined error

【问题讨论】:

  • 如果宿主项目对加载项中的过程有早期绑定引用,那么如果没有现有引用,您可能会收到编译错误:这些错误并不总是等到您实际调用代码引用另一个项目...
  • @Tim:我希望通过在 Workbook_Open() 中调用负载来解决这个问题。虽然,如果在电子表格打开时计算是自动的,它可能会很麻烦。也许可以在某处加载错误处理程序......
  • 您是否在选项下选中了“信任对 VBProject 的访问”?并且您添加了对 VBE 可扩展性库的引用?
  • 顺便说一下,打开 XLA/XLAM 工作簿比尝试将其添加到插件集合更简单。
  • 你最后找到解决办法了吗?

标签: vba excel


【解决方案1】:

您是否考虑在加载项的工作簿打开事件中使用相同的代码(但稍作修改)?

如果我理解正确,那么我想这就是你想要的?

Public ShouldIContinue As Boolean

Private Sub Workbook_Open()
    '~~> This is required to avoid the endless loop
    If ShouldIContinue = True Then Exit Sub

    Dim addXL As AddIn
    Dim strAddInName As String
    Dim oTempBk As Workbook

    strFilePath = ThisWorkbook.FullName

    strAddInName = ThisWorkbook.Name
    '~~> This will work for both .xla and .xlam
    strAddInName = Left(strAddInName, (InStrRev(strAddInName, ".", -1, _
    vbTextCompare) - 1))

    On Error Resume Next
    Set addXL = Excel.AddIns(strAddInName)
    On Error GoTo 0

    If Not addXL Is Nothing Then Exit Sub

    '~~> This is required to avoid the Run-time error '1004':
    '~~> "Unable to get the Add property of the AddIns class"
    '~~> OR
    '~~> "Add method of addins class failed"
    '~~> when there are no workbooks
    Set oTempBk = Workbooks.Add

    Set addXL = AddIns.Add(strFilePath, True)
    addXL.Installed = True

    oTempBk.Close

    ShouldIContinue = True
End Sub

【讨论】:

  • 这很可能是正确的,当我回到这个问题时,我会回到这里并标记为正确,如果它解决了这个问题。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-28
  • 1970-01-01
  • 2012-02-11
  • 1970-01-01
相关资源
最近更新 更多