Log in

View Full Version : How can I add multiple items to Word drop downs without keying each item in?



loebigs
06-05-2018, 08:14 AM
Hello!

So my goal is to enter as many names as I want at once into the Word drop downs created in the Design Mode under the Developer tab. As it is currently, you can only enter one name at a time. I have over 300 names I need to add to multiple drop downs on multiple documents so I have looked for a Macro solution.

I found this solution linked here with the following code: https://answers.microsoft.com/en-us/msoffice/forum/msoffice_word-mso_other-mso_2010/2010-word-drop-down-list-how-can-i-add-multiple/cfa634a9-abb9-49bc-bdd3-8ba0cbfefa85


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oCC As ContentControl
Dim oRng As Word.Range
Dim lngIndex As Long
'Assumes your dropdown is named DDList.
Set oCC = ActiveDocument.SelectContentControlsByTitle("DDList").Item(1)
Set oRng = Selection.Range
oCC.DropdownListEntries.Clear
'Assumes that you have a list selected in the document _
and each list entry starts a new paragraph.
For lngIndex = 1 To oRng.Paragraphs.Count
oCC.DropdownListEntries.Add Left(oRng.Paragraphs(lngIndex).Range.Text, _
Len(oRng.Paragraphs(lngIndex).Range.Text) - 1)
Next lngIndex
End Sub

When I first used this I didn't realize I had to title the drop down with "DDList" (or whatever I want to call it; I have different name lists for different drop downs so I assume I can make a bunch of different ones with different names). Anyway, after doing that I was able to run the macro successfully and I was ecstatic. However after trying to run it on multiple drop downs at once it doesn't work. I asked the creator of the code if there was something wrong or if there was a work around I can do and he replied:

"The code works with the first titled “DDList” If you have more than one titled DDList then you would have to used .Item(2), .Item(3) etc. Or if your CCs have different titles then change “DDList” in code above to the appropriate title as you process each one."

I have no idea what this means or how I can use it to do what I want it to do. I am brand new to VBA's/Macros, I didn't even know they existed until this. Luckily google exists but I still can't seem to figure out what he means and what I should do to solve my problem. I'm hoping you guys can help me.

Some side notes: I'm using Word 2016 on Windows 10

gmayor
06-06-2018, 01:42 AM
You could use the content control list editor option of https://www.gmayor.com/insert_content_control_addin.htm and copy and paste the list of names into the list editor - one per line.

With regard to your specific question, the quoted code processes the first content control with the title "DDList"

Set oCC = ActiveDocument.SelectContentControlsByTitle("DDList").Item(1)

i.e. Item(1)

Content control titles can be duplicated (though this can create confusion if the controls are not mapped and copied). If you have duplicated titles e.g. You copy the first control and paste it elsewhere in the document, they both have the same title and content but remain independent from one another, however the code you gave quoted will only process the first one.

If you want to process (say) the second one using a macro, change Item(1) to Item(2)

Clearly in such circumstances it would be better to have different title (or tags) for the controls and avoid that confusion. To that end https://www.gmayor.com/ExtractDataFromForms.htm includes a utility that can step through the controls and allow you to edit their titles and tags.

Mapped controls behave as one control so when copied as above whatever is selected in one is also selected in the rest. This can be useful for repeating values throughout the document.

macropod
06-06-2018, 05:47 AM
Cross-posted at: https://www.excelforum.com/word-programming-vba-macros/1233332-how-can-i-add-multiple-items-to-word-drop-downs-without-keying-each-item-in.html
Please read VBA Express' policy on Cross-Posting in item 3 of the rules: http://www.vbaexpress.com/forum/faq.php?faq=new_faq_item#faq_new_faq_item3