Microsoft Office Tutorials and References
In Depth Information
Filling a Word Combo Box with Data from Excel
End If¶
End If¶
End Sub¶
' * * * * *¶
Sub PutDataIntoDocument(frm As UserForm, doc As Word.Document)¶
'Variable declaration¶
Dim ctl As MSForms.Control¶
Dim rng As Word.Range¶
For Each ctl In frm.Controls¶
If doc.Bookmarks.Exists(ctl.Name) Then¶
Set rng = doc.Bookmarks(ctl.Name).Range¶
rng.Text = ctl.Text¶
doc.Bookmarks.Add Name:=ctl.Name, Range:=rng¶
End If¶
End Sub¶
' * * * * *¶
Sub FillStateList(cbo As MSForms.ComboBox)¶
'!Remember to set reference to ADO library!¶
'Variable declaration¶
Dim conn As ADODB.Connection¶
Dim rs As ADODB.Recordset¶
Dim SQL As String¶
Dim wbPath As String¶
Dim wbName As String¶
Dim sheetName As String¶
Dim fieldName As String¶
'The relevant information for the Excel file¶
wbName = "States.xls"¶
wbPath = "C:\test\" & wbName¶
sheetName = "StatesList"¶
fieldName = "StateAbbr" 'column heading¶
SQL = "Select [" & fieldName & _¶
"] FROM [" & sheetName & "$]"¶
Set conn = New ADODB.Connection¶
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _¶
"Data Source=" & wbPath & ";" & _¶
"Extended Properties=""Excel 8.0;HDR=Yes"""¶
Set rs = New ADODB.Recordset¶
rs.Open SQL, conn, , adCmdText¶
Do While Not rs.EOF¶
cbo.AddItem rs.Fields("StateAbbr").Value¶
'Free memory¶
Set rs = Nothing¶
Set conn = Nothing¶
End Sub¶
Search JabSto ::

Custom Search