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