问题
In Microsoft word, I have a 3x3 table, a button, and a drop down list.
When I press the button, I want the last row of the first column to be filled with the selected drop down list item. Then, add a row to the table below it.
Currently, I can't even fill values into the table.
ActiveDocument.Tables(15).Rows.Last.Cells.Value = "Hello"
returns an error. What can I do?
I managed to select the last row. ActiveDocument.Tables(15).Rows.Last.Select
Now, I need to copy the current value from the drop down list. How do I do that?
回答1:
Here is one way. It inserts the text after any existing text:
Option Explicit
Sub PopulateTable()
With ActiveDocument
.Tables(1).Rows.Last.Cells(1).Range.InsertAfter .FormFields("DropDown1").Result
End With
End Sub
If you know the specific cell location you could also use:
.Tables(1).Cell(3, 1).Range.InsertAfter .FormFields("DropDown1").Result
References:
1) table-cell-method-word
2) dropdown-object-word
You can loop so see which form fields are present in the Activedocument
and get their names as follows:
Sub GetNames()
Dim myField As FormField
For Each myField In ActiveDocument.FormFields
Debug.Print myField.Name
Next myField
End Sub
If you double click on the drop down form control it will open a window where you can view the Bookmark
name i.e. the current name for the drop down object.
You can also associate a macro via this route (so could remove need for a button potentially)
Legacy form control:
来源:https://stackoverflow.com/questions/48438385/user-selects-item-from-drop-down-box-clicks-button-and-the-selected-item-popul