none
Using get_Style, set_Style, get_BaseStyle, set_BaseStyle, etc. in C# RRS feed

  • Question

  • I have been using Word interop with VB for a while, including working directly with properties of Word styles. In automating Word with C#, I am coming across new (to me) and different usage such as get_Style, set_Style, get_BaseStyle, set_BaseStyle.

    What I am looking for is a reference or documentation describing this approach, as well as what other, similar, methods might be available for different Word objects. I'm coming up empty. (I assume documentation is lacking because these are dynamically created methods?)

    I would love to know more background on this and understand which Word objects have these "extra" methods available in C#. i.e., a solid explanation of which Word objects have these and why. And curiously, why I can't seem to find any mention of them when searching the web.

    Thanks

    Wednesday, September 2, 2015 6:38 PM

Answers

  • Hi wordyone

    These commands aren't "hidden". As I explained in my previous reply, in order for C# to be able to communicate correctly with the Office COM libraries, properties that accept optional parameters and parameters of mixed data type need to be exposed as methods and must therefore be renamed. Anytime you see a "get_" or "set_" method for Word, the "native" property does not have such a prefix but it IS documented - just remove the prefix to find the documentation topic. So if you're looking at the object model documentation (where you will find Style and BaseStyle, for instance) and don't see the property in the Intellisense list when typing code, try with get/set.


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, September 7, 2015 3:22 PM
    Moderator
  • Hi wordyone,

    >> What I am looking for is a reference or documentation describing this approach, as well as what other, similar, methods might be available for different Word objects.

    I tried to find such a document, but I failed. What I could find is about style and Style.BaseStyle property. As my understanding, every object in word has the style and BaseStyle property, like paragraph, header, text and etc. Style represents a single built-in or user-defined style. Style.BaseStyle returns or sets an existing style on which you can base the formatting of another style.

    Here is a simple code:

                Style style=word.Selection.get_Style();
                word.Selection.set_Style(1);
                style.set_BaseStyle(1);
                Style basestyle = style.get_BaseStyle();
                basestyle.set_BaseStyle(1);

    I think the links below might be useful to you:

    # Style Interface
    https://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.style(v=office.14).aspx
    # Style.BaseStyle property
    https://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.style.basestyle.aspx

    Best Regards,

    Edward


    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.


    Thursday, September 3, 2015 7:53 AM
    Moderator
  • Hi wordyone

    Edward's explanation is correct, as far as it goes. These aren't methods in the Word object model, but properties. The PIA that "translates" between .NET and COM has to present them to C# as methods because of the mixed data types some of the parameters can accept (string or integer). The Bookmark property of the Range and other objects is a similar problem, the PIA provides the method: Bookmarks.get_Item(ref name).

    If you're having difficulty understanding the BaseStyle->Style concept, think of like cascading styles. The functionality is not identical, but close enough to understand what BaseStyle is for :-) You can see this in the UI by going to any dialog box that lets you create or modify a style.


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, September 3, 2015 4:46 PM
    Moderator

All replies

  • Hi wordyone,

    >> What I am looking for is a reference or documentation describing this approach, as well as what other, similar, methods might be available for different Word objects.

    I tried to find such a document, but I failed. What I could find is about style and Style.BaseStyle property. As my understanding, every object in word has the style and BaseStyle property, like paragraph, header, text and etc. Style represents a single built-in or user-defined style. Style.BaseStyle returns or sets an existing style on which you can base the formatting of another style.

    Here is a simple code:

                Style style=word.Selection.get_Style();
                word.Selection.set_Style(1);
                style.set_BaseStyle(1);
                Style basestyle = style.get_BaseStyle();
                basestyle.set_BaseStyle(1);

    I think the links below might be useful to you:

    # Style Interface
    https://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.style(v=office.14).aspx
    # Style.BaseStyle property
    https://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.style.basestyle.aspx

    Best Regards,

    Edward


    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.


    Thursday, September 3, 2015 7:53 AM
    Moderator
  • Hi wordyone

    Edward's explanation is correct, as far as it goes. These aren't methods in the Word object model, but properties. The PIA that "translates" between .NET and COM has to present them to C# as methods because of the mixed data types some of the parameters can accept (string or integer). The Bookmark property of the Range and other objects is a similar problem, the PIA provides the method: Bookmarks.get_Item(ref name).

    If you're having difficulty understanding the BaseStyle->Style concept, think of like cascading styles. The functionality is not identical, but close enough to understand what BaseStyle is for :-) You can see this in the UI by going to any dialog box that lets you create or modify a style.


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, September 3, 2015 4:46 PM
    Moderator
  • Thanks Edward and Cindy. I am very familiar with the Word object and usage of BaseStyle, NextParagraphStyle, etc. I was just very surprised when I could not find any documentation on these special "get..." and "set..." commands that seem to be built in to C# dynamically. I am glad the commands are there, though :-)  It makes me wonder what other hidden commands I am missing, however.
    • Edited by wordyone Thursday, September 3, 2015 5:51 PM added something
    Thursday, September 3, 2015 5:48 PM
  • Hi wordyone,

    You are right, documentation on these methods would be helpful when we are developing. But, unfortunately, there is no such document as my research. If you have any issue about this commands when you develop, please feel free to post in this forum.

    Best Regards,

    Edward


    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.


    Monday, September 7, 2015 11:42 AM
    Moderator
  • Hi wordyone

    These commands aren't "hidden". As I explained in my previous reply, in order for C# to be able to communicate correctly with the Office COM libraries, properties that accept optional parameters and parameters of mixed data type need to be exposed as methods and must therefore be renamed. Anytime you see a "get_" or "set_" method for Word, the "native" property does not have such a prefix but it IS documented - just remove the prefix to find the documentation topic. So if you're looking at the object model documentation (where you will find Style and BaseStyle, for instance) and don't see the property in the Intellisense list when typing code, try with get/set.


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, September 7, 2015 3:22 PM
    Moderator