0

我已经在我的 word 模板中创建了 DocVariables,并且正在使用 UserForm 来允许用户输入来填充这些变量。

这是我一直在使用的代码:

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

这确实将文本框中的值插入到变量中,但它不会更新字段,因此我必须手动转到每个字段并更新它。

你能告诉我哪里出错了,以便我可以解决这个问题。

任何和所有的帮助表示赞赏。

4

1 回答 1

1

尝试:

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
于 2021-04-11T12:59:25.370 回答