Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-17-2021, 02:31 AM
JamesWood JamesWood is offline Listbox to select field name from data Windows 10 Listbox to select field name from data Office 2019
Advanced Beginner
Listbox to select field name from data
 
Join Date: Nov 2020
Posts: 37
JamesWood is on a distinguished road
Unhappy Listbox to select field name from data

Hi guys


I'd like to create a form/listbox in Word that displays all the headings from a data source currently loaded for a mail merge, and then I can click and select one (which it will actually then use as a string, but irrelevant for this post).


Please see the attached image. Any thoughts?? Would really help a ton.


Thanks a lot!


James
Attached Images
File Type: png fields window.png (3.0 KB, 28 views)
Reply With Quote
  #2  
Old 05-17-2021, 04:06 AM
gmayor's Avatar
gmayor gmayor is offline Listbox to select field name from data Windows 10 Listbox to select field name from data Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Do you mean like

Code:
Private Sub UserForm_Initialize()
Dim lngFld As Long
    With ActiveDocument.MailMerge.DataSource
        ListBox1.Clear
        For lngFld = 1 To .FieldNames.Count
            ListBox1.AddItem .FieldNames(lngFld)
        Next
    End With
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 05-17-2021, 06:15 AM
JamesWood JamesWood is offline Listbox to select field name from data Windows 10 Listbox to select field name from data Office 2019
Advanced Beginner
Listbox to select field name from data
 
Join Date: Nov 2020
Posts: 37
JamesWood is on a distinguished road
Talking

Quote:
Originally Posted by gmayor View Post
Do you mean like

Code:
Private Sub UserForm_Initialize()
Dim lngFld As Long
    With ActiveDocument.MailMerge.DataSource
        ListBox1.Clear
        For lngFld = 1 To .FieldNames.Count
            ListBox1.AddItem .FieldNames(lngFld)
        Next
    End With
End Sub

Amazing! Thanks so much!!!
Reply With Quote
  #4  
Old 05-25-2021, 07:39 AM
JamesWood JamesWood is offline Listbox to select field name from data Windows 10 Listbox to select field name from data Office 2019
Advanced Beginner
Listbox to select field name from data
 
Join Date: Nov 2020
Posts: 37
JamesWood is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
Do you mean like

Code:
Private Sub UserForm_Initialize()
Dim lngFld As Long
    With ActiveDocument.MailMerge.DataSource
        ListBox1.Clear
        For lngFld = 1 To .FieldNames.Count
            ListBox1.AddItem .FieldNames(lngFld)
        Next
    End With
End Sub


So I'm wondering how I can store one of the fields as a string. So basically on the listbox, I click on the desired field name, then when I click OK it stores that field name as a string. Any thoughts please? Thanks so much
Reply With Quote
  #5  
Old 05-25-2021, 08:21 AM
JamesWood JamesWood is offline Listbox to select field name from data Windows 10 Listbox to select field name from data Office 2019
Advanced Beginner
Listbox to select field name from data
 
Join Date: Nov 2020
Posts: 37
JamesWood is on a distinguished road
Default

Actually I managed to make a string from the selected listbox field


' Make a string from the selected listbox field
For var = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(var) = True Then
docNameField = docNameField & ListBox1.List(var) _
& vbCrLf
End If
Next

I'm not sure why I'm getting this error though:


"The requested member of the collection does not exist"


On this line of code:


strDocName = ActiveDocument.MailMerge.DataSource.DataFields(doc NameField).Value & ".pdf"


Thoughts? It's getting stuck at the docNameField part I think but I don't know why.
Reply With Quote
  #6  
Old 05-25-2021, 02:42 PM
JamesWood JamesWood is offline Listbox to select field name from data Windows 10 Listbox to select field name from data Office 2019
Advanced Beginner
Listbox to select field name from data
 
Join Date: Nov 2020
Posts: 37
JamesWood is on a distinguished road
Default

No worries I figured it out Thanks
Reply With Quote
Reply

Tags
listbox

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Listbox to select field name from data Yet another Multi-Select Listbox in Userform question Javir Word VBA 4 09-24-2019 01:01 AM
How to filter data before it is pulled into combobox/listbox AND remove data from listbox Izzii0x Excel Programming 8 05-24-2019 07:25 AM
Add-In creates ListBox from XLS, but data not saved in ppt barbet PowerPoint 0 05-26-2016 02:30 AM
How to use an ActiveX Control to insert a multi select listbox in Word marksm33 Word 2 01-29-2014 05:21 PM
Listbox to select field name from data Multi-select listbox help gvibe@hotmail.com Word VBA 1 07-19-2013 10:54 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:13 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft