Skip to content

Word Form to Excel Table

Not everyone enters data directly into a spreadsheet. In many work environments, the source is often some kind of “form”. For example, a detailed user form might be filled out manually on a computer, then printed, then signed, and a hard copy retained for later use. Sometimes key information may then have to be taken from that form and re-entered into a computer, into some kind of “tabularised” database for subsequent analysis.

In this article we explain a solution that simplifies the process a little. Data in a User Form designed in Microsoft Word can, once the form is completed, be exported directly from the form (in its digital state) to the spreadsheet package Microsoft Excel (also in its digital state), without having to re-key it manually from the printed version of the form. The form itself can of course still be printed out for hard-copy record keeping as usual.

This particular solution has certain distinct benefits:

When the user has finished completing a form, they can send pre-assigned data fields to a separate spreadsheet, at the press of just a single button.

Keep data in precise addressable locations

Section titled “Keep data in precise addressable locations”

Relevant fields can be sent as an additional record to a precisely defined Excel Table, on a precisely defined worksheet, in a precisely defined file, so the user can choose exactly where they would like to store the data, giving them more control.

Keeping data housed in Tables has its own advantages:

  • It allows the data to be more easily exported to other tools that might be connected to it, such as Microsoft Excel’s own “Power Pivot” functionality
  • If you need to export the data to other software, this can be done by simply referring to the Table’s name
  • It also means that the table, sheet and file names only ever need to be specified once. This is particularly useful if data needs to be regularly sent or kept across several different datasets, on several separate worksheets, in a large workbook, which can very quickly become unwieldy to manage.

Here is an example of what a user form might look like, though you can design and customise one to suit your own needs.

Having completed the form, the next step is to specify the worksheet you wish to export the data to. After this, press the ‘Export to WorkBook’ button.

Pictures may also be included on the form.

After pressing the export button, you should see the data populated automatically in the Table on the worksheet that was specified earlier (in this case, the sheet called “CAR Register”).

Excel - Table

You can then print out the original Microsoft Word User Form and file it away, while the data is saved in a spreadsheet, where it can quickly be retrieved for later use.

Use the following VBA code to create a macro. Ensure you assign it to the Export button in the form. Ensure too that you set the correct file path to where the Excel file is stored.

Sub Export_Click()
'Define variables
Dim xlApp As New Excel.Application, xlWkBk As Excel.Workbook, xlWkSht As Excel.Worksheet
Dim xlTable As Excel.ListObject
Dim table_object_row As Excel.ListRow
Dim Sheet_name As String
'Set the Sheet Variable to the chosen sheet
Sheet_name = SelectContentControlsByTitle("txtSheet_Name")(1).Range.Text
With xlApp
'Hide our Excel session
.Visible = True 'False
'Open the workbook
Set xlWkBk = .Workbooks.Open("C:\Users\ABC\OneDrive\Desktop\VBA - Word Form to Excel Table (records).xlsx", AddToMRU:=False)
'Select the chosen sheet and select the first table in that sheet
Set xlWkSht = xlWkBk.Sheets(Sheet_name)
Set xlTable = xlWkSht.ListObjects(1)
'Add an extra row to the chosen Table
Set table_object_row = xlTable.ListRows.Add
'Display Record No. being added in a Message Box
MsgBox ("Record #: " & xlTable.ListRows.Count & " being added" & " to Sheet: " & Sheet_name)
'Update the table in the first table in the chosen sheet in the WorkBook
With ActiveDocument
table_object_row.Range(1, 1).Value = .SelectContentControlsByTitle("txtCARNumber")(1).Range.Text
table_object_row.Range(1, 2).Value = .SelectContentControlsByTitle("txtCategory")(1).Range.Text
table_object_row.Range(1, 3).Value = .SelectContentControlsByTitle("txtDepartment")(1).Range.Text
table_object_row.Range(1, 4).Value = .SelectContentControlsByTitle("txtLocation")(1).Range.Text
table_object_row.Range(1, 5).Value = .SelectContentControlsByTitle("txtInitiator")(1).Range.Text
table_object_row.Range(1, 6).Value = .SelectContentControlsByTitle("txtResponsible")(1).Range.Text
table_object_row.Range(1, 7).Value = .SelectContentControlsByTitle("txtDateRaised")(1).Range.Text
table_object_row.Range(1, 8).Value = .SelectContentControlsByTitle("txtDetails")(1).Range.Text
table_object_row.Range(1, 9).Value = .SelectContentControlsByTitle("txtPriority")(1).Range.Text
table_object_row.Range(1, 10).Value = .SelectContentControlsByTitle("txtDateComplete")(1).Range.Text
table_object_row.Range(1, 11).Value = .SelectContentControlsByTitle("txtDateVerified")(1).Range.Text
End With
'Save & Close the Excel workbook
xlWkBk.Close SaveChanges:=True
.Quit
End With
'Release object memory
Set xlWkSht = Nothing: Set xlWkBk = Nothing: Set xlApp = Nothing
'Tell the user that the record has been added
MsgBox "Record added to WorkBook.", vbOKOnly
End Sub
Private Sub CommandButton1_Click()
Call Export_Click
End Sub

For the solution to work, ensure that you have the following checked under References in Microsoft Word, especially ‘Microsoft Excel 16.0 Object Library’ or later. To do this, open the VBA Editor, then choose the Tools menu, then ‘References’ and select the correct library. You need to do this in the VBA editor in Microsoft Word (not Microsoft Excel).

That’s it. After activating the above, the form should now work.

The full files can be downloaded here:

VBA - Word Form to Excel Table (docm)

VBA - Word Form to Excel Table (records)