Excel 2003 on 64-bit Windows 7 automatically chang

2019-05-11 09:02发布

问题:

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!