0
votes

I am working on a VBA Microsoft Word template. I built a userform with various checkboxes, textboxes, and optionboxes. I have a procedure where after filling out the userform the user can open the userform again to change an option and the new option replaces the previous option. The following code is involving three different checkboxes and depending on the selected combination of boxes a sentence will appear. So far everything is working properly but what I am trying to figure out is if the user deselected all of the checkboxes the template will replace the previous text in the bookmark range with nothing. Below is a sample of the code that goes through the procedure.

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

ect until every combination is displayed except if the user deselects every checkbox. I've tried the making an if statement if all three boxes are false then txt equals ""

I've also tried equals null and still did not work any help is greatly appreciated.

While you're reading this if you happen to know great. I want to assign a shortcut key to data entered into a textbox in the userform so that while typing a user can simply select Ctrl Shift C to enter the value of a textbox where their cursor is in the word document. Any help with this would be awesome thank you!

1

1 Answers

0
votes

I figured out what I did at the end of my If...Elseif statements the last code that was to nullify the previous text was set as Elseif instead of Else

Below is the code that worked

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 = ""