1
votes

I created a document and added a drop-down list content control in Microsoft Word 2010. How would I select this object and add items to it via VBA? An exhaustive search has led to being able to add one and add items, but I want to know how to add items to a list already on the document.

Also, is it possible to change the extension of a Microsoft Word 2010 document to (.zip) and add in an XML file. This XML file would house all the items to be added to the drop-down content control.

1
What code have you tried? - user1379931
@bibadia: Current, there was no specific code that I used. I tried to use the examples from MSDN and TechNet but none worked as those examples all required me to add a new drop-down content control to the document. What I would like to is pre-make my form and load all items into a drop-down content control when a document opens. - TroyPilewski
With Word VBA you can often learn something by looking at the relevant objects in the View->Object browser pane in the VB Editor and in many cases (but by no means all) recording a macro will give you some clues (in this case, recording the action of inserting some list items manually will give you a pointer, but probably not the most appropriate code. - user1379931
@bibadia: I have tried to record the macro into VBA but once I hit the record button I cannot click on a drop-down content control. I can add one and add items to it but not select a drop-down content control that is already on the document. - TroyPilewski
If you select one first, then record the macro, you get information. Butit won't tell you how to specify a particular control to work with in code - so for that, you would typically go and look at the object model to see for example which objects have a ContentControls collection property, and how to identify a control within that collection. - user1379931

1 Answers

2
votes

Something like

For Each item In ActiveDocument.ContentControls
    If item.Title = "DropDown1" Then
        item.DropdownListEntries.Add Text:="Item1", Value:="Item1"
        item.DropdownListEntries.Add Text:="Item2", Value:="Item2"
        Exit For
    End If
Next

You can find it by title or tag. Or by type - if it is only one dropdown in the document.