You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Microsoft Windows Common Controls 6.0(SP6)实现VBA后期绑定?

VBA后期绑定Microsoft Windows Common Controls 6.0 (SP6)的方法

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:

  1. Open Registry Editor (regedit.exe)
  2. Navigate to HKEY_CLASSES_ROOT
  3. 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.OCX is registered on the target machine. If it's not, run regsvr32.exe C:\Windows\System32\MSCOMCTL.OCX as Administrator to register it.

内容的提问来源于stack exchange,提问作者Jeanjean

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:49:58