如何使用 VBA 启用/添加和禁用/删除 Excel 加载项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26810570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to enable/add and disable/remove an Excel Add-In using VBA?
提问by NathaneilCapital
Something like if addin exists then do nothing else add_addin("AddinName")
.
类似的东西if addin exists then do nothing else add_addin("AddinName")
。
I have a few add-ins, when enabled would first pops up a message. So if I enable them at the excel open automatically, it becomes very annoying. If I can assign a few VBA code to do the enabling and put a quick access button for the code, then it is much easier to manage and I only need to enable them as needed.
我有一些加载项,启用后会首先弹出一条消息。因此,如果我在自动打开 excel 时启用它们,就会变得非常烦人。如果我可以分配一些 VBA 代码来进行启用并为代码放置一个快速访问按钮,那么管理起来就会容易得多,我只需要根据需要启用它们。
回答by Ibo
NathaneilCapital since you don't have any code I will just explain the procedure for you.
NathaneilCapital 因为您没有任何代码,所以我将为您解释程序。
Adding a new add-in and installing it (make it appear on the ribbon) is very easy.
添加新加载项并安装它(使其出现在功能区上)非常简单。
Adding an Add-In:
添加加载项:
Copy your xlam file to the library folder using FileCopy command. You can get the address where you should copy the file TO easily like this:
使用 FileCopy 命令将您的 xlam 文件复制到库文件夹。您可以像这样轻松获取应该将文件复制到的地址:
sAddInPathTo = Application.UserLibraryPath
sAddInPathTo = Application.UserLibraryPath
sToFullName = sAddInPathTo & "\" & "MyAddInName.xlam"
sToFullName = sAddInPathTo & "\" & "MyAddInName.xlam"
FileCopy sFromFullName, sToFullName
FileCopy sFromFullName, sToFullName
At this point if you go to Developer/Add-Ins you would see the name of the addIn but the checkbox next to it is not checked meaning it is not enabled/installed. To do that you can use:
此时,如果您转到 Developer/Add-Ins,您将看到 addIn 的名称,但未选中它旁边的复选框,这意味着它未启用/安装。为此,您可以使用:
AddIns("MyAddInName").Installed=True
AddIns("MyAddInName").Installed=True
After this line, your add-in should appear on the Excel ribbon as a new Tab.
在此行之后,您的加载项应作为新选项卡出现在 Excel 功能区上。
To uninstall the add-in you can simply so this:
要卸载加载项,您可以简单地执行以下操作:
AddIns("MyAddInName").Installed=False
AddIns("MyAddInName").Installed=False
If you would do this manually, you should open up the AddInInstallerManager (Developer/Add-Ins) and uncheck your add-in.
如果您要手动执行此操作,则应打开 AddInInstallerManager(开发人员/加载项)并取消选中您的加载项。
However, removing it from the Add-Ins list in the AddInInstallerManager is more complicated. First, you should delete the xlam file from the library address which is simply done by using:
但是,从 AddInInstallerManager 的 Add-Ins 列表中删除它更复杂。首先,您应该从库地址中删除 xlam 文件,这只需使用:
Kill sToFullName
Kill sToFullName
Make sure you uninstall first, otherwise windows will not be able to delete the file.
确保先卸载,否则windows将无法删除文件。
Second, which is the most difficult part is to clean the registry. In fact after the above mentioned line, you would see the add-in name in the AddInInstallerManager, but when you browse you would not see the xlam file there, which is kind of inconsistensy. Because AddInInstallerManager lists those add-ins from an ini file. Practically if you get to this point, it is still fine and in case Excel finds out about it, it will refresh the ini file and you should be good, but to do this manually, you can open the AddInInstallerManager and when you click on the name of the add-in, it will throw and error for you and then it will remove it from its list. You can do this either but using send keys to simulate it or simply modify the registry. See this for more information:
其次,最困难的部分是清理注册表。实际上,在上述行之后,您会在 AddInInstallerManager 中看到加载项名称,但是当您浏览时,您将看不到那里的 xlam 文件,这有点不一致。因为 AddInInstallerManager 从 ini 文件中列出了这些加载项。实际上,如果你到了这一点,它仍然很好,如果 Excel 发现它,它会刷新 ini 文件,你应该没问题,但要手动执行此操作,您可以打开 AddInInstallerManager,然后单击加载项的名称,它将为您抛出并出错,然后将其从列表中删除。您可以这样做,但使用发送键来模拟它或简单地修改注册表。请参阅此了解更多信息: