?
Solved

How to Reference Combo Box by Name in Word VBA

Posted on 2009-04-23
3
Medium Priority
?
1,891 Views
Last Modified: 2012-05-06
I want to use code like the below (modified from http://support.microsoft.com/kb/253542), but I don't want to hard code the name of the Combo box. How to reference it by name? ActiveDocument.FormFields(cmbName) is not working for me...
Function PopulateTestComboBox(cmbName As String)
    Dim dbDatabase As Database
    Dim rst As Recordset
    Dim i As Integer
    
    Set dbDatabase = OpenDatabase(strDBPath)
    Set rst = dbDatabase.OpenRecordset("tbl_Tests", dbOpenSnapshot)
     
    i = 0
    Do Until rst.EOF
        ActiveDocument.FormFields(cmbName).AddItem (i)
        ActiveDocument.FormFields(cmbName).Column(0, i) = rst.Fields("TestName")
        'ComboBox1.AddItem (i)
        'ComboBox1.Column(0, i) = rst.Fields("TestName")
        rst.MoveNext
        i = i + 1
    Loop
End Function

Open in new window

0
Comment
Question by:Michael Vasilevsky
  • 2
3 Comments
 
LVL 12

Accepted Solution

by:
geneus earned 2000 total points
ID: 24220112
The question is, what type of ComboBox are you using.  The ComboBox from Word 2007 is not a Formfield but a Content Control.

Since you do not want to reference it by name:

For x = 1 To ActiveDocument.ContentControls.Count
   
   If ActiveDocument.ContentControls(x).Type = 3 Then
     ActiveDocument.ContentControls(x).Range.Select
   End If
   
   MsgBox "Found ComboBox"
   
Next x
Sub SetPlaceholderText()
 
    Dim objCC As ContentControl
    
    Set objCC = Selection.ContentControls _
        .Add(wdContentControlComboBox)
    objCC.Title = "Favorite Animal"
    objCC.SetPlaceholderText _
        Text:="Please select your favorite animal "
    
    'List entries
    objCC.DropdownListEntries.Add "Cat"
    objCC.DropdownListEntries.Add "Dog"
    objCC.DropdownListEntries.Add "Horse"
    objCC.DropdownListEntries.Add "Monkey"
    objCC.DropdownListEntries.Add "Snake"
    objCC.DropdownListEntries.Add "Other"
 
End Sub

Open in new window

0
 
LVL 11

Author Comment

by:Michael Vasilevsky
ID: 24226653
I do want to reference it by name, but dynamically (not hardcoded). Something like the below. But I get an "Invalid Parameter" error at the line: ActiveDocument.ContentControls("ComboBox11").Range.Select
Function PopulateTestComboBox(cmbName As String)
    Dim dbDatabase As Database
    Dim rst As Recordset
    Dim i As Integer
    Dim objCC As ContentControl
    
    strDBPath = "C:\MyPath\MyDB.mdb"
 
    Set dbDatabase = OpenDatabase(strDBPath)
    Set rst = dbDatabase.OpenRecordset("SELECT TestName FROM tbl_Tests ORDER BY TestName", dbOpenSnapshot)
    ActiveDocument.ContentControls("ComboBox11").Range.Select
    Set objCC = Selection.ContentControls _
                    .Add(wdContentControlComboBox)
 
    i = 0
    With objCC
        'Add initial blank value
        .DropdownListEntries.Add ("")
        i = i + 1
        Do Until rst.EOF
            .DropdownListEntries.Add (rst.Fields("TestName"))
            rst.MoveNext
            i = i + 1
        Loop
        'Add final value "Other"
        .DropdownListEntries.Add ("Other")
        'Set to initial blank value
        .SetPlaceholderText Text:=""
    End With
End Function

Open in new window

0
 
LVL 11

Author Comment

by:Michael Vasilevsky
ID: 24486056
This is the way to do it:

ActiveDocument.FormFields(GetControlName).Result = ComboBox1.Value

Function GetControlName() As String
    Set rng = Selection.Range
    Do Until rng.FormFields.Count > 0
        rng.MoveStart wdCharacter, -1
    Loop
   
    strControl = rng.FormFields(1).Name

    GetControlName = strControl
End Function
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

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

This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
Suggested Courses

616 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