用户窗体更新后删除书签范围内的先前文本 VBA Microsoft Word

Delete previous text in bookmark range after userform update VBA Microsoft Word

我正在制作 VBA Microsoft Word 模板。我构建了一个带有各种复选框、文本框和选项框的用户表单。我有一个程序,在填写用户表单后,用户可以再次打开用户表单以更改选项,并且新选项会替换以前的选项。以下代码涉及三个不同的复选框,并且根据 selected 框的组合,将出现一个句子。到目前为止一切正常,但我想弄清楚的是,如果用户 deselected 所有复选框,模板将用任何内容替换书签范围内的先前文本。下面是执行该过程的代码示例。

Dim Bookmark1 As Range
If Me.CheckBox1.Value And Me.CheckBox2 And Me.CheckBox3 = True Then
    Set Bookmark1 = ActiveDocument.Bookmarks("Bookmark1").Range
    Bookmark1.Text = "You have selected Apples, Oranges, and Bananas."
    ActiveDocument.Bookmarks.Add "Bookmark1", Bookmark1
ElseIf Me.CheckBox1.Value And Me.CheckBox2 = True Then
    Set Bookmark1 = ActiveDocument.Bookmarks("Bookmark1").Range
    Bookmark1.Text = "You have selected Apples and Bananas."
    ActiveDocument.Bookmarks.Add "Bookmark1", Bookmark1

直到显示每个组合,除非用户在每个复选框中都取消select。如果所有三个框都为假,我已经尝试制作一个 if 语句,然后 txt 等于“”

我也试过 equals null 但还是不行,非常感谢任何帮助。

当你阅读本文时,如果你碰巧知道很棒。 我想为在用户窗体中输入到文本框中的数据分配一个快捷键,以便在键入时用户可以简单地 select Ctrl Shift C 来输入光标在 word 文档中的文本框的值。任何帮助都将非常感谢!

我弄清楚了我在 If...Elseif 语句末尾做了什么,最后一个用来使前面的文本无效的代码被设置为 Elseif 而不是 Else

下面是有效的代码

Dim Bookmark1 As Range
If Me.CheckBox1.Value And Me.CheckBox2 And Me.CheckBox3 =.       True Then
Set Bookmark1 = ActiveDocument.Bookmarks("Bookmark1").Range
Bookmark1.Text = "You have selected Apples, Oranges, and Bananas."
ActiveDocument.Bookmarks.Add "Bookmark1", Bookmark1
ElseIf Me.CheckBox1.Value And Me.CheckBox2 = True Then
Set Bookmark1 = ActiveDocument.Bookmarks("Bookmark1").Range
Bookmark1.Text = "You have selected Apples and Bananas."
ActiveDocument.Bookmarks.Add "Bookmark1", Bookmark1
Else Set Bookmark1 = ActiveDocument.Bookmarks("Bookmark1").Range
Bookmark1.Text = ""