Combination button/dropdown in office

核能气质少年 提交于 2019-12-23 21:18:18

问题


How do I add a combination button/dropdown in office (see below). Preferably with code.

Update: If it helps any, code isn't needed.


回答1:


you can do it, based on the following ActiveX controls:

  • Microsoft ImageList Control, Version 6
  • Microsoft ImageComboBox Control, Version 6

Manually, you select "More Controls..." from the [Control Toolbox] menu bar and double click the mentioned controls to get them on your sheet. Position the ComboBox where you want it to be, and disregard the position of the ImageList, it is visible only in design mode. By now you have two embedded contros named

  • ImageList1
  • ImageCombo1

The insertion of the two components also creates a reference to ...\system32\MSCOMCTL32.OCX.

Then you

  1. manually add icons (GIF, BMP, etc) to the Image list
  2. manually set the Combo's ImageList property to "ImageList1"
  3. manually set the Combo's AutoLoad property to True

By now you have a Combo with the error but no icons.

Then you execute this code

Sub FillCombo()
Dim SH As Worksheet, OO As OLEObjects, Idx As Integer

    Set SH = ActiveSheet
    Set OO = SH.OLEObjects


    With OO("ImageCombo1").Object
        .ComboItems.Clear
        For Idx = 1 To OO("ImageList1").Object.ListImages.Count
            .ComboItems.Add , , , Idx
        Next Idx
    End With

End Sub

I've tried hard to create the objects by VBA, but the ImageCombo seems to behave different when created as

Set SH = ActiveSheet
Set OO = SH.OLEObjects
OO.Add "MSComctlLib.ImageComboCtl.2"
' .... etc ....

The combo is created, but the dropdown arrow is not displayed no matter what I do, allthough debugger shows that all ListView elements are neatly attached. Lots of colleagues seem to have problems with that ActiveX, there's loads of posting on the net.

Further reading here



来源:https://stackoverflow.com/questions/4318837/combination-button-dropdown-in-office

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