0
votes

I have created DocVariables in my word template, and am using a UserForm to allow user input to populate these variables.

Here is the code I have been using:

Private Sub CommandButton1_Click()

Dim ReportTitle, reportSub As String

ReportTitle = Me.textBox1.Value
reportSub = Me.textBox2.Value

ActiveDocument.Variables("Report Title").Value = ReportTitle
ActiveDocument.Variables("Sub-Title").Value = reportSub

ActiveDocument.Fields.Update

Me.Repaint

End Sub

This does insert the values from the text boxes into the variables, but it does not update the fields, so I have to manually go to each field and update it.

Can you please tell me where I have gone wrong so that I can fix this issue.

Any and all help is appreciated.

1
You didn’t look very hard. A simple Bing/Google search would have given you the answer, which you can also find here: stackoverflow.com/questions/33733113/…Timothy Rylatt

1 Answers

1
votes

Try:

Private Sub CommandButton1_Click()
Application.ScreenUpdating = False
With ActiveDocument
  .Variables("Report Title").Value = Me.textBox1.Value
  .Variables("Sub-Title").Value = Me.TextBox2.Value
  .Fields.Update
  .PrintPreview
  .ClosePrintPreview
End With
Me.Repaint
Application.ScreenUpdating = True
End Sub