none
Word 2010 Checkbox Content Control - XML binding RRS feed

  • Question

  • This is frustrating me to no end, and it's probably a very simple thing.  Here's the deal - I have a Word 2010 document that consists of a mixture of textboxes, rich textboxes, and checkboxes.  The rich text and the text is no problem when I bind them using a CustomXML part (I'm doing this because it's a form filled out on a web page and needs to be in Word for printing, but not editing, purposes) but the checkboxes - how on earth do you get those to check or uncheck when you're binding this way? 

    As I understand it, checked/unchecked is a method, and to set it, you set a property (In this case, true/false).  The XML file contains values, not properties.  Here is an example of the XML file:

    <Form>
      <StudentNumber>123456</StudentNumber>
      <StudentName>Test</StudentName>
      <IsReeval>Reevaluation</IsReeval>
      <ReviewExisting>-1</ReviewExisting>
    </Form>

    In this case, the "ReviewExisting" entry is the checkbox.  Trying to set it just like the other controls:

    Dim part As CustomXMLPart
    Set part = ActiveDocument.CustomXMLParts.SelectByNamespace("")(1)

    Dim ctlReviewExisting As ContentControl
    Set ctlReviewExisting = ActiveDocument.ContentControls(4)
    ctlReviewExisting.XMLMapping.SetMapping "/Form/ReviewExisting", "", part

    It just won't work.  I've tried every value I can think of and my box stays unchecked.  Any help would be appreciated.

     

    Wednesday, December 8, 2010 8:16 PM

Answers

  • I found some code that creates a mapped CC on the fly so to speak.  If you open a new document and run it, it will insert a mapped CC.  Next I manually inserted another checkbox CC and ran the sub Scratchmacro provided below. It may help you see how to map your CC:

    Option Explicit
    Sub AddandMapCContheFly()
    Dim oCustXMLParts As CustomXMLParts
    Dim oCustXMLPart As CustomXMLPart
    Dim oCustXMLNode As CustomXMLNode
    Dim xPath As String
    Dim pStr As String
    Dim oCCs As ContentControls
    Dim oCC As ContentControl
    Set oCCs = ActiveDocument.ContentControls
    Set oCustXMLParts = ActiveDocument.CustomXMLParts
    Err_ReEnter:
    'Define a collection of customXMLParts with unique namespace.
    Set oCustXMLParts = oCustXMLParts.SelectByNamespace _
       ("http://gremaxey.mvps.org/CustomXML/MappedCCs")
    On Error GoTo Err_Handler
    'Use the unique oCustomPart. If it doesn't exist use Error Handling to create it.
    Set oCustXMLPart = oCustXMLParts(1)
    On Error GoTo 0
    'Define the XML Node that you want to add a child node under.
    Set oCustXMLNode = oCustXMLPart.SelectSingleNode("/ns0:ccMap[1]")
    'Add the titled ContentCOntrol
    Set oCC = oCCs.Add(wdContentControlCheckBox)
    oCC.Title = InputBox("Enter the title for this CC:", "CC Title")
    'Add the new node
    pStr = "ccElement_" & oCC.ID
    oCustXMLPart.AddNode oCustXMLNode, pStr, _
      "http://gremaxey.mvps.org/CustomXML/MappedCCs", , , ""
    'Map CC to new node
    xPath = "/ns0:ccMap[1]/ns0:" & pStr
    oCC.XMLMapping.SetMapping xPath
    Exit Sub
    Err_Handler:
    Set oCustXMLPart = oCustXMLParts.Add _
      ("<ccMap xmlns='http://gremaxey.mvps.org/CustomXML/MappedCCs'></ccMap>")
    Resume Err_ReEnter
    End Sub
     Sub ScratchMacro()
    'A quick macro scratch pad created by Greg Maxey
    Dim oCC As ContentControl
    Dim xPath As String
    xPath = ActiveDocument.ContentControls(1).XMLMapping.xPath
    Set oCC = ActiveDocument.ContentControls(2)
    oCC.XMLMapping.SetMapping xPath
    MsgBox xPath
    End Sub
       "streamhopper" wrote in message news:a1b824eb-8d1b-4269-895e-6b4c3fd6311c@communitybridge.codeplex.com...

    Hello Greg,

    Yes.  I've tried true/false, 1/0, -1/0, on/off - every combination I could think of that could toggle visibility.  I played with the idea of using Unicode checkboxes from Wingdings 2, but the ActiveX controls don't auto-convert Unicode characters.  I then tried just changing the font to Wingdings 2, but the unchecked box is a GB pound sterling symbol, which doesn't appear on the keyboard.  I suppose if I looked long enough and hard enough, I may be able to find a font that has what I want; however, it seems to me that it should be possible to check and uncheck that little box this way.  Somehow.  Any help, greatly appreciated.  If further details are needed, please ask.


    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm
    • Marked as answer by Bessie Zhao Thursday, December 16, 2010 9:09 AM
    Thursday, December 9, 2010 10:24 PM

