vba 添加安装并引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15951518/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 20:34:00  来源:igfitidea点击:

Add in installed and referenced

excel-vbaadd-invbaexcel

提问by Santosh

I want to check if addin is installed and is referenced. The below code checks for add in is installed or not. How can i check if its referenced in excel.

我想检查插件是否已安装并被引用。下面的代码检查插件是否安装。我如何检查它是否在 excel 中被引用。

By Refernced i mean is Tools > Addins > Addins Dailog box > If addins is installed > check if a addin with particular name is checked.

引用我的意思是工具> 插件> 插件对话框> 如果安装了插件> 检查是否选中了具有特定名称的插件。

I would like preferably without any loop.

我希望最好没有任何循环。

Sub Demo() 
    Dim b As Boolean 
    b = CheckAddin("Solver add-in") 
    MsgBox "Solver is " & IIf(b, "", "not ") & "installed" 
End Sub 

Function CheckAddin(s As String) As Boolean 
    Dim x As Variant 
    On Error Resume Next 
    x = AddIns(s).Installed 
    On Error Goto 0 
    If IsEmpty(x) Then 
        CheckAddin = False 
    Else 
        CheckAddin = True 
    End If 
End Function 

回答by Siddharth Rout

Sub Sample()
    Dim wbAddin As Workbook

    On Error Resume Next
    Set wbAddin = Workbooks(AddIns("My Addin").Name)

    If Err.Number <> 0 Then
        On Error GoTo 0
        'Set wbAddin = Workbooks.Open(AddIns("My Addin").FullName)
        Debug.Print "Not Referenced"
    Else
        Debug.Print "Referenced"
    End If
End Sub

回答by Doug Glancy

You need to test is the addin is open, pretty much like any other workbood. This will return True if an addin is loaded:

您需要测试插件是否打开,就像任何其他工作区一样。如果加载了插件,这将返回 True:

Function AddinIsLoaded(AddinName As String) As Boolean
On Error Resume Next
AddinIsLoaded = Len(Workbooks(AddIns(AddinName).Name).Name) > 0
End Function

For example:

例如:

Sub Test
Debug.Print AddinIsLoaded("Solver add-in")
End Sub

回答by GisMofx

I've had a problem that even when the function returns True, I would still get an error when trying to use that addin. It turns out, an addin can be installed, but not "open". So, in addition to checking for the addin, I also check if the addin file is open. If not, I open the addin. See my question and answer here:

我遇到了一个问题,即使函数返回 True,我在尝试使用该插件时仍然会收到错误消息。事实证明,可以安装插件,但不能“打开”。所以,除了检查插件之外,我还会检查插件文件是否打开。如果没有,我打开插件。在此处查看我的问题和答案:

Excel VBA Checking if Addin Is Installed But Not Open

Excel VBA 检查插件是否已安装但未打开