SOLVED
Home

Userform Listbox HELP

%3CLINGO-SUB%20id%3D%22lingo-sub-959586%22%20slang%3D%22en-US%22%3EUserform%20Listbox%20HELP%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-959586%22%20slang%3D%22en-US%22%3E%3CP%3EGreetings%20Everyone%2C%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20need%20some%20HELP!%20Please%20bear%20with%20me%20if%20I%20ask%20too%20many%20questions%2C%20as%20I%20am%20fairly%20new%20to%20VBA.%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAnyways%2C%26nbsp%3B%26nbsp%3BI%20created%20a%20userform%20in%20Word%202016%20to%20auto-populate%20a%20template%20but%20I%20would%20like%20to%20create%20a%20drop-down%20where%20a%20user%20could%20select%20text%20from%20the%20drop-down%20and%20insert%20the%20selected%20text%20in%20the%20Word%20template.%20How%20would%20I%20go%20about%20doing%20so%3F%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20was%20able%20to%20create%20a%20listbox%20in%20the%20userform%20with%20the%20following%20code%20below%2C%20however%20how%20do%20I%20get%20the%20item%20selected%20to%20populate%20in%20the%20word%20template%3F%3F%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPrivate%20Sub%20UserFrom_Initialize()%3C%2FP%3E%3CP%3EWith%20ListBox1%3C%2FP%3E%3CP%3E.AddItem%20%22VIA%20US%20MAIL%20ONLY%22%3C%2FP%3E%3CP%3E.AddItem%20%22VIA%20Electronic%20Mail%20Only%22%3C%2FP%3E%3CP%3EEnd%20With%3C%2FP%3E%3CP%3Elbl_Exit%3A%3C%2FP%3E%3CP%3EExit%20Sub%3C%2FP%3E%3CP%3EEnd%20Sub%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20in%20advance%20for%20anyone's%20assistance!!%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-959586%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Elistbox%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EUserform%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EVba%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-961200%22%20slang%3D%22en-US%22%3ERe%3A%20Userform%20Listbox%20HELP%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-961200%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F388183%22%20target%3D%22_blank%22%3E%40R3d3mpt10n%3C%2FA%3E%26nbsp%3B%20The%20code%20to%20update%20the%20document%20should%20usually%20go%20into%20the%20Click%20procedure%20of%20the%20OK%20button%20(or%20whatever%20you've%20named%20it).%20In%20order%20to%20write%20that%20code%2C%20you%20need%20to%20know%20where%20in%20the%20document%20the%20text%20will%20go%3A%20it%20could%20be%20a%20bookmark%2C%20a%20content%20control%2C%20a%20legacy%20text%20form%20field%2C%20a%20particular%20table%20cell%2C%20etc.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAs%20an%20example%20using%20a%20bookmark%2C%20your%20code%20can%20look%20like%20this%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EPrivate%20Sub%20btnOK_Click()%3CBR%20%2F%3EDim%20rg%20As%20Range%3CBR%20%2F%3E%3CBR%20%2F%3ESet%20rg%20%3D%20ActiveDocument.Bookmarks(%22TextHere%22).Range%3CBR%20%2F%3Erg.Text%20%3D%20ListBox1.Value%3CBR%20%2F%3EActiveDocument.Bookmarks.Add%20%22TextHere%22%2C%20rg%3CBR%20%2F%3E%3CBR%20%2F%3E'%20any%20other%20actions%20that%20are%20needed%3CBR%20%2F%3E%3CBR%20%2F%3EUnload%20Me%3CBR%20%2F%3EEnd%20Sub%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EPrivate%20Sub%20UserForm_Initialize()%3CBR%20%2F%3EWith%20ListBox1%3CBR%20%2F%3E.AddItem%20%22VIA%20US%20MAIL%20ONLY%22%3CBR%20%2F%3E.AddItem%20%22VIA%20Electronic%20Mail%20Only%22%3CBR%20%2F%3E.ListIndex%20%3D%200%20'%20guarantee%20that%20an%20entry%20is%20selected%3CBR%20%2F%3EEnd%20With%3CBR%20%2F%3Elbl_Exit%3A%3CBR%20%2F%3EExit%20Sub%3CBR%20%2F%3EEnd%20Sub%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E(The%20code%20for%20re-adding%20the%20bookmark%20after%20the%20inserted%20text%20has%20erased%20it%20comes%20from%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fwordmvp.com%2FFAQs%2FMacrosVBA%2FInsertingTextAtBookmark.htm%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fwordmvp.com%2FFAQs%2FMacrosVBA%2FInsertingTextAtBookmark.htm%3C%2FA%3E%26nbsp%3B).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20the%20target%20in%20the%20document%20is%20one%20of%20the%20other%20things%2C%20or%20if%20you%20want%20help%20deciding%20which%20to%20use%2C%20reply%20to%20this%20thread.%3C%2FP%3E%3C%2FLINGO-BODY%3E
R3d3mpt10n
Occasional Contributor

Greetings Everyone, 

 

I need some HELP! Please bear with me if I ask too many questions, as I am fairly new to VBA.  

 

Anyways,  I created a userform in Word 2016 to auto-populate a template but I would like to create a drop-down where a user could select text from the drop-down and insert the selected text in the Word template. How would I go about doing so?    

 

I was able to create a listbox in the userform with the following code below, however how do I get the item selected to populate in the word template???

 

Private Sub UserFrom_Initialize()

With ListBox1

.AddItem "VIA US MAIL ONLY"

.AddItem "VIA Electronic Mail Only"

End With

lbl_Exit:

Exit Sub

End Sub

 

Thank you in advance for anyone's assistance!! 

1 Reply
Solution

@R3d3mpt10n  The code to update the document should usually go into the Click procedure of the OK button (or whatever you've named it). In order to write that code, you need to know where in the document the text will go: it could be a bookmark, a content control, a legacy text form field, a particular table cell, etc. 

 

As an example using a bookmark, your code can look like this:

 

Private Sub btnOK_Click()
Dim rg As Range

Set rg = ActiveDocument.Bookmarks("TextHere").Range
rg.Text = ListBox1.Value
ActiveDocument.Bookmarks.Add "TextHere", rg

' any other actions that are needed

Unload Me
End Sub

 

Private Sub UserForm_Initialize()
With ListBox1
.AddItem "VIA US MAIL ONLY"
.AddItem "VIA Electronic Mail Only"
.ListIndex = 0 ' guarantee that an entry is selected
End With
lbl_Exit:
Exit Sub
End Sub

 

(The code for re-adding the bookmark after the inserted text has erased it comes from https://wordmvp.com/FAQs/MacrosVBA/InsertingTextAtBookmark.htm ).

 

If the target in the document is one of the other things, or if you want help deciding which to use, reply to this thread.

Related Conversations
Tabs and Dark Mode
cjc2112 in Discussions on
35 Replies
Extentions Synchronization
Deleted in Discussions on
3 Replies
flashing a white screen while open new tab
Deleted in Discussions on
14 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies