user3479553
user3479553

Reputation: 1

Populating a text box with some text depending on values of dropdowns selected in document

I am making a document in Microsoft Word 2016 and I would like this to be a form that a user will fill out (using Dropdown list content control)- from there I have assigned all of the items in the list with numbers for their values. I need to populate a text box with some words (determined by the sum of the values) and I am having trouble. Never used VBA- I don't even know if my first line is correct. I'm really not sure how to begin the document and how to populate the sum of all the dropdowns. I'm not using macros from another document, I just want to populate based upon what users select in the word document. I named my field with a tag on a text box that I want the text "account" "account 2" etc to appear in. Thanks!

Set myField = Selection.FormFields(1)
If myField.Type = wdFieldFormDropDown Then
 Num = myField.DropDown.ListEntries.Count
 If Num >= 75 Then
 myField.Value = "Account 1"
 End If
 If Num > 50 Then
 myField.Value = "Account 2"
 End If
 If Num <= 50 Then
 myField.Value = "Account 3"
 End If
 End If
End Sub

Upvotes: 0

Views: 4214

Answers (1)

Cindy Meister
Cindy Meister

Reputation: 25693

If you plan to use Content Controls then FormField object in your code is not correct. Form fields are "legacy" - they're still quite useful, but it's a different part of the object model.

Content Controls are "embedded" in the document and have events to trigger code. That means you need to double-click the ThisDocument entry in the Project for the parent document in the VB Editor. Then select "Document" from the dropdown to the left of the Code Window and "ContentControlOnExit" from the dropdown on the right. That will insert the event stub in the code window for ThisDocument (see picture).

enter image description here

A Content Control event will trigger for all content controls in the document, which means you need to distinguish which content control triggered it. The event passes in a ContentControl object which can be tested for this purpose, usually using Select Case.

The sample code shows how to do this, how to get the count of the ListEntries, how to select a specific content control by its title (or by its tag) and how to write content to the content control. (Since what you want to do with Num in your code, above, is not clear to me I don't try to include that. But it looks like here, as well, you could use Select Case.)

Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, _
                                          Cancel As Boolean)
    Dim cc As Word.ContentControl
    Dim doc As Word.Document
    Dim countListEntries As Long

    Set doc = ContentControl.Parent
    Select Case ContentControl.Title
        Case "SelectAccount"
            countListEntries = ContentControl.DropdownListEntries.Count
            Set cc = doc.SelectContentControlsByTitle("Account").Item(1)
            'Set cc =doc.SelectContentControlsByTag("Account").Item(1)
            cc.Range.Text = ContentControl.Range.Text & " " & countListEntries
        Case Else
    End Select
End Sub

Upvotes: 0

Related Questions