Applying multiple custom styles to a Word range RRS feed

  • Question

  • I am trying to apply multiple custom styles on a particular range and I need to retain the properties of both the styles on the selected range.

    For example, if its a code sample that has a bulleted list,i first apply a custom style to make the background color for the text as Grey.I then apply a custom Bullet style which has a White background property.When I do this programatically,I get incorrect formatting for the content,the last applied styles overwrites the earlier one.In the scenario mentioned above, I get text with grey background,but the bulleted lines have a white background around them.

    Is there a way to retain such overlapping style properties on the range?

    Thursday, September 15, 2011 11:25 AM

All replies

  • H K

    Perhaps if you were to give detailed steps how you do it manually (the way you want) and the code, we can figure out where the difference is coming from?

    Which version of Word is involved?

    In general terms, what you're seeing with the code is correct. A paragraph style overrides another paragraph style. Any formatting applied directly (not as part of a style) should be retained; any character style applied should be retained.

    Cindy Meister, VSTO/Word MVP
    Friday, September 16, 2011 7:24 AM
  • Hi K Pallavi,

    As Cindy suggested, could you please provide more information? Would you mind providing the code on your side? If so, we could reproduce this issue on our side.

    Have a nice day.

    Best regards

    Liliane Teng [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.

    Tuesday, September 20, 2011 10:59 AM
  • Hi,

     We are using Word 2007 VSTO add-in.

    An example of what we are tyring to achieve is if we have some content "Note: This is note content" ,I am trying to apply a style say "Note Heading" to the "Note:" text and style "Note" to the rest of the content "This is note content".

    Both the styles are Paragraph styles. I am not able to do this either manually or programatically. If I change the style type to "Linked(paragraph and character)" for both the styles,I can do the required change manually. But I am still unable to do it programatically.

    My code is like :

    Word.Range oRange =null;
    object oStyle = "Note";
    object oHeaderStyle = "Note Heading";
    oRange.Text = "Note: This is note content";
    oRange.set_Style(ref oStyle);
    oRange.Words[1].set_Style(ref oHeaderStyle);

    But this ends up applying the oHeaderStyle style to the entire paragraph content.

    Is there a workaround I could try with the code here?



    Monday, October 31, 2011 1:25 PM
  • Hi Pallavi

    The key is for you to make the "Note Heading" style a character style. You can leave Note as a paragraph style. Once you've made that change (you'll need to recreate the style, you can turn a paragraph style into a character style), the code should work, as it stands.

    In Word 2007 it would be possible to do what you have in mind if Note Heading is defined as a linked style. But the only time you'd want to do that is if you're actually formatting entire paragraphs with that style.

    Cindy Meister, VSTO/Word MVP
    Monday, October 31, 2011 3:28 PM
  • Hi Cindy,

    Thank you for the suggestion.This might work for our scenario.

    There is another scenario with multiple styles where we are having trouble.If the source content has some list items(bullets/numbers) and we apply a custom style on that content(a Paragraph style),the numbers/bullets are getting removed from the content. Is there a way we can apply the style while retaining the numbered/bulleted lists?



    Monday, November 14, 2011 9:36 AM
  • Hi Pallavi

    Please post your new question as a new discussion. That will ensure you get the broadest community exposure. Don't forget to specify the version of Word involved!

    For this new question, you should also specify where the bullets/numbering formatting is coming from: Was it applied by clicking the buttons in the UI? By applying a style? Something else?

    Cindy Meister, VSTO/Word MVP
    Monday, November 14, 2011 4:59 PM