Ms office 如何知道是否安装了Office的VBA组件?

Ms office 如何知道是否安装了Office的VBA组件?,ms-office,vba,excel,Ms Office,Vba,Excel,My Excel加载项要求安装Excel的Visual Basic for Applications选项,以使其正常工作。我希望我的安装(使用InnoSetup编写)能够检测是否安装了VBA,并在未安装时警告用户 如何检测该选项是否已安装 一种可能是检查C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6中是否存在VBE6.DLL。或者在注册表中四处寻找对该DLL或字符串VBA的引用 请注意,对于Office 2010,此位置/文件名可

My Excel加载项要求安装Excel的Visual Basic for Applications选项,以使其正常工作。我希望我的安装(使用InnoSetup编写)能够检测是否安装了VBA,并在未安装时警告用户

如何检测该选项是否已安装


一种可能是检查C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6中是否存在VBE6.DLL。或者在注册表中四处寻找对该DLL或字符串VBA的引用


请注意,对于Office 2010,此位置/文件名可能会有所不同,因为VBA编辑器中有一些更改。

为什么不尝试这样的函数。。。

公共静态类VbePrequisiteDetector{
私有常量字符串VbeInstallationPathKey=@“软件\Microsoft\VBA”;
私有常量字符串Vbe6InstallationPathValue=“Vbe6DllPath”;
私有常量字符串Vbe7InstallationPathValue=“Vbe7DllPath”;
/// 
///如果安装了VBE6,则返回true。VBE6是Office2003和Office2007的先决条件
/// 
///如果安装了VBE6,则返回true。
公共静态bool IsVbe6Installed(){
试一试{
RegistryKey vbaPathKey=Registry.LocalMachine.OpenSubKey(VbeInstallationPathKey);
如果(vbaPathKey!=null){
如果(vbaPathKey.GetValue(Vbe6InstallationPathValue)!=null){
字符串pathToVbe=(字符串)vbaPathKey.GetValue(Vbe6InstallationPathValue);
if(File.Exists(pathToVbe)){
返回true;
}
}
}
}
捕获(例外){
//忽略所有例外情况
}
返回false;
}
/// 
///如果已安装VBE7,则返回true。VBE7是Office2010的先决条件
/// 
///如果安装了VBE7,则返回true。
公共静态bool IsVbe7Installed(){
试一试{
RegistryKey vbaPathKey=Registry.LocalMachine.OpenSubKey(VbeInstallationPathKey);
如果(vbaPathKey!=null){
如果(vbaPathKey.GetValue(Vbe7InstallationPathValue)!=null){
字符串pathToVbe=(字符串)vbaPathKey.GetValue(Vbe7InstallationPathValue);
if(File.Exists(pathToVbe)){
返回true;
}
}
}
}
捕获(例外){
//忽略所有例外情况
}
返回false;
}
}

我们讨论的是Windows Installer组件。 安装程序有一个API,您可以在其中请求是否安装了功能/组件。 当然,api也返回组件的安装位置。 如果需要,您可以安装缺少的组件

您唯一需要的是组件和产品guid


检测是否安装了VBA的最佳方法是使用MsiQueryFeatureState API并询问Windows Installer是否安装了该功能。下面是一些在VB.NET中执行此操作的示例代码,但是您可以用任何允许您调用COM组件的语言编写此代码(抱歉,不熟悉InnoSetup)


这上面的标签是VBA和Office。您确定要发布与标签无关的答案而不做任何解释吗?
Option Explicit
Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long

Private Sub cmdCheck_Click()
MsgBox "Exist ???    =" & CheckForComponent("user32.dll")
End Sub

Private Function CheckForComponent(ComPath As String) As Boolean
Dim Ret As Long
Ret = LoadLibrary(ComPath)
FreeLibrary Ret

If Ret = 0 Then
        CheckForComponent = False
    Else
        CheckForComponent = True
End If

End Function 
public static  class VbePrerequisiteDetector {
    private const string VbeInstallationPathKey = @"SOFTWARE\Microsoft\VBA";
    private const string Vbe6InstallationPathValue = "Vbe6DllPath";
    private const string Vbe7InstallationPathValue = "Vbe7DllPath";

    /// <summary>
    /// Return true if VBE6 installed. VBE6 is prerequisite for for Office2003 and Office2007
    /// </summary>
    /// <returns>Return true if VBE6 installed.</returns>
    public static bool IsVbe6Installed() {
        try {
            RegistryKey vbaPathKey = Registry.LocalMachine.OpenSubKey(VbeInstallationPathKey);

            if (vbaPathKey != null) {
                if (vbaPathKey.GetValue(Vbe6InstallationPathValue) != null) {
                    string pathToVbe = (string)vbaPathKey.GetValue(Vbe6InstallationPathValue);
                    if (File.Exists(pathToVbe)) {
                        return true;
                    }

                }
            }
        }
        catch (Exception) {
            //Ignore all exceptions
        }
        return false;
    }

    /// <summary>
    /// Return true if VBE7 installed. VBE7 is prerequisite for for Office2010
    /// </summary>
    /// <returns>Return true if VBE7 installed.</returns>
    public static bool IsVbe7Installed() {
        try {
            RegistryKey vbaPathKey = Registry.LocalMachine.OpenSubKey(VbeInstallationPathKey);

            if (vbaPathKey != null) {
                if (vbaPathKey.GetValue(Vbe7InstallationPathValue) != null) {
                    string pathToVbe = (string)vbaPathKey.GetValue(Vbe7InstallationPathValue);
                    if (File.Exists(pathToVbe)) {
                        return true;
                    }

                }
            }
        }
        catch (Exception) {
            //Ignore all exceptions
        }
        return false;
    }
}
Private Declare Function MsiQueryFeatureState Lib "Msi" Alias "MsiQueryFeatureStateA" (ByVal Product As String, ByVal Feature As String) As Long

Public Function FVbaAvailable() As Boolean

    Dim objExcelApp As Object
    Dim strProductCode As String
    Dim nState As Long
    Dim fAvailable As Boolean = False

    Try
        ' Start an Excel instance and get the product code.
        objExcelApp = CreateObject("Excel.Application")
        strProductCode = DirectCast(objExcelApp.ProductCode, String)

        ' Get FeatureState for the VBAFiles Feature.
        nState = MsiQueryFeatureState(strProductCode, "VBAFiles")

        If (nState = 1) OrElse (nState = 3) OrElse (nState = 4) Then
            ' VBA is available.
            fAvailable = True
        End If

        ' Clean up.
        objExcelApp.Quit()
        Runtime.InteropServices.Marshal.FinalReleaseComObject(objExcelApp)
        objExcelApp = Nothing
    Catch ex As Exception
        Trace.WriteLine(ex.Message)
    End Try

    Return fAvailable
End Function