none
Changing character style of nested Content Controls RRS feed

  • Question

  • In a VSTO application, I have the scenario of multiple nested content controls, each with a different character style (all selected from built-in styles), sort of like this:

    [(CC1) one [(CC2) two [(CC3) three [(CC4) four] three] two] one]

    The user may select to eliminate any of the controls, retaining its content and removing only the control. My code checks the nesting structure and attempts to apply the character style of the control's ParentContentControl to the text that was in the removed control. This is done by calling Selection.ClearCharacterStyle() for the Range of the removed control just before its removal and then iterating through the parent and child controls to reapply their styles using ContentControl.Range.set_Style(xxx).

    If CC4 (in the above) is the removed control, the style of CC3 gets applied to "four" as expected. If CC3 is the removed control, the style for CC2 is applied to the two occurrences of "three" and the style for CC4 is applied to "four", again as expected. If CC2 is the removed control, the style for CC1 is applied to "one two three" and "three two one", and "four" retains the style of CC4. The style for CC3 is absent! When there are even more nesting levels, it is consistent that only the innermost content control gets its style restored and all the others get the style of the parent of the removed control. What is limiting this and how do I get around it?


    Monday, August 11, 2014 7:27 PM

Answers

  • As I thought through your suggestion, I wondered how it could make a difference since that event is triggered by something I do in the already-described code. But I started copying the style reapplication code anyway and, in the process, noted that when I was saving the existing styles of the content controls that remain, I was getting the style of their Range, not the default style of the control. So I went back to the original code and collected ContentControl.get_DefaultTextStyle() instead of Range.get_Style(). Eureka! That solved it!  And so, another "Duh!" moment occurs.
    • Marked as answer by JohnWhitmire Tuesday, August 12, 2014 1:20 PM
    Tuesday, August 12, 2014 1:20 PM

All replies

  • Hi John,

    You can use Document.ContentControlBeforeDelete Event to achieve the goal.

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
    	this.Application.ActiveDocument.ContentControlBeforeDelete += ActiveDocument_ContentControlBeforeDelete;
    }
    
    void ActiveDocument_ContentControlBeforeDelete(Word.ContentControl OldContentControl, bool InUndoRedo)
    {
    	//do what you want
    }
    
    Hope this helps.

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, August 12, 2014 9:33 AM
    Moderator
  • As I thought through your suggestion, I wondered how it could make a difference since that event is triggered by something I do in the already-described code. But I started copying the style reapplication code anyway and, in the process, noted that when I was saving the existing styles of the content controls that remain, I was getting the style of their Range, not the default style of the control. So I went back to the original code and collected ContentControl.get_DefaultTextStyle() instead of Range.get_Style(). Eureka! That solved it!  And so, another "Duh!" moment occurs.
    • Marked as answer by JohnWhitmire Tuesday, August 12, 2014 1:20 PM
    Tuesday, August 12, 2014 1:20 PM