在VBA中打开特定的Excel实例

时间:2017-07-05 06:48:52

标签: vba excel-vba excel-2010 excel-2016 excel

希望有人知道如何做到这一点,我对VBA不太好。

我的机器上安装了Excel 2016(64位)和Excel 2010(32位)。这是因为我有一些旧的基于excel的工具,只能在2010年运行。 其中一个工具包含以下代码:

Start:

            ThisWorkbook.Activate
            strOutputsFilePath = gStrOutputsPath
            strDMfilename = [DMFilePath]

   '2. Refresh the PowerPivot model with the new data

            'Create a new instance of excel
            Application.StatusBar = "Opening a new instance of MS Excel"

            Set appExcelApp = New Excel.Application


            'Disconnect and Re-connect to the PowerPivot Add-in (in case it has been disconnected)

            For Each comAddin In appExcelApp.COMAddIns

                If comAddin.Description = "PowerPivot for Excel" Then
                        Set comAddinPPVT = comAddin
                End If

            Next

            If Not comAddinPPVT Is Nothing Then

                comAddinPPVT.Connect = False
                comAddinPPVT.Connect = True

            End If

            'Apply the settings for the Excel application

            With appExcelApp
                    .Visible = False
                    .ScreenUpdating = False
                    .DisplayAlerts = False
                    .EnableEvents = False
            End With

应用程序应该执行的是运行Excel 2010的新实例并在其中打开某些文件。 但是,安装2016后,新Excel.Application 命令默认打开Excel 2016而不是2010.由于数据模型之间不兼容,这会导致PowerPivot插件出错。

问题是:有没有办法在VBA代码中指定我想要一个Excel 2010实例?

尝试在我的系统上默认使用Excel 2010,但代码仍会打开2016并出错:\

非常感谢任何帮助!

由于

2 个答案:

答案 0 :(得分:0)

没有简单的解决方法。不建议或不支持在一台计算机上使用两种不同版本和两种不同的Excel版本的Excel。这很不寻常,所以没有开箱即用的解决方案。

我的建议是,如果您不希望Excel 2016(64位)干扰您的Excel 2013(32位)内容,请不要在同一台计算机上安装这两个版本。

考虑使用虚拟机(VM)运行一个版本并将另一个版本保留在主机上。这将确保他们不会干涉。

答案 1 :(得分:0)

如何使用Shell功能?

Private Sub OpenExcelInstance(version as string)
Dim path As String
Dim appInstance As Variant
Select Case version
    Case "Excel 2010" 
         path = "C:\Program Files (x86)\blablabla\Excel.exe" 'path to 32bit 2010 executable
    Case "Excel 2016"
         path = "C:\Program Files\blablabla\Excel.exe" 'path to 64 bit 2016 executable.
    Case default
         Exit sub
End Select

appInstance = Shell(path, 1)

'GetObject function here.
End Sub

但是,您无法对此appInstance进行编码 - 它是shell命令返回的整数。

之后可以使用GetObject从中获取Application实例,但是我没有深入研究,因为我遇到了记录here的问题并且有一个SO问题{{ 3}}(不是真的没有答案)。

相关问题