Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / February 2007

Tip: Looking for answers? Try searching our database.

Populate Dropdown Box in Word

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
No_Spam - 12 Feb 2007 19:32 GMT
I have a drop down in my word document. It's not a list, but a form
drop down (HTMLSelect) using the Web Tools toolbar. I want to
populate
this drop down with values from an Excel sheet. How do I do this? I'm
connecting to Excel, but unable to figure out how to populate the drop
down.
jille - 12 Feb 2007 19:59 GMT
So if you're already connecting to Excel (ie opened the Excel file as an
object) you should be able to do something like this:

yourcomboname.RowSource = "NamedRangeName"

The one I did was purely in Excel, using a named range to populate a
combobox, but the principle should work. Since the combobox is in your
document and not a userform, I would recommend positioning it in the autoopen
procedure so that when you open the document, the combobox auto populates.

Hope this helps!

> I have a drop down in my word document. It's not a list, but a form
> drop down (HTMLSelect) using the Web Tools toolbar. I want to
> populate
> this drop down with values from an Excel sheet. How do I do this? I'm
> connecting to Excel, but unable to figure out how to populate the drop
> down.
JodieM - 12 Feb 2007 23:06 GMT
Hi,
this piece of code works well for me, it's in the initialize section
of the form with the drop down field on it. I got it from somewhere
else, but I'm not sure where.

Private Sub UserForm_Initialize()
Dim i As Integer
Dim aResults()

Dim cn As ADODB.Connection
Dim rsT As New ADODB.Recordset
Dim intTblCnt As Integer, intTblFlds As Integer
Dim strTbl As String
Dim rsC As ADODB.Recordset
Dim intColCnt As Integer, intColFlds As Integer
Dim strCol As String
Dim t As Integer, c As Integer, f As Integer
Set cn = New ADODB.Connection
With cn
   .Provider = "Microsoft.Jet.OLEDB.4.0"
   .ConnectionString = "Data Source=\\server\path
\filename.xls;Extended Properties=Excel 8.0;"
   .CursorLocation = adUseClient
   .Open
End With
rsT.Open "Select * from YourNamedRange", cn, adOpenStatic

i = 0

  With rsT
  ' This code populates the combo box with the values
  ' in the YourNamedRange named range in the .xls file. this example
table is 2 rows by 6 columns and is set as a named range.

     Do Until .EOF
        cboDiv.AddItem (i)
        cboDiv.Column(0, i) = rsT.Fields(0).Value
        cboDiv.Column(1, i) = rsT.Fields(1).Value
        .MoveNext
        i = i + 1
     Loop

  End With
End Sub

Hope that helps
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.