Search code examples
vbacomboboxms-wordactivexlegacy

How to populate a ComboBox from a Legacy dropdown result in VBA (Word)


I have a Word form (not a userform) containing Legacy DropDownList (FormField).

I can access these using the bookmark I set for them. (Ex: For one with the bookmark "bookmark")

ActiveDocument.FormFields("bookmark")

I used the Legacy dropdowns instead of the ActiveX ComboBox because I need to change the value of other Legacy dropdowns using the Result of one another and so on.

For example :

If ActiveDocument.FormFields("bookmark").Result <> "whatever" Then
    Select Case ActiveDocument.FormFields("bookmark").Result
        Case "Test"
            With ActiveDocument.FormFields("bookmark2").DropDown.ListEntries
                .Clear
                .Add "Whatever string I want"
                .Add "Another string I need"
            End With
    'Let's not put them all but there is more cases than one
    End Select
End If

In other words, the content of the next one is dependant on the content of the previous one.

That being said, I now need to do some multi-selection on the last one of these Dropdowns. The thing is, I can't do multi-select with Legacy Dropdowns from what I've read. It is possible though using ActiveX ComboBox, again, from what I've read.

The problem is, the way I made my "dependant" dropdowns is that, when th user selects a value, a Module is called doing the above code. Unfortunately, I can't use the ActiveX ComboBox in these Modules, only in the ThisDocument. If I could, I would only have to change the

ActiveDocument.FormFields("bookmark2").DropDown.ListEntries

for

Me.ComboBoxWhatever,

use AddItem instead of Add and find how to do multi-selection from there. But I can't.

From what I've yet tried/read, I can't call a Sub method from the ThisDocument (which here would do the code I need using the ComboBox) after a user selects a value from a Legacy Dropdowns.

How can I make it so when the user selects a value from a Legacy Dropdowns, I can check that Result and populate my ActiveX ComboBox with the appropriate values so the user can then select multiple choice from the ComboBox ?

EDIT : Cindy Meister answers is good for any ActiveX controls such as ListBox and so on.


Solution

  • You can access ActiveX controls outside of ThisDocument through the InlineShape or Shape object which contains it. Word manages non-Word content by "wrapping" them in a graphics object.

    Here's a bit of sample code I have that demonstrates how to add an item to the end of a combobox list using a procedure in a "normal" module. Notice how it uses the OLEFormat.Object to get "into" the ActiveX control's properties and methods.

    Sub AddItemToExistingCombo()
        Dim doc As word.Document
        Dim obj1 As String, oIndex As Long
        Dim of As word.OLEFormat
        Dim cb As ComboBox
    
        Set doc = ActiveDocument
        Set of = ActiveDocument.InlineShapes(1).OLEFormat
        'Set of = ActiveDocument.Bookmarks("combobox").Range.InlineShapes(1).OLEFormat
        Set cb = of.Object
        If Not cb Is Nothing Then
            oIndex = cb.ListCount
            obj1 = "item" & oIndex + 1
            cb.AddItem obj1, oIndex
        End If
    End Sub
    

    Of course, using the index value of the InlineShape isn't ideal, you might want to select and bookmark it. In order to do that, go into "Design mode", select the control and insert the bookmark.