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

发布于 2024-08-04 02:56:58 字数 279 浏览 5 评论 0原文

我的 Excel 插件需要安装 Excel 的 Visual Basic for Applications 选项才能正常工作。我希望我的安装(用 InnoSetup 编写)能够检测是否安装了 VBA,如果没有安装,则警告用户。

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

替代文本

My Excel addin requires the Visual Basic for Applications option of Excel to be installed in order for it to work. I would like my install (which is written with InnoSetup) to be able to detect if VBA is installed and warn the user if it is not.

How can I detect if the option is already installed?

alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

她如夕阳 2024-08-11 02:56:58

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

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

One possibility is to check for the presence of VBE6.DLL in C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6. Or poke about in the registry looking for references to that DLL or the string VBA.

Note that this location/file name might be different for Office 2010 as there are some changes in the VBA editor.

野稚 2024-08-11 02:56:58

你为什么不尝试这样的功能......
在这里找到

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 

Why don't you try a function like this...
found here

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 
可爱咩 2024-08-11 02:56:58

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

您唯一需要的是组件和产品指南。

查看文档

We are talking about Windows Installer components.
The Installer has an API, where you can request if a feature/component is installed.
ofcurse that api also return where the component is installed.
if nessacary you can install missing components.

the only thing you need, is the component und product guid.

see documentation

⒈起吃苦の倖褔 2024-08-11 02:56:58
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;
    }
}
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;
    }
}
颜漓半夏 2024-08-11 02:56:58

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

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

The best way to detect if VBA is installed is to use the MsiQueryFeatureState API and ask Windows Installer whether the feature is installed or not. Below is some sample code that does this in VB.NET, however you could code this in any language that allows you to call COM components (sorry, not familiar with InnoSetup).

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文