How to Assign Paragraph Style when inserting text into a Word Docx using RRS feed

  • Question

  • I am trying to insert groups of text with mixed formatting which I want to keep together

    I am using  -  Windows 7(sp1) Prof, Office 2010 Prof, and VB 2010 Express(sp1)
           .NET Framework 4, Microsoft.Office.Interop.Word

    The code I have tried with most success is:-

         myParagraph = myDoc.Content.Paragraphs.add
         myParagraph.Range.ParagraphFormat.Style = "myStyle"
         myParagraph.Range.Text = "This is my text"

    The style "myStyle"  is defined as "Style Type : Paragraph"

    The formatting appears to be applied as character formatting with the font attributes assigned.

    The Style name is not assigned for the Paragraph - Defaults to  "Normal"
    The Pagination defined in the style is not applied i.e "Keep with next" & "Keep lines together"

    I don't want to set all the attributes in code  -  I want to be able to modify the output by manipulating the Styles
    I want to setup different templates with the same styles names for different output page sizes - controlled outside code

    I have also tried - separately

         myParagraph.Range.Style = "myStyle"
         myParagraph.Format.Style = "myStyle"

    Any help or pointers would be gratefully received - I am a self taught novice

    Thursday, October 25, 2012 11:08 PM

All replies

  • Hi MEWoodworker,

    Thanks for posting in the MSDN Forum.

    Based on your issue, I would suggest you try to set the style for specified paragraph like this

    Selection.Paragraphs(1).Style = "myStyle"

    And please refer to the link

    Hope it helps.

    Best Regards,

    Leo_Gao [MSFT]
    MSDN Community Support | Feedback to us

    Monday, October 29, 2012 2:00 AM
  • Thanks for the input, that was one route through the maze of alternative routes to style I did not try.

    It is certainly the most elegant.

    Unfortunately no more successful when applied to a Range than the others I tried.

    From the Object Model I believe that they should all work.

    I have been doing further research and have come across a solution. Although it does not explain the problem.

    See post :- "Adding multiple styles to selection in Word 2007"   

    (I was not trusted to give the link - I'm not sure how I get verified)

    It appears the assignments of properties for paragraph are not commutative.

    If the Assignment of Style is moved after the Text Assignment it works !!!  Hurray

    Beware !!! 

    Please refer to the above article as this does not seem to be consistent when applying a style to a Selection rather than a Range.

    The Code I settled on was independent of Selection & Range - taken from the reference kindly provided by Leo_Gao

    myParagraph.Range.Text = "This is my text"
    myParagraph.Style = "myStyle"

    To further my understanding, am I right to think :-

    1) The assignment of properties should be independent of each other?

    2) If there are any interdependencies, a method should be used to access / modify all associated properties as a set?

    3) If there are any interdependencies between Properties the Object Model would be incorrect?

    4) The implementation is not consistent with the Object Model?

    5) This is a Bug and not my inexperience as a novice programmer?

    Thanks to Leo_Gao & the forum for the work around.

    I would still like to understand the problem.

    Monday, October 29, 2012 4:03 AM
  • Hi Woodworker

    If you want to "mix and match" formatting (no matter whether using styles or direct formatting) I think it would be a good idea for you to work with objects. More like this:

      Dim para as Word.Range
      Dim rngPara as Word.Range
      Set para = myDoc.Content.Paragraphs.Add()
      Set rngPara = para.Range
      rngPara.Style = "myStyle"

    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, November 1, 2012 4:55 PM
  • Hi Cindy
    Thanks for the feed back

    I should have included my declarations in the original posting which were as follows

    Dim myDoc As Word.Document
    Dim myParagraph As Word.Paragraph

    Your declaration defines both the paragraph and the range objects as Word.Range objects I believe you intended to declare
    Dim para As Word.Paragraph
    The code extract posted will not compile in Visual Studio Express 2012
    Error 2 'Range' is not a member of 'Microsoft.Office.Interop.Word.Range'

    The other thing that Visual Studio did was to remove the Set prefix to the assignments ?

    My understanding is that the assignment creates a reference to the desired object to the right of the "=".

    Is there any difference in the reference to the range spanning the paragraph in the two examples below

    Dim myWord As Word.Application
    Dim myDoc As Word.Document
    Dim myParagraph As Word.Paragraph
    Dim myRange As Word.Range

    myWord = CreateObject("Word.Application")
    myWord.Visible = True
    myDoc = myWord.Documents.Add
    myParagraph = myDoc.Content.Paragraphs.add

    myParagraph.Range.Style = "myStyle"                '------ Reference 1

    myRange = myParagraph.Range
    myRange.Style = "myStyle"                                '------ Reference 2

    Is one more efficient in execution?

    My understanding is :-
    In both Reference 1 & 2 above, the reference is to a Style Object - which should create the exactly same reference?
    The Instantiated Object is a Word.Paragraph object, reference by myParagraph.
    References 1 & 2 above once resolved should be identical references to the same primary object produced by different indirections.

    This assumes that the Style is applied after the references are fully resolved.

    Is the application of Style context sensitive - this is not evident from the Object Model
    Does the second example above create a modified Style contained within the Paragraph? i.e. independent of Paragraph.Style

    I did not understand your suggestion.

    myDoc myParagraph and myRange are all references to an Object as is .Style above

    Unless I am making several reference to the Range Object and its Members - As you might do with the "With/End With" statements
    I do not understand the benefit of creating an intermediate reference to the range of the paragraph Object.

    I feel it is better to have a fully qualified reference to the original object to avoid confusion as to what range is being referenced.

    This solution fails in the same way as my original code if the Text is assigned after the Style.

    Thanks for your comments
    In trying to appreciate your suggestion, I believe I have increased my understanding of Objects (still a confused novice).

    Sunday, November 4, 2012 1:02 AM
  • <<Your declaration defines both the paragraph and the range objects as Word.Range objects I believe you intended to declare
    Dim para As Word.Paragraph >>

    Correct. My apologies.

    <<The other thing that Visual Studio did was to remove the Set prefix to the assignments ?>>

    Yes, as soon as I start thinking "Word object model" in the VB-language my brain automatically thinks in terms of VBA. (That's why, when programming in Visual Studio, I tend to use C#, just to keep things straight!)

      myParagraph.Range.Style = "myStyle"                '------ Reference 1

      myRange = myParagraph.Range
      myRange.Style = "myStyle"                               '------ Reference 2

    The difference between the two is very subtle. I would actually expect both to work, but when you start talking about "mixed formatting", working with individual Range objects is more specific. If I'm going to be switching formatting as I generate content, I'll almost always work with one or more Range objects, not the next "higher" level.

    I just re-read your previous reply and see "If the Assignment of Style is moved after the Text Assignment it works ". I missed that before and probably wouldn't have replied as I did if I'd picked that up earlier (guess I was having a bad day <sigh>). Yes, this is a major difference between using Selection and Range. Selection emulates how the user works, and by default Selection is always going to be a "point". Range, however, "grows" to include everything you incorporate into it.

    If you apply formatting to a "point" the formatting will, at best, apply to what comes after. Applying formatting to a Range will apply it to the entire Range, from start to end. You can think of it like working as a user: when your press Ctrl+B everything you type from that point on will be Bold - that's working with Selection. When thinking about Range, put a bookmark around some document content, add things to it, delete, etc. Then go to Insert/Bookmark and in the dialog box select that bookmark name, then click "Go to". The entire range will be selected, as it now is, not as it was when you created the bookmark. Apply the formatting and it is applied to the entire Range.

    I think I've addressed most of your questions?

    Cindy Meister, VSTO/Word MVP, my blog

    Sunday, November 4, 2012 6:05 PM