没有更多引用时如何关闭该加载项? [英] How to close an add-in when there are no more References to it?
问题描述
我正在用Excel VBA编写一组简单的模块化加载项.
I'm writing a simple set of modular add-ins in Excel VBA.
在 Addin1.xlam
中,我选择了工具..参考",并添加了 MyMenus.xlam
作为参考.
In Addin1.xlam
, I have selected "Tools.. References" and added MyMenus.xlam
as a reference.
Addin1
也有一些代码可以从菜单中关闭.但是,当 Addin1
关闭时,即使不再需要或未引用任何内容, MyMenus
仍保持打开状态.
Addin1
also has some code to close itself from a menu. But when Addin1
closes, MyMenus
stays open, even though it is no longer needed or referenced by anything.
我可能还具有 Addin2
或 Addin3
,并引用了 MyMenus
.
I may also have Addin2
or Addin3
with a reference to MyMenus
.
在没有其他打开的项目具有引用的情况下,如何使 MyMenus
自动关闭?
How can I get MyMenus
to automatically close when no other open Project has a Reference to it?
或者,如何告诉 Addin1
关闭,并且也关闭我引用的所有内容"?
Alternatively, how can I tell Addin1
to "close, and also close anything I had a Reference to"?
推荐答案
保持 MyMenus
外接程序处于打开状态会导致任何问题吗?出于您所描述的原因(例如在其他地方引用),通常会完全保留此类引用.我建议保留所有内容,但如果您要继续进行,则需要分两个步骤完成:首先从第一个加载项( Addin1.xlam
)中删除引用,然后关闭第二个加载项( MyMenus.xlam
).这样的事情应该起作用:
Is keeping the MyMenus
add-in open causing any issue? References like this would normally be left open exactly for the reason you describe, i.e. in case it is referenced elsewhere. I would recommend leaving it all as is but if you do want to proceed then it needs to be done in two steps: First remove the reference from the first add-in (Addin1.xlam
), and then close the second add-in (MyMenus.xlam
). Something like this should work:
Dim objRef As Reference
Set objRef = ThisWorkbook.VBProject.References("MyMenus")
Call ThisWorkbook.VBProject.References.Remove(objRef)
'Do not raise an exception as may not be able to close if the add-in is referenced elsewhere
On Error Resume Next
Call Workbooks("MyMenus.xlam").Close(False)
On Error Goto 0
请注意,我在项目中添加了 Microsoft Visual Basic for Applications Extensibility
参考(或者仅将 objRef
声明为变体).
Note, I have the Microsoft Visual Basic for Applications Extensibility
reference added to the project (alternatively just declare objRef
as a Variant).
最后一次完成操作后,关闭 AddIn1.xlam
,请确保没有保存就这样做,否则将永久丢失引用.
When you do finally then close AddIn1.xlam
, make sure you do so without saving otherwise you'll permanently lose the reference.
这篇关于没有更多引用时如何关闭该加载项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!