none
How to Show/Hide Checkboxes

    Question

  • Good day!
    I have created a simple Word add-in that allows me to add a checkbox in a document by clicking a button. I followed this example on how to create the checkboxes. http://www.cimaware.com/resources/article_82.html 

    But at a certain point in my program, through another click of a button, I want to be able to hide the unchecked boxes (and the text beside them) and hide the checked boxes (but leave the text beside them). How can this be achieve?

    Thank  you very much.
    Monday, May 25, 2009 3:44 PM

Answers

  • Hi Joey

    Mmm, that sort of depends on how the document should look/work. Two possibilities that come immediately to mind:

    1. A table (cell or row)

    2. A bookmark


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by dyowee Tuesday, May 26, 2009 8:38 AM
    Tuesday, May 26, 2009 8:33 AM

All replies

  • Based on the example you used, I'd create a Character Style and use it to format each range of text that will be hidden/shown. Just change the style definition for the Font.Hidden property whenver you want to hide or show the checkboxes.


    Cindy Meister, VSTO/Word MVP
    Tuesday, May 26, 2009 6:47 AM
  • Good day ma'am!
    Actually ma'am, I have a set of checkboxes (with text after each textbox) within a range, in a single line in a document. But I'm not sure which property of the Range object should I use to get the checkbox (and text) objects so I can hide them. Just to test, I accessed Range.Fields, and for each Field object, I accessed the Result property (which is a Range object). The Result object has a Font property. I sent the Font.Hidden to 1, but it stays as 0. I'm not sure if what I'm doing is right. I hope you can point me to the right way. Thank you very much.
    Tuesday, May 26, 2009 7:05 AM
  • It's difficult for me to give you any more detailed instructions without knowing the version of Word involved.

    In any case, it makes no sense to work with the individual ranges - that's much too inefficient. If you want to test, do something like Range.Paragraphs(1).Font.Hidden = true. The Result property of a Field object may or may not return the range you expect - it depends very much on the field involved. For this, it would probably make sense to include the \* CharFormat switch in the MacroButton field codes to force the entire field to take on the formatting of the first character in the field code.

    Are you working with document protection, so that the user can't change the text? If you are, then you may not be able to change the formatting of a range without removing the protection, first. The definition of a style, however, can be changed with protection in-force.


    Cindy Meister, VSTO/Word MVP
    Tuesday, May 26, 2009 7:23 AM
  • Hello again ma'am!

    I'm using Word 2007. The type of Font.Hidden is an integer. So should I set it to 1? Also, I'm not using document protection. From the example link I sent, how would I be able to get the Range of a checkbox and the text beside it?

    Kind regards,
    Joey
    Tuesday, May 26, 2009 7:28 AM
  • Hi Joey

    Usually, I'd set Font.Hidden to True or False, but you can also use -1 or 0, respectively (that's what VBA uses for True/False).

    The example in the link is not accurate enough for getting the Range AND the "text beside it". I have no way of knowing how to determine the extent of the "text beside it".

    As I mentioned before, the most reliable and efficient way would be to define a Character Style. Apply that style to the AutoText entries, as well as to all the text in the document that should be shown/hidden at the same time. Have your code change the Font.Hidden property of the Style.

    In order to create a style, click the Dialog Launcher of the Style group, in the Home tab of the Ribbon. At the bottom of the Styles task pane, click the "New Styles" button (left-most). Give the style a name. Select as the Style Type "Character".
    Cindy Meister, VSTO/Word MVP
    • Marked as answer by dyowee Tuesday, May 26, 2009 8:23 AM
    • Unmarked as answer by dyowee Tuesday, May 26, 2009 8:25 AM
    Tuesday, May 26, 2009 8:13 AM
  • Sorry about that. I'll try this one ma'am. But I have a last inquiry. From the example link I sent, is there a way I can somehow group a checkbox and the text beside it?
    Thank you very much for your help. I really appreciate it.

    Regards,
    Joey
    Tuesday, May 26, 2009 8:26 AM
  • Hi Joey

    Mmm, that sort of depends on how the document should look/work. Two possibilities that come immediately to mind:

    1. A table (cell or row)

    2. A bookmark


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by dyowee Tuesday, May 26, 2009 8:38 AM
    Tuesday, May 26, 2009 8:33 AM