none
How to get an existing style from a word document? RRS feed

  • Question

  • I need to get an already existing style from a word document and set the Range object's style to that style. Also is there some reason I can't get to Range.Style it is not available in intellisense.
    Tuesday, June 28, 2011 4:34 PM

Answers

  • Style createdStyle=null;

    foreach(Style style in document.Styles)

    {

    if(style.NameLocal ==stylename)

    createdStyle = style;

    }


    • Marked as answer by Just Perdueit Tuesday, June 28, 2011 8:30 PM
    • Edited by Just Perdueit Thursday, June 30, 2011 4:28 PM wasn't using correct property name
    Tuesday, June 28, 2011 8:30 PM

All replies

  • Using C#? Then you need set_Style and get_Style (of the Range or Selection object). It's because this property works with parameters, something C# only allows with methods.

    I'm not sure how to understand the first part of your question? Unless the above answers it...


    Cindy Meister, VSTO/Word MVP
    Tuesday, June 28, 2011 4:52 PM
    Moderator
  • So is Range.Style only available in vb?

     

     

    I did figure out how to get the quick style I needed.

    Tuesday, June 28, 2011 6:29 PM
  • Style createdStyle=null;

    foreach(Style style in document.Styles)

    {

    if(style.NameLocal ==stylename)

    createdStyle = style;

    }


    • Marked as answer by Just Perdueit Tuesday, June 28, 2011 8:30 PM
    • Edited by Just Perdueit Thursday, June 30, 2011 4:28 PM wasn't using correct property name
    Tuesday, June 28, 2011 8:30 PM
  • I'd rather put it that the syntax Range.Style is not useable as such in C# due to limitations of how C# is designed. It would likely be available in other programming languages that don't have the same limitations. But the get_Style and set_Style methods are exact equivalents.


    Cindy Meister, VSTO/Word MVP
    Wednesday, June 29, 2011 8:59 AM
    Moderator
  • I think you should edit your message and correct the code in case others try to use it, since you marked it as an answer. It's style.namelocal, not localname.
    Cindy Meister, VSTO/Word MVP
    Wednesday, June 29, 2011 8:59 AM
    Moderator