Log in

View Full Version : Solved: Update Text Content Control Automatically with Option Button Control



brent.fraser
12-10-2012, 10:22 AM
Hi all,

I am using Word 2010 and I am creating a form with option buttons for a rating scale. When the user selects one of the option (radio) buttons, I have text content controls where a description of the criteria of the button entry is displayed. For example, if "unsatisfactory" is selected, I have text that defines what unsatisfactory is.

Everything is working well but when I select the option button, the text doesn't automatically update. I think using the "ContentControlOnExit" is the way to go here.

Am I pointing in the right direction?

Here's the code I have so far:

Private Sub Document_New()
frmNewEmployee.Show
End Sub
Private Sub SetCCValue(sCCTitle As String, sValue As String)
Dim cc As ContentControl
For Each cc In ThisDocument.ContentControls
If cc.Title = sCCTitle Then
cc.Range.Text = sValue
End If
Next
End Sub
'This is the area where we configure the option buttons and the text associated with it.
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 ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:=""
End If
If Unsatisfactory.Value = True Then
UpdateBookmark "MyBkMrk", strNewTxt
UpdateBookmark "description", strDescription
'UpdateAllRefFields
SetCCValue "description2", strDescription
Else
UpdateBookmark "MyBkMrk", ""
UpdateBookmark "description", ""
SetCCValue "description2", ""
'UpdateAllRefFields
End If
If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
End If
End If
End With
Application.ScreenUpdating = True
End Sub
Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As String)
Dim BMRange As Range
Set BMRange = ActiveDocument.Bookmarks(BookmarkToUpdate).Range
BMRange.Text = TextToUse
ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
End Sub
Public Sub UpdateAllRefFields() 'this updates all the cross-references to the document
Dim lngJunk As Long
Dim fldItem As Word.Field
Dim rngStory As Word.Range
' Word missing first Header/Footer bug workaround
lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType
' Iterate through all story types in the current document
For Each rngStory In ActiveDocument.StoryRanges
' Iterate through all linked stories
Do
' Only Update the Ref fields
For Each fldItem In rngStory.Fields
If fldItem.Type = wdFieldRef Then
fldItem.Update
End If
Next
' Get next linked story (if any)
Set rngStory = rngStory.NextStoryRange
Loop Until rngStory Is Nothing
Next
End Sub
Private Sub btnUpdateEmployee_Click()
ActiveDocument.Unprotect Password:=""
frmUpdateEmployee.Show
End Sub

Also, since I have bookmarks (populated by a user form), I am struggling with unprotecting the document to update the bookmarks and then re-protecting it. Form Field and Content Control protection seem to be a bit different.

Can any of you people see where I am hitting the road-blocks?

Still searching for the solutions.... I will update this when I find out more.

Thank you everone,

Brent

gmaxey
12-10-2012, 09:05 PM
Brent,

Why are you mixing content controls and legacy form fields? Why are you using REF fields instead of mapped content controls?

The attached document uses locked content controls and document editing restrictions.

See: http://gregmaxey.mvps.org/word_tip_pages/content_control_fillin_form.html

brent.fraser
12-11-2012, 07:18 AM
Hey Greg,

The reason why I am using content controls and legacy form fields is because I am very new to VBA and I am looking to see what works for what I am doing. Also, it was a test to see if the content controls worked as well as legacy forms. My plan was to get rid of legacy forms once all content controls worked.

I just looked at what you have in your document/page and I will try to work my way through that. Thanks for the point in the right direction.

Hopefully this will solve my issues and hopefully have the information updated as soon as the checkbox is selected.

Thanks again.

brent.fraser
12-12-2012, 10:16 AM
Hey Greg,

The sample you posted is great. Jist what I was looking for. Thank you!

What I am trying to do now is when someone checks the checkbox, I want to display the status and also a description. In your code, you identify a single target to change based on the checkbox. How do you identify two content control targets to change based on the single checkbox?

I have tried to create two targets but when I call the text to populate them, I am running into a few issues. Here's a snippet of your code I modified:
Private Sub Custom_ContentControlOnChange(oCC_Changed As ContentControl)
Dim oCC_Target As ContentControl
Dim oCC_Target2 As ContentControl
Dim pText As String
Dim pStatus As String
Select Case oCC_Changed.Tag
Case "unsatisfactory"
Set oCC_Target = ActiveDocument.SelectContentControlsByTag("description").Item(1)
Set oCC_Target2 = ActiveDocument.SelectContentControlsByTag("status").Item(1)
#If VBA7 Then
If oCC_Changed.Checked = True Then
pText = "unsatisfactory"
'this is where I would like to call the status text but I am unsure how to place the target here.
pStatus = "Your Status is Unsatisfactory"
Else
pText = ""
pStatus = ""
End If
#Else
pText = "This is a remnant of a Word 2010 checkbox content control that is not compatible with Word 2007."
#End If
With oCC_Target
.LockContents = False
.Range.Text = pText
.LockContents = True
End With
Set oCC_Target = ActiveDocument.SelectContentControlsByTag("status").Item(1)
#If VBA7 Then
If oCC_Changed.Checked = True Then
pText = "unsatisfactory"
Else
pText = ""
End If
#Else
pText = "This is a remnant of a Word 2010 checkbox content control that is not compatible with Word 2007."
#End If
With oCC_Target
.LockContents = False
.Range.Text = pText
.LockContents = True
End With

Thank you once again for your code and help. It was great.

Brent

gmaxey
12-12-2012, 07:29 PM
Brent,

Seems you would use something like this:
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
Dim oCC_Target As ContentControl
Dim pText As String
Dim pStatus As String
Select Case ContentControl.Tag
Case "unsatisfactory"
#If VBA7 Then
If ContentControl.Checked = True Then
pText = "unsatisfactory"
pStatus = "Your Status is Unsatisfactory"
Else
pText = ""
pStatus = ""
End If
#Else
pText = "This is a remnant of a Word 2010 checkbox content control that is not compatible with Word 2007."
#End If
Set oCC_Target = ActiveDocument.SelectContentControlsByTag("description").Item(1)
With oCC_Target
.LockContents = False
.Range.Text = pText
.LockContents = True
End With
Set oCC_Target = ActiveDocument.SelectContentControlsByTag("status").Item(1)
With oCC_Target
.LockContents = False
.Range.Text = pStatus
.LockContents = True
End With
End Select
End Sub

brent.fraser
12-13-2012, 07:53 AM
Greg,

I know I have said it before but I will say it again. You are amazing!!!! Thank you so incredibly much for what you have helped me out with. I appreciate your assistance with this!

Have a great day in N.C.

Brent