Add items to dropdown list in VBA after adding dropdown in Word

noclew
noclew used Ask the Experts™
on
Hi Experts.  I currently have code that adds a line to a table in Word, then adds formfields to that line as well as a date picker.  The last column in the new line is a dropdown field.  I can add the dropdown field.  However, I'm having trouble then changing the name of the newly added dropdown and adding items to the dropdown list.  Any assistance would be greatly appreciated.  I've attached my current code for review.

Many thanks
Sub AddRowtoObstacleTable()
    Dim tblNew As Table
    Dim rowNew As Row
    Dim objCC As ContentControl
    
    ActiveDocument.Unprotect ("casemanagement")
If ActiveDocument.Range(0, Selection.Tables(1).Range.End).Tables.Count = 4 Then
    
    Set tblNew = ActiveDocument.Tables(4)
    Set rowNew = tblNew.Rows.Add
   
    
    Selection.Collapse (wdCollapseStart) 'Put the cursor in the first cell of the new row
     CurRow = Selection.Information(wdStartOfRangeRowNumber) 'Read the number of the new row
 
     For i = 1 To 5
        tblNew.Cell(tblNew.Rows.Count, i).Select 'Select the next cell for processing
        If i < 4 Then
            Selection.FormFields.Add Range:=Selection.Range, _
            Type:=wdFieldFormTextInput 'And add a form field
        ElseIf i = 4 Then
            Set objCC = ActiveDocument.ContentControls.Add(wdContentControlDate)
            objCC.DateDisplayFormat = "dd-MMM-yy"
            objCC.SetPlaceholderText , , "Click Here"
        Else
            Dim choice
            choice = "Choose"
            Set ffield = Selection.FormFields.Add(Range:=Selection.Range, Type:=wdFieldFormDropDown)
            With ffield.DropDown.ListEntries
                .Add Name:=choice
            End With
            MsgBox (ffield.DropDown.ListEntries.Count)
        End If
          fCount = ActiveDocument.Range.FormFields.Count
          With ActiveDocument.FormFields(fCount)
               .Name = "col" & i & "row" & CurRow 'Add a unique bookmark name
               .Enabled = True 'Enable the field for user entry
                    .Enabled = False
                    .TextInput.EditType Type:=wdCalculationText, _
                    Default:="=Col1Row" & CurRow _
                    & " + Col2Row" & CurRow _
                    & " + Col3Row" & CurRow _
                    & " + Col4Row" & CurRow _
                    & " + Col5Row" & CurRow, _
                    Format:=""
          End With
     Next i
    tblNew.Rows.Last.Select
ElseIf ActiveDocument.Range(0, Selection.Tables(1).Range.End).Tables.Count = 2 Then
    Set tblNew = ActiveDocument.Tables(2)
    Set rowNew = tblNew.Rows.Add
       
    Selection.Collapse (wdCollapseStart) 'Put the cursor in the first cell of the new row
     CurRow = Selection.Information(wdStartOfRangeRowNumber) 'Read the number of the new row
 
     For i = 1 To 2
        tblNew.Cell(tblNew.Rows.Count, i).Select 'Select the next cell for processing
        Selection.FormFields.Add Range:=Selection.Range, _
        Type:=wdFieldFormTextInput 'And add a form field
        fCount = ActiveDocument.Range.FormFields.Count
        
          With ActiveDocument.FormFields(fCount)
               .Name = "col" & i & "row" & CurRow 'Add a unique bookmark name
               .Enabled = True 'Enable the field for user entry
               .Enabled = False
               .TextInput.EditType Type:=wdCalculationText, _
               Default:="=Col1Row" & CurRow _
               & " + Col2Row" & CurRow, _
               Format:=""
          End With
     Next i
     tblNew.Rows.Last.Select
Else
    MsgBox ("Your cursor must be in the Obstacles Table or in the Meeting Participants Table." _
            & Chr(13) & "Please place your cursor in one of those two tables and click the button again")
End If
 ActiveDocument.Protect wdAllowOnlyFormFields, True, "casemanagement"
End Sub
Sub RemoveRowObstacleTable()
    Dim tblNew As Table
    Dim rowDel As Row
    Dim LastRow
    
    ActiveDocument.Unprotect ("casemanagement")
    
If ActiveDocument.Range(0, Selection.Tables(1).Range.End).Tables.Count = 4 Then
    Set tblNew = ActiveDocument.Tables(4)
    LastRow = tblNew.Rows.Count
    Set rowDel = tblNew.Rows(LastRow)
    rowDel.Delete
    tblNew.Rows.Last.Select
ElseIf ActiveDocument.Range(0, Selection.Tables(1).Range.End).Tables.Count = 2 Then
    Set tblNew = ActiveDocument.Tables(2)
    LastRow = tblNew.Rows.Count
    Set rowDel = tblNew.Rows(LastRow)
    rowDel.Delete
    tblNew.Rows.Last.Select
Else
    MsgBox ("Your cursor must be in the Obstacles Table or in the Meeting Participants Table." _
    & Chr(13) & "Please place your cursor in one of those two tables and click the button again")
End If
 
ActiveDocument.Protect wdAllowOnlyFormFields, True, "casemanagement"
End Sub

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Manager, Document Services Center
Top Expert 2004
Commented:
I don't usually use these but the best I can come up with to add items is:
    Selection.FormFields("Dropdown1").DropDown.ListEntries.Add Name:="Item 5"
    Selection.FormFields("Dropdown1").DropDown.ListEntries.Add Name:="Item 4"
    Selection.FormFields("Dropdown1").DropDown.ListEntries.Add Name:="Item 3"
    Selection.FormFields("Dropdown1").DropDown.ListEntries.Add Name:="Item 2"
    Selection.FormFields("Dropdown1").DropDown.ListEntries.Add Name:="Item 1"

Open in new window

Joanne M. OrzechManager, Document Services Center
Top Expert 2004

Commented:
Thanks noclew...

Joanne

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial