【问题标题】:Using a UserForm in VBA to update DocVariables in Word在 VBA 中使用用户窗体来更新 Word 中的 DocVariables
【发布时间】:2021-04-11 20:59:25
【问题描述】:

我已经在我的 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

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

请告诉我哪里出了问题,以便我解决这个问题。

感谢所有帮助。

【问题讨论】:

标签: vba ms-word docvariable


【解决方案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

【讨论】:

    猜你喜欢
    • 2019-04-19
    • 2020-06-29
    • 1970-01-01
    • 1970-01-01
    • 2018-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-06
    相关资源
    最近更新 更多