如何为Microsoft Windows Common Controls 6.0(SP6)实现VBA后期绑定?
Got it, let's clear this up for you! Unlike Outlook or PowerPoint which have a single top-level application object, the MSCOMCTL.OCX file is a collection of multiple UI controls (like ListView, TreeView, Toolbar, ImageList, etc.)—each with its own unique ProgID you need to use for late binding.
First: Find the right ProgID for your target control
Each control in this OCX follows a specific ProgID format: MSComctlLib.[ControlName]Ctrl.2. Here are the most common ones you'll work with:
- ListView:
MSComctlLib.ListViewCtrl.2 - TreeView:
MSComctlLib.TreeViewCtrl.2 - ImageList:
MSComctlLib.ImageListCtrl.2 - Toolbar:
MSComctlLib.ToolbarCtrl.2 - StatusBar:
MSComctlLib.StatusBarCtrl.2
If you need ProgIDs for other controls in the library:
- Open Registry Editor (
regedit.exe) - Navigate to
HKEY_CLASSES_ROOT - Look for keys starting with
MSComctlLib.—each sub-key corresponds to a control's ProgID
Alternatively, temporarily enable early binding (add the reference to "Microsoft Windows Common Controls 6.0"), press F2 to open the Object Browser, find the MSComctlLib library, and note the class name of the control you want (append Ctrl.2 to get the ProgID).
Example: Late binding a ListView control
Here's how to create and add a ListView to a form using late binding:
Sub AddLateBoundListView() Dim lv As Object ' Create the ListView instance using its ProgID Set lv = CreateObject("MSComctlLib.ListViewCtrl.2") ' Configure basic properties With lv .Visible = True .Left = 10 .Top = 10 .Width = 350 .Height = 250 .View = 3 ' Equivalent to lvwReport (use numeric values since no early binding constants) .Gridlines = True End With ' Add the control to your form (replace "UserForm1" with your form name) UserForm1.Controls.Add lv ' Add columns and sample items lv.ColumnHeaders.Add , , "Name", 150 lv.ColumnHeaders.Add , , "Value", 150 lv.ListItems.Add , , "Item 1", , 1 lv.ListItems(1).SubItems(1) = "100" End Sub
Key Notes
- Since late binding doesn't give you access to named constants (like
lvwReport), you'll need to use their numeric equivalents—you can look these up in the Object Browser during temporary early binding. - Make sure
MSCOMCTL.OCXis registered on the target machine. If it's not, runregsvr32.exe C:\Windows\System32\MSCOMCTL.OCXas Administrator to register it.
内容的提问来源于stack exchange,提问作者Jeanjean

