Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello VB Code people,
I'm having trouble finding the correct code that when I select or multi-select an item from a list box in a Word 2007 userform and then press a button. I want it to create a table in Word 2007 from the items that I've selected in the list box. The userform gets populated from Excel and it works fine. Below is the VB Code for populating the userform with Excel data. Any help is greatly appreciated. Thank you

VB
Private Sub Userform_Initialize()
Dim strOffice As String
Dim i As Long
Dim db As DAO.Database
Dim rs As DAO.Recordset
  strOffice = "Test"
  'Open the spreadsheet containing the data
  Set db = OpenDatabase("C:Testdata.xlsx", False, False, "Excel 12.0; IMEX=1;")
  'Retrieve the recordset
  Set rs = db.OpenRecordset("SELECT * FROM `Test`")
  'Set the number of Columns = number of Fields in recordset
  ListBox1.ColumnCount = rs.Fields.Count
  'Determine the number of retrieved records
  With rs
    .MoveLast
    i = .RecordCount
    .MoveFirst
  End With
  'Load the listbox with the retrieved records
  ListBox1.Column = rs.GetRows(i)
  'Cleanup
    
  rs.Close
  db.Close
  Set rs = Nothing
  Set db = Nothing
lbl_Exit:
  Exit Sub
    
End Sub
Posted

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900