支持Excel的自动化多个版本的Access应用程序多个、应用程序、版本、Excel

2023-09-08 11:15:58 作者:/

我有一个访问应用程序,在多用户ENV访问2013开发的,使用Excel的自动导出和格式化的Excel文件。

I have an Access app, developed in Access 2013 in multi-user env, uses Excel automation to export and format an Excel file.

正常办公/ Excel的2013(15.0)引用都被提出和一切运作良好的Office 2013的机器。不上2010的机器发挥很好。

The normal Office/Excel 2013 (15.0) references have been made and all works well on Office 2013 machines. Does not play nicely on 2010 machines.

使用2010的机器,我换成了15.0参考14.0引用和应用程序很高兴于2010年和2013年的机器。当我2013机旁编辑/更新15.0引用返​​回。

Using a 2010 machine, I replaced the 15.0 references with 14.0 references, and the app is happy on 2010 and 2013 machines. Upon next edit/update on my 2013 machine the 15.0 references return.

任何建议,以更方便地开发/运营在这个多​​版本的环境?

Any suggestions to more conveniently develop/operate in this multi-version environment?

谢谢!

推荐答案

总体解决这个问题是使用的后期绑定。该缺点后期绑定是

The overall solution to this issue is to use late binding. The downsides to late binding are

昏暗xlApp作为对象意味着我们没有得到任何智能感知 xlApp 和 有关的常数像 xlEdgeTop 不无关联的参考定义 Dim xlApp As Object means that we don't get any IntelliSense for xlApp, and related constants like xlEdgeTop are not defined without the associated Reference

这些问题都可以通过使用条件编译VBA项目得到缓解。为了发展,需​​要参考添加到项目中,并定义一个条件编译参数

These issues can be mitigated by using conditional compilation in the VBA project. For development, add the required Reference to the project and define a conditional compilation argument

您可以在您的code使用这样

which you can use in your code like this

Option Compare Database
Option Explicit

Public Sub WorkWithExcel()
    #If LateBinding Then
        Dim xlApp As Object
        Set xlApp = CreateObject("Excel.Application")
    #Else
        Dim xlApp As Excel.Application
        Set xlApp = New Excel.Application
    #End If

    Debug.Print xlEdgeTop
End Sub

要避免混乱,我会倾向于保持常量在这样一个单独的模块

To avoid clutter, I would be inclined to keep the constants in a separate Module like this

Option Compare Database
Option Explicit

#If LateBinding Then
    Public Const xlEdgeTop = 8
#End If

在code调整完成后,取出的参考,将 LateBinding 参数为True( LateBinding = -1 )并编译该项目。添加你错过任何常量(也似乎总是一个或两个),当它编译没有参考你要善于进行部署。

When the code tweaking is complete, remove the Reference, set the LateBinding argument to "True" (LateBinding = -1) and compile the project. Add any constants you've missed (there always seems to be one or two) and when it compiles without the Reference you should be good to deploy.

有关下一发展会议,设置LateBinding回假( LateBinding = 0 ),并添加引用。最

For the next development session, set LateBinding back to "False" (LateBinding = 0) and add the Reference back in.