Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Create dynamic list box entries on a userform for data in Excel 2007 and create word doc from it.

Posted on 2009-07-10
6
Medium Priority
?
498 Views
Last Modified: 2012-06-27
Hi,

I have an Excel 2007 spreadsheet with two columns of data. Column A has a letter type and Column B has a letter fragment.

What I would like to do is create a userform , in excel 2007,  which has a combox box with various letter types in it and a listbox . If you select a particular type of letter e.g. DV1 from the combobox the listbox brings up the relevant letter fragment options which you can then tick. A letter is then produced, using vba, from the options which you have selected.

Attached is a sheet with an example of the data.

Any help appreciated

Regards..
Fragment.xlsx
0
Comment
Question by:bhattar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 24824170
Why would you begin this process in Excel when you want to create a Word letter?  Wouldn't it just be easier to create the userform in Word?
0
 

Author Comment

by:bhattar
ID: 24824253
Hi JOrzech,

The excel sheet contains all of my fragments and that is the source file. There are a large number of these and just thought it would be cleaner to keep everything in one place and dynaically create the word doc when required.

However I am open to suggestions.

Regards..

0
 
LVL 19

Accepted Solution

by:
david251 earned 2000 total points
ID: 24824322
Here is a simple example using your data to accomplish this.

Using your data in the above sample
-Add a reference to Word in VBA
-Create a userform
-Create a Listbox nambed ListBox1
-Create a Combobox nambed ComboBox1
-Create a button named CommandButton1
-Switch to code view and paste the following code.

I hope this helps
-David251
Dim arr
 
Private Sub CommandButton1_Click()
    Dim wdApp As New Word.Application
    Dim wdDoc As Word.Document
    Set wdDoc = wdApp.Documents.Add
    
    Dim lItem As Long
 
    For lItem = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(lItem) = True Then
            wdDoc.Content.InsertAfter ListBox1.List(lItem)
            wdDoc.Content.InsertParagraphAfter
 
        End If
    Next lItem
    wdApp.Visible = True
    Set wdDoc = Nothing
    Set wdApp = Nothing
End Sub
 
Private Sub UserForm_Initialize()
    
    arr = Range("a1:b21").Value
    'Create ComboBox1 Entries
    Dim strTemp As String
    strTemp = "|"
    For i = 2 To UBound(arr, 1)
        If InStr(strTemp, "|" & arr(i, 1) & "|") = 0 Then
            strTemp = strTemp & arr(i, 1) & "|"
            Me.ComboBox1.AddItem arr(i, 1)
        End If
    Next i
End Sub
 
 
Private Sub ComboBox1_Change()
    'Update ListBox1
    Me.ListBox1.Clear
    For i = 2 To UBound(arr, 1)
        If arr(i, 1) = Me.ComboBox1.Value Then Me.ListBox1.AddItem arr(i, 2)
    Next i
End Sub

Open in new window

0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 19

Expert Comment

by:david251
ID: 24824348
BTW, ListBox1 needs to allow multiselect.

You might want to modify the code to check if the list box has anything selected before creating the word file.
0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 24824753
LOL - I only know how to code Word so I would imagine that if you want to create a Word letter, you would start the process in Word.  You can always copy the Excel columns and paste into Word (which automatically creates a Word data source), but it looks like you have a viable solution posted by David251....

Just let me know if I can be of any assistance in this process regarding the Word end of it :)

Joanne Orzech
MS Word MVP
EE Zone Advisor
0
 

Author Closing Comment

by:bhattar
ID: 31602059
david251,

Sorry for not getting back to you sooner! Great soultion works a treat!

Regards..
0

Featured Post

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
Outlook for dependable use in a very small business   This article is about using the Outlook application (part of Microsoft Office) in a very small business, or for homeowners where dependability and reliability are critical requirements. This …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question