Excel 2003 on 64-bit Windows 7 automatically changes reference to SysWOW64\MSCOMCTL.OCX so 32-bit Excel complains

廉价感情. 提交于 2019-12-10 09:50:20

问题


In an Excel 2003 VBA project I am using controls from MSCOMCTL.OCX. That is the VBA project has a reference to System32\MSCOMCTL.OCX.

When I open that project in Excel 2003 on my 64-bit Windows 7 system, Excel automatically changes the reference to SysWOW64\MSCOMCTL.OCX (which is the correct location).

However, when I send that project to my client who is using 32-bit Windows XP, the project complains during opening because SysWOW64\MSCOMCTL.OCX does not exist on his system.

Here are the (unsatisfactory) solutions I came up with so far:

  1. Instruct the client to manually change the reference back to the correct location on his system (System32\MSCOMCTL.OCX).

    • This does not really work for the following reasons:
      1. When Excel 2003 32-bit opens the sheet and it cannot find the reference to MSCOMCTL, it removes all the controls that came from the library (e. g. TreeCtrl) from the forms :-(
      2. Client is struggling with the procedure and it is quite cumbersome for him.
  2. Automatically correct the reference using VBA's VBProject.References.AddFromFile/AddFromGuid.
    • Same problem as above: When compilation of VBA during opening of workbook fails, Excel will remove all controls that it could not find from the forms.
  3. Automatically add the reference (as in 2.) and use dynamic binding to add all the relevant controls during runtime.
    • This could actually work, however currently I am struggling with binding the event handlers to the controls (but that will be separate question ;-)

Approaches 1. and 2. do not really solve anything and solution 3 is a lot of work.

Any ideas would be greatly appreciated.


回答1:


What if you automatically turned the reference off when the workbook closed? that way the reference wouldn't be 'broken' when the workbook is opened, and all your control should still be good.

i.e. :

Private Sub Workbook_Open()
'use environ variable for folder locs
If os = "64bit" Then
    Me.VBProject.References.AddFromFile ("C:\WINDOWS\SysWOW64\MSCOMCTL.OCX")
Else
    Me.VBProject.References.AddFromFile ("C:\WINDOWS\system32\MSCOMCTL.OCX")
End If
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    For Each ref In Me.VBProject.References
       If ref.Name = "MSComctlLib" Then
        Me.VBProject.References.Remove ref
       End If
        Next ref
End Sub

I did a quick test with the ADODB dll and it seemed to work,but I am not sure how you're using that DLL specifically; let me know if that works, though! Sure a lot better than option 3!



来源:https://stackoverflow.com/questions/14054371/excel-2003-on-64-bit-windows-7-automatically-changes-reference-to-syswow64-mscom

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!