# [SOLVED] Word Macros either/or code



## CodingBlind (Oct 14, 2011)

I am VERY new to macros, and pretty much starting from scratch.

I have a form with a numbered list of questions. Ideally I would like that if the user selects a checkbox in line 1, then line two will be disabled, deleted, and/or greyed out, or vice versa (check box 2 and line 1 disappears).

There's a total of about 10 lines where the user would select either the top line or the bottom line, and the unselected line would disappear. If anyone can help me with the coding for the first either/or situation, I'm sure I can edit it for the following situations.

I do have each line numbered (1-10) and each checkbox has a unique bookmark name, if that helps.

Thanks in advance.


----------



## macropod (Apr 11, 2008)

*Re: Word Macros either/or code*

What happens if the user makes a mistake?


----------



## CodingBlind (Oct 14, 2011)

*Re: Word Macros either/or code*

If the user selects the check box in line 1, then line 2 (line 2 check box included) would be disabled/greyed out/disappear. If the user decides he or she has made a mistake and would like to select line 2, he would first need to unselect the check box for line 1. By unselected line 1, line 2 would then be enabled/reappear.

I basically need a code that says "if this line is selected, then disable the other line."

This is the basic concept I'm looking for. If there's another way of achieveing a similar result, I'm open to all ideas. Thanks for the help.


----------



## macropod (Apr 11, 2008)

*Re: Word Macros either/or code*

Here's an approach you might take:
Create a document with a bookmarked range named "MyBkMrk" and a checkbox Formfield with the bookmark name "Check1". Then add the following code to a normal code module attached to the document:

```
Sub ShowHide()
Dim BmkNm As String, NewTxt As String, BmkRng As Range, FFld As Formfield
BmkNm = "MyBkMrk"
NewTxt = "The pay rate is || per hour for ** hours."
Application.ScreenUpdating = False
With ActiveDocument
  If .Bookmarks.Exists(BmkNm) Then
    .Unprotect
    Set BmkRng = .Bookmarks(BmkNm).Range
    If .Formfields("Check1").Result = 0 Then
      NewTxt = ""
      For Each FFld In BmkRng.Formfields
        FFld.Delete
      Next
    ElseIf BmkRng.Text <> "" Then
      .Protect Type:=wdAllowOnlyFormfields, noreset:=True
      End
    End If
    BmkRng.Text = NewTxt
    .Bookmarks.Add BmkNm, BmkRng
    If NewTxt <> "" Then
      BmkRng.Select
      With Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "||"
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute
      End With
      Set FFld = ActiveDocument.Formfields _
        .Add(Range:=Selection.Range, Type:=wdFieldFormTextInput)
      With FFld
        .Name = "Text98"
        .TextInput.EditType Type:=wdNumberText, Default:="", Format:="$#,##0.00"
        .TextInput.Width = 8
        .Result = "$0.00"
      End With
      BmkRng.Select
      With Selection.Find
        .Text = "**"
        .Execute
      End With
      Set FFld = ActiveDocument.Formfields _
        .Add(Range:=Selection.Range, Type:=wdFieldFormTextInput)
      With FFld
        .Name = "Text99"
        .TextInput.EditType Type:=wdNumberText, Default:="", Format:="0"
        .TextInput.Width = 2
        .Result = "0"
      End With
      .Bookmarks("Text98").Range.Select
    End If
    .Protect Type:=wdAllowOnlyFormfields, noreset:=True
  End If
End With
Set BmkRng = Nothing
Application.ScreenUpdating = True
End Sub
```
Once you've done that, go back to the formfield and choose the ‘On Exit’ macro option and select the 'ShowHide' macro. Finally, protect the document for forms.

Once you've done that, if the CheckBox is checked, the bookmarked range named "MyBkMrk" is populated with a string "The pay rate is $$ per hour for XX hours" and two Formfields (indicated by the $$ and XX), the first of which is selected; otherwise the bookmarked range is cleared. If the user tabs through the already-checked CheckBox, the bookmarked range is preserved (along with the Formfield contents).


----------



## AlbertMC2 (Jul 15, 2010)

*Re: Word Macros either/or code*

Hi

I am no genius in VBA for MS Word but I would just use option buttons and text boxes.
The option buttons will only allow one choice to be made and then depending on the choice just disable the other text box.

In Word 2007 you can use something like:

```
Private Sub OptionButton1_Click()

    TextBox1.Enabled = True
    TextBox2.Enabled = False
    TextBox1.Select

End Sub
-------------------------------------------------------
Private Sub OptionButton2_Click()

    TextBox2.Enabled = True
    TextBox1.Enabled = False
    TextBox2.Select

End Sub
```
Where OptionButton1 and OptionButton2 are the Option Buttons in the same group / question
And TextBox1 and TextBox2 are the corresponding text boxes.


I do not know if this works on versions prior to Word 2007.


----------



## CodingBlind (Oct 14, 2011)

*Re: Word Macros either/or code*

Thanks, but I'm not having too much success. Here is my exact, step-by-step process.

Open Word
Created a legacy check box form field
Open the properties window to ensure the bookmark name is "Check1"
Close properties window
I typed in the following line dbl spaced after the check box "Line 1 should change when the box to the left is selected"
I highlighted everything from "Line 1" to "left is selected"
Went to the Insert tab, and opened the Bookmark window
Named the selection "MyBkMrk" and clicked Add
Open the Macros window in the Developer tab
Typed the name "ShowHide" and then clicked Create
Copied and pasted everything in your code from "Sub ShowHide()" to "End Sub"
Clicked the Save icon and closed the Macros window
Protected the document

Yet when I click the check box, nothing happens. I have tried it with the "Calculate on Exit" option checked and unchecked in Check1's properties, but get no response either way.

Thank you again for your help, and I'm sorry for causing such frustration.


----------



## macropod (Apr 11, 2008)

*Re: Word Macros either/or code*



AlbertMC2 said:


> Hi
> 
> I am no genius in VBA for MS Word but I would just use option buttons and text boxes.


Code like that is for a userform, and won't work with formfields.


----------



## macropod (Apr 11, 2008)

*Re: Word Macros either/or code*

Hi CodingBlind,

I'd suggest pressing Alt-F11 to activate the visual basic editor and checking that the code is actually in your form document (and not in 'Normal.dot', for example) and that it replicates what I posted.

As for how it should work, do note that, if you bookmark the "Line 1 ... is selected" text, that text will get deleted when you tab out of the formfield. As per the description I gave, the formfield adds some text & formfields if the checkbox is checked. This is different to what you envisaged, but is the only reliable way of giving the user the ability to recover from an erroneous choice. You could, of course, have some default text (such as "Line 1 ... is selected") and include that in the macro as well, for 'recovery' purposes.

As to nothing happening, from what you've described, it's not apparent you've actually exited the formfield after checking it (eg by pressing <Tab>). Without that last step, the 'On exit' event won't be triggered.


----------



## CodingBlind (Oct 14, 2011)

*Re: Word Macros either/or code*

You got it (silly mistake), the macro was Normal.dot. Once I put it into the actual document, it worked wonderfully. Thank you.


----------

