none
Match Destination Formatting while Inserting Text in Word 2003

    Question

  • I am inserting some text into a word document in C#, using Microsoft.Office.Interop.Word in Word 2003.
    I need to match the destination formatting [the smart tag that comes while pasting into word].

    I know the way to do it while copy-pasting:
    Code Block

    range.PasteAndFormat(WdRecoveryType.wdFormatSurroundingFormattingWithEmphasis);


    but, I don't wanna do it by copying [as i am using range.InsertXML]

    I thought of another way but it's not working correctly [which was to get style of current pos. before inserting]

    Code Block

    Object style = range.get_Style();

    r.set_Style(ref style); // r is range object
    r.InsertAfter("advanced");


    get_Style() is returning 'normal' while I have set the document [pre-created] to some other.

    I am using VS 2k5.
    Monday, October 15, 2007 10:47 AM

Answers

  •  pagfloyd wrote:
    >>a style is a pre-defined set of formatting
    That's what I thought...so, is there no way to get the style (i mean 'font, 'size' etc.) of the current selection (or range) and set it to some other range?

     

    Note the qualifier pre-defined. The style must already exist AND the user must have applied it. Depending on the environment and type of document you need to process, you may not be able to guarantee this is the case. You tell us nothing about the scenario in which your application will be working, so we have to assume you're working with a default installation type of situation. Most users have no idea styles exist, much less use them...

     

    The best you can do for "quick and dirty" (as opposed to working through the attributes, one-by-one) is to use the Selection.CopyFormat / Selection.PasteFormat methods of the Word object model. These emulate the Format Painter (paint brush) tool in the Word UI; for details look them up in the Help. Play around with that a bit and see if it gives you what you're after. If you need both paragraph and character-level attributes, copy the paragraph formatting first, then the character formatting.


     pagfloyd wrote:

    In word 2003 application [not the api], there is a menu 'Format' which has an option 'Reveal Current Formatting' - Can't it be done programmatically for some range/selection and applied to some other range/selection?

    Is there a way to do this using wordML?

     

    No, you can't pick up what the RevealFormatting taskpane does. Yes, it could be done using WordProcessingML, that stores formatting attributes in "PR" elements, such as w:rPR (text run properties). WordProcessingML (actually, the entire discussion) is outside the scope of this forum. You should be able to find information and get assistance at OpenXMLDeveloper.org

    Tuesday, October 16, 2007 6:56 AM

All replies

  • <<
    I thought of another way but it's not working correctly [which was to get style of current pos. before inserting]

    Code Block

    Object style = range.get_Style();

    r.set_Style(ref style); // r is range object
    r.InsertAfter("advanced");


    get_Style() is returning 'normal' while I have set the document [pre-created] to some other.>>

     

    Could you please provide a more comprehensive code snippet? One that includes setting the range and applying the style? Are you certain the formatting you're concerned about is applied by a style? Might it have been applied directly (by selecting the text and using a formatting command such as bold or italic)?

     

    FWIW, when you insert text into a range it should automatically pick up the formatting of that range. There should be no need to apply any formatting, unless you want the text to have different formatting than the target range.

    Monday, October 15, 2007 2:55 PM
  • >>Are you certain the formatting you're concerned about is applied by a style? Might it have been applied directly (by selecting the text and using a formatting command such as bold or italic)?

    I think you are correct. I am confused between formatting and style. What I want to do is: format the inserted text such that it matches the destination formatting - [fontsize, font, etc.] - which you say is automatically done if inserted into the same range.

    What if I want to match the formatting of some range to another range? [do i have to explicitly get and set font, size, indentation, line spacing etc.? or is there anything like get_Style and set_Style <- what are they used for?]

    Monday, October 15, 2007 9:14 PM
  • Back in the old Wordbasic days there used to be a way to do as you imagine. But that useful little method was lost when Word moved to the current object model 10 years ago...

     

    Now, you would indeed have to check through and duplicate every single property if you can't rely on the text being formatted with styles.

     

    (Note: a style is a pre-defined set of formatting. get_Style and set_Style are used in C# because C# doesn't support all the things that the VB languages do. In a VB language you can say Range.Style = "name of the style" OR use a wdStyle constant. This is actually cutting a corner (using a default property), something that C# doesn't allow. So the PIA for C# "wraps" this up in a syntax C# can use.)

     

    What I might be tempted to do in the scenario you describe would be to insert the text in the one range that contains the formatting. Then use the Range.FormattedText property to transfer it to a different range. And finally delete the original text you inserted.

     

    Another possibility might be to use the FormatPainter. I've never tried it, but in theory it should do what you're after.

     

    Yet a third way to approach this would be to create a style-by-example from the formatting you want to use, insert the text where ever you need it, then apply this style.

     

    Monday, October 15, 2007 9:30 PM
  • >>a style is a pre-defined set of formatting
    That's what I thought...so, is there no way to get the style (i mean 'font, 'size' etc.) of the current selection (or range) and set it to some other range?

    In word 2003 application [not the api], there is a menu 'Format' which has an option 'Reveal Current Formatting' - Can't it be done programmatically for some range/selection and applied to some other range/selection?

    Is there a way to do this using wordML?

    Tuesday, October 16, 2007 5:02 AM
  •  pagfloyd wrote:
    >>a style is a pre-defined set of formatting
    That's what I thought...so, is there no way to get the style (i mean 'font, 'size' etc.) of the current selection (or range) and set it to some other range?

     

    Note the qualifier pre-defined. The style must already exist AND the user must have applied it. Depending on the environment and type of document you need to process, you may not be able to guarantee this is the case. You tell us nothing about the scenario in which your application will be working, so we have to assume you're working with a default installation type of situation. Most users have no idea styles exist, much less use them...

     

    The best you can do for "quick and dirty" (as opposed to working through the attributes, one-by-one) is to use the Selection.CopyFormat / Selection.PasteFormat methods of the Word object model. These emulate the Format Painter (paint brush) tool in the Word UI; for details look them up in the Help. Play around with that a bit and see if it gives you what you're after. If you need both paragraph and character-level attributes, copy the paragraph formatting first, then the character formatting.


     pagfloyd wrote:

    In word 2003 application [not the api], there is a menu 'Format' which has an option 'Reveal Current Formatting' - Can't it be done programmatically for some range/selection and applied to some other range/selection?

    Is there a way to do this using wordML?

     

    No, you can't pick up what the RevealFormatting taskpane does. Yes, it could be done using WordProcessingML, that stores formatting attributes in "PR" elements, such as w:rPR (text run properties). WordProcessingML (actually, the entire discussion) is outside the scope of this forum. You should be able to find information and get assistance at OpenXMLDeveloper.org

    Tuesday, October 16, 2007 6:56 AM
  • >>The best you can do for "quick and dirty" (as opposed to working through the attributes, one-by-one) is to use the Selection.CopyFormat / Selection.PasteFormat methods of the Word object model

    tht's exactly what i was looking for! CopyFormat and PasteFormat - it did the job - thnx a lot, Cindy!

    btw, it's quick alright, but 'dirty!', how?
    Tuesday, October 16, 2007 7:21 AM
  • "quick and dirty" is just an expression. By it, I meant the simpler process, but one you can't control at a very fine level, so you might get unexpected results. Or, if you don't like the results, there's nothing you can do to change how it works.

     

    Glad it's what you were looking for :-)

    Tuesday, October 16, 2007 7:52 AM