none
Adding a ContentControl from within an Event Handler RRS feed

  • Question

  • Hi Everyone,

    I have a rich text content control on a Word 2010 document that has two options; YES and NO.  If the user selects YES, I want to add a date content control just to the right of the first content control.

     

    I have written some VBA code inside the Document_ContentControlOnExit event handler.  That reads as follows:

    Private Sub Document_ContentControlOnExit(ByVal ccChanged As ContentControl, Cancel As Boolean)
    
    Dim cc, ccFirst, ccMiddle, ccLast As ContentControl
    
    
      Select Case ccChanged.Tag
       ...
    
        Case "cmbTrialDateYesNo"
          Dim oCC As ContentControl
          Set cc = ActiveDocument.SelectContentControlsByTitle("cmbTrialDateYesNo").Item(1)
          
          'If user selects YES this will add a date picker content control to the right of this content control
          If ccChanged.Range.Text = "YES" Then
            cc.Range.Text = "has been set for "
            cc.Range.Collapse (Word.WdCollapseDirection.wdCollapseEnd)
            cc.Range.MoveEnd
            Set oCC = ActiveDocument.ContentControls.Add(wdContentControlDate)
            oCC.Title = "dtTrialDate"
            oCC.Tag = "dtTrialDate"
          Else
            cc.Range.Text = "has not been set."
          End If
    
      End Select
    End Sub


    The problem is the date content control gets created where ever I click the mouse upon exiting the rich content control instead of to the right of this control.

     

    Any suggestions on how to do this would be appreciated!

     

    Thanks, Susan

    Friday, July 1, 2011 9:02 PM

All replies

  • I would create it manually in teh form then set teh Visible property to False. To display set to True.

    Rod Gill

    The one and only Project VBA Book Rod Gill Project Management
    Sunday, July 3, 2011 5:15 AM
  • Hi Rod,

    Thanks for your help!  This is the first approach I tried but I could not find a "visible" property of the date time control content control in the Word 2010 document.  How do I set this property from with in the document?

    Susan

    Sunday, July 3, 2011 2:17 PM
  • Hi Susan,

    Have you resolved your problem yet?  When exiting the content control, the mouse set focus to the location where you clicked, so the statement  ActiveDocument.ContentControls.Add(wdContentControlDate) can't add the date control to the right of the content control. I modified the code as follows which can add the date content control to the right of the content control:

     

    Private Sub Document_ContentControlOnExit(ByVal ccChanged As ContentControl, Cancel As Boolean)
    Dim cc, ccFirst, ccMiddle, ccLast As ContentControl
    Select Case ccChanged.Tag
    
     Case "cmbTrialDateYesNo"
      Dim oCC As ContentControl
      Set cc = ActiveDocument.SelectContentControlsByTitle("cmbTrialDateYesNo").Item(1)
      
      'If user selects YES this will add a date picker content control to the right of this content control
      If ccChanged.Range.Text = "YES" Then
      cc.Range.Text = "has been set for "
    '  cc.Range.Collapse (Word.WdCollapseDirection.wdCollapseEnd)
    '  cc.Range.MoveEnd
      cc.Range.Select
      Selection.Copy
      Selection.Paste
      Set oCC = Selection.ContentControls.Add(wdContentControlDate)
      oCC.Title = "dtTrialDate"
      oCC.Tag = "dtTrialDate"
      ElseIf InStr(cc.Range.Text, "has been set for") < 1 Then   ' judge whether has been set
      cc.Range.Text = "has not been set."
      End If
    
     End Select
    End Sub
    

     Hope this can resolve the problem and feel free to follow up after you have tried.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Marked as answer by Bruce Song Monday, July 11, 2011 10:42 AM
    • Unmarked as answer by Susan J Wilson Monday, July 11, 2011 3:01 PM
    Wednesday, July 6, 2011 8:30 AM
  • Hi Susan,

    What is the status of the problem on your side? If you still need any assistant, just feel free to follow up and wish you a nice day.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, July 8, 2011 8:09 AM
  • Hi Bruce,

    Sorry it's taken me so long to respond, I took an extended break away from my key board.

     Your solution is beautiful!  I had set the font to hidden to hid the text in the control, but your solution is a lot neater.

    Thanks for your help!

    Susan

    Monday, July 11, 2011 3:04 PM