All replies

  • Have you tried using "true" or "false" in your XML vice "-1?"
     "streamhopper" wrote in message news:8bdd746e-1054-48cb-b266-43c0e67dde2b@communitybridge.codeplex.com...

    This is frustrating me to no end, and it's probably a very simple thing. Here's the deal - I have a Word 2010 document that consists of a mixture of textboxes, rich textboxes, and checkboxes.  The rich text and the text is no problem when I bind them using a CustomXML part (I'm doing this because it's a form filled out on a web page and needs to be in Word for printing, but not editing, purposes) but the checkboxes - how on earth do you get those to check or uncheck when you're binding this way?

    As I understand it, checked/unchecked is a method, and to set it, you set a property (In this case, true/false).  The XML file contains values, not properties.  Here is an example of the XML file:

    <Form>
     <StudentNumber>123456</StudentNumber>
     <StudentName>Test</StudentName>
     <IsReeval>Reevaluation</IsReeval>
     <ReviewExisting>-1</ReviewExisting>
    </Form>

    In this case, the "ReviewExisting" entry is the checkbox.  Trying to set it just like the other controls:

    Dim part As CustomXMLPart
    Set part = ActiveDocument.CustomXMLParts.SelectByNamespace("")(1)

    Dim ctlReviewExisting As ContentControl
    Set ctlReviewExisting = ActiveDocument.ContentControls(4)
    ctlReviewExisting.XMLMapping.SetMapping "/Form/ReviewExisting", "", part

    It just won't work.  I've tried every value I can think of and my box stays unchecked.  Any help would be appreciated.


    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm
    Wednesday, December 8, 2010 9:28 PM
  • Hello Greg,

    Yes.  I've tried true/false, 1/0, -1/0, on/off - every combination I could think of that could toggle visibility.  I played with the idea of using Unicode checkboxes from Wingdings 2, but the ActiveX controls don't auto-convert Unicode characters.  I then tried just changing the font to Wingdings 2, but the unchecked box is a GB pound sterling symbol, which doesn't appear on the keyboard.  I suppose if I looked long enough and hard enough, I may be able to find a font that has what I want; however, it seems to me that it should be possible to check and uncheck that little box this way.  Somehow.  Any help, greatly appreciated.  If further details are needed, please ask.

    Wednesday, December 8, 2010 9:35 PM
  • Hi,

    It has been a long time since I tinkered with XML code.  A few years ago I wrote and AddIn for creating mapped ContentControls in a Word document.  I modified it when Word2010 came out to include a check box.  When I look at the resulting XML that is created it looks like this:

    <ccMap xmlns="DemoNS"><ccElement_CB1>true</ccElement_CB1></ccMap>

    That "true" changes to "false" when the box is not checked.

    I can send you the AddIn and perhaps you are XML savvy enough to sort out what I did in the first place. I just can't get my head around it right now ;-)

    Greg

    "streamhopper" wrote in message news:a1b824eb-8d1b-4269-895e-6b4c3fd6311c@communitybridge.codeplex.com...

    Hello Greg,

    Yes.  I've tried true/false, 1/0, -1/0, on/off - every combination I could think of that could toggle visibility.  I played with the idea of using Unicode checkboxes from Wingdings 2, but the ActiveX controls don't auto-convert Unicode characters.  I then tried just changing the font to Wingdings 2, but the unchecked box is a GB pound sterling symbol, which doesn't appear on the keyboard.  I suppose if I looked long enough and hard enough, I may be able to find a font that has what I want; however, it seems to me that it should be possible to check and uncheck that little box this way.  Somehow.  Any help, greatly appreciated.  If further details are needed, please ask.


    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm
    Thursday, December 9, 2010 2:58 AM
  • Is there definitely only one non-builtin CustomXMLPart in your document, and are you definitely setting all your controls to point to the same part? (It doesn't seem likely, but I just wondered whether you might have set your other controls to point at one part, then inadvertently added another part and pointed the new checkbox control to that, or some such).

     


    Peter Jamieson
    Thursday, December 9, 2010 10:08 PM
  • I found some code that creates a mapped CC on the fly so to speak.  If you open a new document and run it, it will insert a mapped CC.  Next I manually inserted another checkbox CC and ran the sub Scratchmacro provided below. It may help you see how to map your CC:

    Option Explicit
    Sub AddandMapCContheFly()
    Dim oCustXMLParts As CustomXMLParts
    Dim oCustXMLPart As CustomXMLPart
    Dim oCustXMLNode As CustomXMLNode
    Dim xPath As String
    Dim pStr As String
    Dim oCCs As ContentControls
    Dim oCC As ContentControl
    Set oCCs = ActiveDocument.ContentControls
    Set oCustXMLParts = ActiveDocument.CustomXMLParts
    Err_ReEnter:
    'Define a collection of customXMLParts with unique namespace.
    Set oCustXMLParts = oCustXMLParts.SelectByNamespace _
       ("http://gremaxey.mvps.org/CustomXML/MappedCCs")
    On Error GoTo Err_Handler
    'Use the unique oCustomPart. If it doesn't exist use Error Handling to create it.
    Set oCustXMLPart = oCustXMLParts(1)
    On Error GoTo 0
    'Define the XML Node that you want to add a child node under.
    Set oCustXMLNode = oCustXMLPart.SelectSingleNode("/ns0:ccMap[1]")
    'Add the titled ContentCOntrol
    Set oCC = oCCs.Add(wdContentControlCheckBox)
    oCC.Title = InputBox("Enter the title for this CC:", "CC Title")
    'Add the new node
    pStr = "ccElement_" & oCC.ID
    oCustXMLPart.AddNode oCustXMLNode, pStr, _
      "http://gremaxey.mvps.org/CustomXML/MappedCCs", , , ""
    'Map CC to new node
    xPath = "/ns0:ccMap[1]/ns0:" & pStr
    oCC.XMLMapping.SetMapping xPath
    Exit Sub
    Err_Handler:
    Set oCustXMLPart = oCustXMLParts.Add _
      ("<ccMap xmlns='http://gremaxey.mvps.org/CustomXML/MappedCCs'></ccMap>")
    Resume Err_ReEnter
    End Sub
     Sub ScratchMacro()
    'A quick macro scratch pad created by Greg Maxey
    Dim oCC As ContentControl
    Dim xPath As String
    xPath = ActiveDocument.ContentControls(1).XMLMapping.xPath
    Set oCC = ActiveDocument.ContentControls(2)
    oCC.XMLMapping.SetMapping xPath
    MsgBox xPath
    End Sub
       "streamhopper" wrote in message news:a1b824eb-8d1b-4269-895e-6b4c3fd6311c@communitybridge.codeplex.com...

    Hello Greg,

    Yes.  I've tried true/false, 1/0, -1/0, on/off - every combination I could think of that could toggle visibility.  I played with the idea of using Unicode checkboxes from Wingdings 2, but the ActiveX controls don't auto-convert Unicode characters.  I then tried just changing the font to Wingdings 2, but the unchecked box is a GB pound sterling symbol, which doesn't appear on the keyboard.  I suppose if I looked long enough and hard enough, I may be able to find a font that has what I want; however, it seems to me that it should be possible to check and uncheck that little box this way.  Somehow.  Any help, greatly appreciated.  If further details are needed, please ask.


    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm
    • Marked as answer by Bessie Zhao Thursday, December 16, 2010 9:09 AM
    Thursday, December 9, 2010 10:24 PM
  • The suggestion that was marked as answer leads to the solution, and helped me find it too (actually easy because I could just set the checkbox in my template and then step into the customxml update code and see what the node text is prior to updating it).

    But I don't think the actual answer to the question was posted, so here it is: you need lower case 'true' or 'false' ... 

    Tuesday, September 18, 2012 7:49 PM