none
Get Character Style RRS feed

  • Question

  • Hello,

    I was wondering if it was possible to find instances of a character style being used in a document. 

    Currently I am using visual studio to iterate through paragraphs and picking out the paragraph style, however I am a bit stumped as to how to find a character style. The issue being that a paragraph could be a mixture of a paragraph style and a character style, however a call to paragraph.getStyle() will allows return the paragraph style. 

    The only other option I could think of was to maybe iterate through each character, this would take FAR too long though.

    Thank you. 

    Will

    Tuesday, July 24, 2012 10:05 AM

Answers

  • Hi Will

    Well, if you know the name of the style you can use Find.Execute to pick up any instances of it.

    Otherwise, the only possibility is "walking" the document, character-by-character. Faster would probably be to "parse" the file's XML. If we're talking Word 2007 that would be Document.Content.WordOpenXML. You'd be looking for w:style tags in w:rPr elements.

    For example, if the built-in character style "Strong" has been used to format a run of text, the relevant XML section would be:
    <w:rPr><w:rStyle w:val="Strong"/></w:rPr><w:t>specify</w:t>


    Cindy Meister, VSTO/Word MVP

    • Marked as answer by Smithys Friday, July 27, 2012 8:19 AM
    Wednesday, July 25, 2012 9:40 AM
    Moderator

All replies

  • Hi,

    Thanks for posting in the MSDN Forum.

    Based on your issue, I think the WdStyleType Enumeration may be helpful. You can specify the WdStyleTypeParagraph or WdStyleTypeCharacter in word document by this. Please refer to the following link:http://msdn.microsoft.com/en-us/library/ff196870.aspx.

    Hope this can help you.

    Best Regards,


    Leo_Gao [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, July 25, 2012 8:00 AM
    Moderator
  • Hi,

    Thanks for your reply. 

    Yes that will be very useful and I imagine I will use this in some way. However how do I go about checking if there is a type of "wdStyleTypeCharacter" anywhere in a document, I think this is where the issue lies. 

    I can't iterate through the paragraphs as I can only retrieve paragraph styles using this method, does anyone know of any solutions to this?

    Thanks,

    Will

    Wednesday, July 25, 2012 8:47 AM
  • Hi Will

    Well, if you know the name of the style you can use Find.Execute to pick up any instances of it.

    Otherwise, the only possibility is "walking" the document, character-by-character. Faster would probably be to "parse" the file's XML. If we're talking Word 2007 that would be Document.Content.WordOpenXML. You'd be looking for w:style tags in w:rPr elements.

    For example, if the built-in character style "Strong" has been used to format a run of text, the relevant XML section would be:
    <w:rPr><w:rStyle w:val="Strong"/></w:rPr><w:t>specify</w:t>


    Cindy Meister, VSTO/Word MVP

    • Marked as answer by Smithys Friday, July 27, 2012 8:19 AM
    Wednesday, July 25, 2012 9:40 AM
    Moderator