我确实有一个用户形式的用户表单,该表单用值填充预定义的书签。我使用以下代码将文本写入书签:
Private Sub OKButton_Click()
Dim Text1 As Range
Set Text1 = ActiveDocument.Bookmarks("Text1").Range
Text1.Text = Me.ComboBox1.Value
当我再次按下OK按钮时,文本将添加到书签中(可以一遍又一遍地完成)。这应该是不可能的。如何解决?
通常,您会以关闭用户窗体的命令结束“确定”按钮子,这会阻止第二次单击:
Unload UserForm1
但是,如果您不想再次添加文本,则可以在书签中添加对现有文本的检查:
Private Sub OKButton_Click()
Dim Text1 As Range
Set Text1 = ActiveDocument.Bookmarks("Text1").Range
If Text1.Text <> Me.ComboBox1.value Then
Text1.Text = Me.ComboBox1.value
Else
MsgBox "That text has already been entered."
End If
End Sub