Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 06-23-2015, 08:44 AM
DougsGraphics DougsGraphics is offline Windows 7 64bit Office 2010 32bit
Novice
 
Join Date: Jun 2015
Location: New Mexico
Posts: 2
DougsGraphics is on a distinguished road
Question Clicking the selected Content Control checkbox returns wrong control in vba event


I am perplexed by the behavior of a word 2010 form built using content controls. I have found a very reproducible error that is driving me nuts. To illustrate, the form has a lot of checkbox controls that I use as radio buttons through vba. For example, the word file will have a list of options, each content control having the same title (e.g., "Char") and a sequential ordering of tags built from the title (e.g., "Char-0", "Char-1"...). When one of the items in the list is selected, I use a Select Case statement on the title to know which list, then if the user selected (set the control to true), step through all the items of the list to deselect all the others.

The VBA for a grouping of five such controls would then look like this:

Code:
Private Sub Document_ContentControlOnEnter(ByVal CC As ContentControl)

Dim MyGrp As String
Dim i As Integer
Dim MySelectX As Integer

'get the tag number of the item in the list
If IsNumeric(Right(CC.Tag, 1)) Then
    MySelectX = CInt(Right(CC.Tag, 1))
End If

'Get the base name of the tag group
MyGrp = Left(CC.Tag, InStr(1, CC.Tag, "-"))

Select Case CC.Title
  Case "Char"
     'if user checks box, uncheck all others in list
     If CC.Checked = True Then
       For i = 0 to 4
          If i <> MySelectX then ActiveDocument.SelectContentControlsByTag(MyGrp & i).Item(1).Checked = False
      Next i
   End If

End Select
End Sub
Each time the user selects a control, the control appropriately toggles and the highlight advances to the next control in the form. The behavior I observe is that everything works just fine UNLESS one of the items in the list is already checked, and the user clicks on the selected (highlighted) control. The clicked control appropriately toggles, and the highlight advances to the next, HOWEVER, the content control that is then returned through the event handler is NOT the item the user selected, but instead, the next control in the form. Needless to say, things don't work in that event and you end up with multiple items in the option list selected. It doesn't matter which position in the list is clicked, it is only dependent upon whether the highlighted control is clicked.

Has anyone else observed, and more importantly, overcome this issue?

I've used both the OnEnter and OnExit event handler, and it doesn't make any difference.

Can the advancing of the control field on clicking be somehow disabled, overridden, or forced to return to the control that was clicked? If so, I can devise a work-around, but I haven't figured out how to do that.

Last edited by DougsGraphics; 06-24-2015 at 07:37 AM. Reason: To indicate solved
Reply With Quote
  #2  
Old 06-23-2015, 07:58 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,296
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Try something along the lines of:
Code:
Private Sub Document_ContentControlOnEnter(ByVal CC As ContentControl)
If CC.Type <> wdContentControlCheckBox Then Exit Sub
Application.ScreenUpdating = False
Dim MyGrp As String, i As Integer, MySelectX As Integer, bChk As Boolean
'get the tag number of the item in the list
If UBound(Split(CC.Tag, "-")) > 0 Then
  If IsNumeric(Right(CC.Tag, 1)) Then
    MySelectX = CInt(Split(CC.Tag, "-")(1))
  Else
    MySelectX = ""
  End If
End If
'Get the base name of the tag group
MyGrp = Split(CC.Tag, "-")(0) & "-"
Select Case CC.Title
  Case "Char"
    bChk = ActiveDocument.SelectContentControlsByTag(MyGrp & MySelectX)(1).Checked
    'if user checks box, uncheck all others in list
    If CC.Checked = True Then
      For i = 0 To 4
        ActiveDocument.SelectContentControlsByTag(MyGrp & i)(1).Checked = False
      Next i
      If bChk = False Then
        ActiveDocument.SelectContentControlsByTag(MyGrp & MySelectX)(1).Checked = True
      End If
    End If
End Select
Application.ScreenUpdating = True
End Sub
You may also be interested in: http://www.msofficeforums.com/word-t...html#post33489
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #3  
Old 06-24-2015, 07:31 AM
DougsGraphics DougsGraphics is offline Windows 7 64bit Office 2010 32bit
Novice
 
Join Date: Jun 2015
Location: New Mexico
Posts: 2
DougsGraphics is on a distinguished road
Default Thanks!

Thanks Paul! That helped me work it out.
Reply With Quote
Reply

Tags
content control, content control events, vba
Please reply to this thread with any new information or opinions.

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace checkbox symbol with check box content control canadansk Word VBA 5 04-01-2015 08:21 AM
How to Control Worksheet Event Handler in Module? tinfanide Excel Programming 2 10-19-2014 09:46 AM
Deleting a table from a content control -- preserving the content control BrainSlugs83 Word Tables 8 11-14-2013 03:06 AM
Retrieving content control value jillapass Word VBA 4 05-24-2012 05:07 AM
Excel ->VB code for Checkbox (control toolbox) kirkstyle Excel 0 08-16-2006 04:17 PM


All times are GMT -7. The time now is 03:18 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft