Excel-VBA 如果未安装插件则显示警告

Excel-VBA show Warning if Addon is not Installed

我正在使用 Excel 2013 文件,我更新了该文件以使用 PowerQuery 来更轻松地导入数据。

它已经使用了 VBA 宏,我想包含一个 Warning/MsgBox 和一个 link 来下载 PowerQuery,如果它还没有安装的话。

我如何检查主机系统上是否存在 PowerQuery?

改编 Rory 在 link 上的代码,前提是你会得到如下内容。注意:您可以使用 Rory 的附加代码来处理 2016 版本或更早版本,确保是否已安装。

因为您不能直接使用超链接,所以我修改了 Wiktor Stribiżew 的代码 允许用户在收到提示未安装的消息框后单击“确定”转到下载站点。

Option Explicit

Private Sub IsPowerQueryAvailable()
    Dim downloadlink As String
    downloadlink = "https://www.microsoft.com/en-gb/download/details.aspx?id=39379"

    Dim bAvailable As Boolean

    If Application.Version >= 16 Then
        bAvailable = True
    Else
        On Error Resume Next
        bAvailable = Application.COMAddIns("Microsoft.Mashup.Client.Excel").Connect
        On Error GoTo 0
        If Not bAvailable Then DownloadPowerQuery downloadlink
    End If

End Sub

Private Sub DownloadPowerQuery(downloadlink As String)

    Dim objShell As Object
    Dim Message As String
    Dim Wscript As Object

    Set objShell = CreateObject("Wscript.Shell")

    Message = MsgBox("Would you like to download PowerQuery?", vbYesNo, "Powerquery not available")

    If Message = vbYes Then
        objShell.Run (downloadlink)
    Else
        Wscript.Quit
    End If

End Sub