PDA

View Full Version : User Form - Checkboxes to show/hide bookmarks



kristenb
01-18-2012, 06:45 PM
Hi,

I am just learning VBA and need some help. I am creating a userform for work. Form is to be protected.

I have three checkboxes A,B and C. If the user checks A then bookmark1 (which is a table full of drop downs and questions and text fields appear) if they choose checkbox B then bookmark2 appears and if C was selected a different bookmark/table would appear.

I need help writing the code. I also do not know if i should use a formfield checkbox or activex. I also dont know if it is easier to base it off a table or bookmark.

If someone could help me with the code, id be so grateful!

macropod
01-19-2012, 04:26 AM
Hi kristenb,

Your post mentions both a userform and form protection. These are two quite different things that typically don't relate to each other. Furthermore, your second paragraph suggests you're actually working with a document containing formfields, to which forms protection does apply. Assuming that's the case, your suggested approach is not going to work, as you can't hide formfields. Furthermore, even hiding text is problematic as the display and printing of hidden text is controlled by the user's configuration of Word, not by anything in your document.

Here's an example of something that will work. The following code is designed for an ‘On Exit’ macro attached to a checkbox formfield with the bookmark name "Check1". If the CheckBox is checked, a (previously) bookmarked range named "MyBkMrk" is populated with a string and two Formfields, 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).
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