Document Editing Problems

Create Word Documents from UserForm Answers with VBA

Rana GarciaRana Garcia Nov 18, 2026 869 views

Question details

Create customized Word documents using answers entered in a VBA UserForm.

Create Word Documents from UserForm Answers with VBA
Product
Microsoft Word for Microsoft 365
Device & OS
not provided
Scenario
A user wants to insert text for a Yes answer and repeat content according to a number entered in a form.
Observed behavior
The desired document contains conditional and repeated content generated from the form answers.
Before you start

Open the desktop version of Word and enable the Developer tab in Word's ribbon settings if it is hidden. Check that your macro settings and organizational policies permit trusted VBA macros; Word for the web cannot run them.

Solution 1Recommended

Generate a document with a VBA UserForm

Use this approach to collect a Yes or No answer and a repeat count, then create a document containing the corresponding text.

This example creates a new blank document. You can replace the sample text with your own content or adapt the code to insert formatted AutoText or other Building Blocks stored in a Word template.

1
Save a macro-enabled file

In desktop Word, select File > Save As and save the file as a Word Macro-Enabled Document (.docm). The form and macro will be stored in this file.

2
Create the form

Select Developer > Visual Basic. In the VBA editor, select Insert > UserForm and set its Name property to UserForm1. Add two OptionButton controls named optYes and optNo, a TextBox named txtCount, and a CommandButton named cmdCreate. Set their captions or nearby labels so users know to choose Yes or No, enter a repeat count, and create the document.

3
Add the form code

Double-click the Create button on the form and add this code to the form's code window: Private Sub cmdCreate_Click() Dim countText As String Dim repeatCount As Long Dim i As Long Dim outputText As String Dim newDoc As Document If Not optYes.Value And Not optNo.Value Then MsgBox "Select Yes or No." Exit Sub End If countText = Trim(txtCount.Value) If Len(countText) = 0 Or Len(countText) > 3 Then MsgBox "Enter a whole number from 0 to 100." Exit Sub End If If Not IsNumeric(countText) Then MsgBox "Enter a whole number from 0 to 100." Exit Sub End If If CDbl(countText) < 0 Or CDbl(countText) > 100 Or CDbl(countText) <> Fix(CDbl(countText)) Then MsgBox "Enter a whole number from 0 to 100." Exit Sub End If repeatCount = CLng(countText) If optYes.Value Then outputText = "Text included for a Yes answer." & vbCr End If For i = 1 To repeatCount outputText = outputText & "Repeated content " & i & "." & vbCr Next i Set newDoc = Documents.Add newDoc.Content.Text = outputText Unload Me End Sub

4
Add a macro to open the form

In the VBA editor, select Insert > Module and add: Public Sub ShowForm() UserForm1.Show End Sub Save the macro-enabled file, return to Word, and select Developer > Macros. Choose ShowForm and select Run.

5
Generate and save the document

Choose Yes or No, enter a whole number from 0 to 100, and select the form's Create button. Review the new document, then use File > Save As to save it, for example as a .docx file.

Generate a document with a VBA UserForm
Keep the form file: The generated document contains the resulting text, not the form or macro. Keep the original .docm file to generate more documents.
Free Microsoft Office alternative

Edit generated Word documents with WPS Writer

WPS Office is a free, lightweight alternative with a familiar interface and support for common Word formats, making it easy to move existing documents into WPS Writer. Use desktop Word for the VBA UserForm workflow, then open and edit its generated documents in WPS.

Review and edit text generated from UserForm answers.Open and save Microsoft Word formats such as DOCX and DOC.Use a familiar document-editing interface.Get started with a free, lightweight office suite.
QA img-9

Frequently Asked Questions

Can I use VBA UserForms in Microsoft 365 Word?

Yes, desktop Word supports VBA UserForms. Open Developer > Visual Basic to create one, provided your macro settings and organizational policies allow it.

Can this form run in Word for the web?

No. Open the macro-enabled file in desktop Word to run the VBA form.

How can I insert formatted content instead of plain text?

Save reusable formatted content as AutoText or another Building Block in a Word template, then adapt the VBA code to insert that entry into the generated document.

Why can't I run the ShowForm macro?

Check that the file is saved as .docm, that ShowForm is in a standard module, and that your macro settings and organizational policies allow trusted macros. Then try Developer > Macros > ShowForm > Run.