locked
Word 2007 : Apply style to selection RRS feed

  • Question

  • Hi,

    I am loading custom styles of active document in comboBox of MyRibbon of Word 2007.

    When I am changes the style from style comboBox of MyRibbon, if any word or range is selected then style applies to only that portion.

    But if there is not selection, style is applied to entire row. It should behave like word, only current cursor range style should change and when one start writing then it should be of applied style.

    Code Snip

    <comboBox id="cboChooseItem" getItemCount="cboGetItemCount"

                                 getItemID ="cboGetItemID"

                                 getItemLabel ="cboGetItemLabel"

                                 onChange ="OnChangeCmbStyle"/>

    public string OnChangeCmbStyle(Office.IRibbonControl control, string index)

    {

    object style = index;

          if (control.Id == "cboChooseItem")

          {

                ObjActiveDocument = Globals.ThisAddIn.Application.ActiveDocument; 

                ObjActiveDocument.Application.ScreenUpdating = false;

                ObjActiveDocument.Application.Selection.SetRange(ObjActiveDocument.Application.Selection.Start,ObjActiveDocument.Application.Selection.End);

                ObjActiveDocument.Application.Selection.set_Style(ref style);

                ObjActiveDocument.Application.ScreenUpdating = true;

    }

          return index;

    }

    Thanks in Advance.

    • Edited by Mihir1303 Saturday, August 8, 2009 7:54 AM subject change
    Saturday, August 8, 2009 7:51 AM

Answers

  • Hi Mihir

    In Word terminology, "row" means a Table Row. I suspect you may mean "line" or, more likely in the given context "paragraph"?

    I think you need to test this behavior in the Word UI, as an end-user. In my experience, if you have a blinking insertion point (rather than a selection), applying a paragraph style affects the entire paragraph. This is simply tested:

    1. Type text for multiple paragraphs in a document
    2. Click in any one paragraph, so that the insertion point ("cursor") is blinking
    3. From the Styles group in the Home tab, click "Heading 1"

    You should see that "Heading 1" is applied to the entire paragraph.

    Thus, the behavior you describe is by design and how Word handles paragraph style application.

    Word 2003 and 2007 support the concept of linked styles, meaning a single style can target both paragraphs and characters. In this case, when text is selected, the style is applied to the selected characters, rather than to the entire paragraph.

    Possibly, what you require is a style explicitly for formatting characters (a character style). When using a character style you're more likely to get the behavior you require.
    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Mihir1303 Monday, August 10, 2009 7:06 AM
    Sunday, August 9, 2009 4:08 PM