Log in

View Full Version : Solved: Change Bookmark Text based on Checkbox



brent.fraser
12-06-2012, 10:33 AM
Hi,

I am working on a form that has checkboxed and I would like to update a bookmark based on the checkmark. I have looked at the following piece and it seems to do part of the trick but it's pretty complicated:

http://www.vbaexpress.com/forum/showthread.php?t=40569&highlight=change+bookmark+text+based+on+checkbox

Basically, it's for an evaluation form with 5 categories (checkboxes) to choose from. If the user checks the "unsatisfactory" checkbox, I would like to change the "custSat" bookmark to "unsatisfactory." When the checkbox is not selected, the bookmark is blank.

This is what I have pieced together so far (that's semi working):
Sub ShowHide()
Dim BmkNm As String, NewTxt As String, BmkRng As Range, FFld As FormField
BmkNm = "MyBkMrk"
NewTxt = "Unsatisfactory-dude"
Good = "We Be Good"
Application.ScreenUpdating = False
With ActiveDocument
If .Bookmarks.Exists(BmkNm) Then
.Unprotect
Set BmkRng = .Bookmarks(BmkNm).Range
If .FormFields("Check1").Result = 0 Then
UpdateBookmark "MyBkMrk", NewTxt
UpdateAllRefFields
ElseIf BmkRng.Text <> "" Then
.Protect Type:=wdAllowOnlyFormFields, noreset:=True
End
End If
'this part isn't working
If .FormFields("Check1").Result = 1 Then
UpdateBookmark "MyBkMrk", Good
UpdateAllRefFields
End If
.Protect Type:=wdAllowOnlyFormFields, noreset:=True
End If
End With
Application.ScreenUpdating = True
End Sub

Also, should I use the legacy checkboxes or the new Content Controls? I haven't used them before and I am not sure how it changes the VBA stuff.

Tanks everyone.....
B

gmaxey
12-06-2012, 11:07 AM
It doesn't work because it is it is never executed. The If statement preceding it either does something else with the formfield or terminates the procedure.

When you post code, post please post "all" relevant code (e.g., your write to bookmark code) that we might need to test and confirm our proposed solutions. This way we don't have to waste our time writing it.

Sub ShowHide()
Dim strGood As String, strNewTxt As String
strNewTxt = "Unsatisfactory-dude"
strGood = "We Be Good"
Application.ScreenUpdating = False
With ActiveDocument
If .Bookmarks.Exists("MyBkMrk") Then
If .ProtectionType <> wdNoProtection Then .Unprotect
If .FormFields("Check1").Result = 0 Then
UpDateBookmark "MyBkMrk", strNewTxt
'UpdateAllRefFields
Else
UpDateBookmark "MyBkMrk", strGood
'UpdateAllRefFields
End If
.Protect Type:=wdAllowOnlyFormFields, noreset:=True
End If
End With
Application.ScreenUpdating = True
End Sub
Function UpDateBookmark(strName As String, strVal As String)
Dim oRng As Word.Range
Set oRng = ActiveDocument.Bookmarks(strName).Range
oRng.Text = strVal
ActiveDocument.Bookmarks.Add strName, oRng
End Function

brent.fraser
12-06-2012, 11:14 AM
Thanks Greg.... it works like a charm!

Sorry I didn't include all the code. The code that wasn't included does indeed work but I can see how it could add time to your assistance.

Thank you so much again.

Hope all is well in N.C. My father-in-law is from there and a huge Tar Heels fan.

gmaxey
12-06-2012, 11:29 AM
Brent,
You're welcome. Take taxes off the table and all is indeed well in NC.

Best Regards,
Greg Maxey

The future doesn't belong to the fainthearted; it belongs to the brave.
~ Ronald Reagan

brent.fraser
12-06-2012, 03:29 PM
Hey Greg,

Taxes... yeah they are a mess everywhere huh? 27% here. I guess it could be worse right?

Thanks again for the help. I am making a change from check-boxes to "option buttons" since I have 5 categories to pick from.

The code you provided works for the checkboxes and I have converted them over to "option buttons" and I am getting a wierd message.

Here's the code:
Sub ShowHide()
Dim strDescription As String, strNewTxt As String
strNewTxt = "Unsatisfactory-dude"
strDescription = "We Be Good"
Application.ScreenUpdating = False
With ActiveDocument
If .Bookmarks.Exists("MyBkMrk") Then
If .ProtectionType <> wdNoProtection Then .Unprotect
If OptionButton1.Value = True Then
UpDateBookmark "MyBkMrk", strNewTxt
UpDateBookmark "description", strDescription
'UpdateAllRefFields
Else
UpDateBookmark "MyBkMrk", ""
UpDateBookmark "description", ""
'UpdateAllRefFields
End If
.Protect Type:=wdAllowOnlyFormFields, noreset:=True
End If
End With
Application.ScreenUpdating = True
End Sub
Function UpDateBookmark(strName As String, strVal As String)
Dim oRng As Word.Range
Set oRng = ActiveDocument.Bookmarks(strName).Range
oRng.Text = strVal
ActiveDocument.Bookmarks.Add strName, oRng
End Function


When I run it, I get a message in the VBA editor that states "The Protected method or property is not available because the current selection is locked for format changes." As soon as I click on "ok" the protection on the document is removed but the information in the bookmarks updates. Do you know why this is happening? I think it has something to do with "wdAllowOnlyFormFields."

Also, is there a way to update the bookmarks automatically after I select the "option button?"

Thanks again.

B.

fumei
12-06-2012, 08:35 PM
Selection locking is not the same as protected for formfields. It seems you have locked the selection. What evrsion are youi using. BTW, with the differences between version being fairly significant, it is important that version is mentioned when posting.

brent.fraser
12-07-2012, 07:45 AM
I guess that's why I am a newbie.... I don't know all of the ins and outs of VBA.

I am using Word 2010.

Thank you!