How can I create a userform that populates a table in Word 2016?

Anonymous
2018-04-30T21:00:14+00:00

I have created a userform using Visual Basic in Excel that was able to insert rows of data into a worksheet. I have a client who would like me to do this for him but wants his table to remain in Word. Does anyone know how to do this?

Here is a snapshot of the user form that I made in Excel:

Here is the Visual Basic Code behind it:

Private Sub Close_Button_Click()

    'Close UserForm.

    Unload Me

End Sub

Private Sub Save_Entry_Click()

    'Copy input values to sheet.

    Dim lRow As Long

    Dim ws As Worksheet

    Set ws = Worksheets("Names&Addresses")

    lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row

    With ws

        .Cells(lRow, 1).Value = Me.NoOfPeople.Value

        .Cells(lRow, 2).Value = Me.First_Name.Value

        .Cells(lRow, 3).Value = Me.Last_Name.Value

        .Cells(lRow, 4).Value = Me.Final_Format.Value

        .Cells(lRow, 5).Value = Me.Address.Value

        .Cells(lRow, 6).Value = Me.City.Value

        .Cells(lRow, 7).Value = Me.State.Value

        .Cells(lRow, 8).Value = Me.Zipcode.Value

    End With

    'Clear input controls.

    Me.NoOfPeople.Value = ""

    Me.First_Name.Value = ""

    Me.Last_Name.Value = ""

    Me.Final_Format.Value = ""

    Me.Address.Value = ""

    Me.City.Value = ""

    Me.State.Value = ""

    Me.Zipcode.Value = ""

End Sub

Private Sub State_DropButtonClick()

    Me.State.AddItem "AL"

    Me.State.AddItem "AK"

    Me.State.AddItem "AZ"

    Me.State.AddItem "AR"

    Me.State.AddItem "CA"

    Me.State.AddItem "CO"

    Me.State.AddItem "CT"

    Me.State.AddItem "DE"

    Me.State.AddItem "FL"

    Me.State.AddItem "GA"

    Me.State.AddItem "HI"

    Me.State.AddItem "ID"

    Me.State.AddItem "IL"

    Me.State.AddItem "IN"

    Me.State.AddItem "IA"

    Me.State.AddItem "KS"

    Me.State.AddItem "KY"

    Me.State.AddItem "LA"

    Me.State.AddItem "ME"

    Me.State.AddItem "MD"

    Me.State.AddItem "MA"

    Me.State.AddItem "MI"

    Me.State.AddItem "MN"

    Me.State.AddItem "MS"

    Me.State.AddItem "MO"

    Me.State.AddItem "MT"

    Me.State.AddItem "NE"

    Me.State.AddItem "NV"

    Me.State.AddItem "NH"

    Me.State.AddItem "NJ"

    Me.State.AddItem "NM"

    Me.State.AddItem "NY"

    Me.State.AddItem "NC"

    Me.State.AddItem "ND"

    Me.State.AddItem "OH"

    Me.State.AddItem "OK"

    Me.State.AddItem "OR"

    Me.State.AddItem "PA"

    Me.State.AddItem "RI"

    Me.State.AddItem "SC"

    Me.State.AddItem "SD"

    Me.State.AddItem "TN"

    Me.State.AddItem "TX"

    Me.State.AddItem "UT"

    Me.State.AddItem "VT"

    Me.State.AddItem "VA"

    Me.State.AddItem "WA"

    Me.State.AddItem "WV"

    Me.State.AddItem "WI"

    Me.State.AddItem "WY"

End Sub

Here is the macro that opens the userform:

Sub ShowContactUF()

    'Display Animals UserForm.

    Contacts.Show Modal

End Sub

And finally here are the errors that I am getting when I use an adjusted form in Word:

Any help will be greatly appreciated! Thanks in advance!

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} votes
Answer accepted by question author
  1. Jay Freedman 206K Reputation points Volunteer Moderator
    2018-05-01T02:41:13+00:00

    The immediate cause of that error is that the Worksheet object type is specific to Excel and doesn't exist in Word. There's a lot more to this, though, because Word doesn't work much like Excel in other respects.

    The big one is that while rows, columns, and cells always exist in Excel, they don't exist in a Word document unless the document contains a table. The userform could put data into cells of an existing table, or it could insert a new table (during which the code would have to know how many columns to create). Most likely, the code should add a new row to the table for each press of the Save Entry button, because you won't know in advance how many rows there will be.

    Here's a version of the Save_Entry_Click procedure that runs in Word. It assumes that there is already a table in the current document, presumably inherited from the template on which the document is based. It further assumes that table is the first (or only) one in the document, and that it has at least 8 columns (probably with the column headings already in place).

    Private Sub Save_Entry_Click()

        'Copy input values to table in document.

        Dim lRow As Long

        Dim tbl As Table

        Set tbl = ActiveDocument.Tables(1)

        tbl.Rows.Add   ' defaults to adding one row to bottom of table

        lRow = tbl.Rows.Count

        With tbl.Rows(lRow)

            .Cells(1).Range.Text = Me.NoOfPeople.Value

            .Cells(2).Range.Text = Me.First_Name.Value

            .Cells(3).Range.Text = Me.Last_Name.Value

            .Cells(4).Range.Text = Me.Final_Format.Value

            .Cells(5).Range.Text = Me.Address.Value

            .Cells(6).Range.Text = Me.City.Value

            .Cells(7).Range.Text = Me.State.Value

            .Cells(8).Range.Text = Me.Zipcode.Value

        End With

        'Clear input controls.

        Me.NoOfPeople.Value = ""

        Me.First_Name.Value = ""

        Me.Last_Name.Value = ""

        Me.Final_Format.Value = ""

        Me.Address.Value = ""

        Me.City.Value = ""

        Me.State.Clear

        Me.State.Value = ""

        Me.Zipcode.Value = ""

    End Sub

    Note the addition of the statement Me.State.Clear near the end. It removes all the entries from the dropdown list. This should be in the Excel version, too; otherwise, each time the user clicks the down arrow of the State dropdown, the code will add a complete new set of abbreviations to the existing list.

    While this code does what it says on the label, it isn't complete for practical use. It needs error handling. As a simple example, if the user clicks the Save Entry button when all the fields are blank, the code will happily add a blank row to the table. If for any reason the document doesn't contain any tables, or if the first table in the document doesn't contain at least 8 columns, the code will throw an error (and the default error message won't mean anything to the user). There's nothing to check that the text in the State field is one of the abbreviations in the list, or that the Zipcode entry is a valid set of digits.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2018-05-01T15:54:18+00:00

    Dear Jay Freedman,

                  Thank you so much for your help! The userform works great now, and I think that my client will be very pleased.

                                        -- Christopher Hastings

    0 comments No comments