none
Appending text with multiple formats to cell in MS word table using C# RRS feed

  • Question

  • Hi all,

    How can i write 3 lines with 3 different formates in a cell in MS word table using C#,like this:

    this is the required cell:

    Some text ---> Font = Arial ,size=20

    Some text ---> Font = Times new roman, size=8

    Some text ----> Font = another font , size=8

    PLease guys any help is appreciated,

    Any Suggestions?


    Thanks & Regards, ---------------- Ahmed ElGamal
    Thursday, December 29, 2011 2:44 PM

Answers

  • Hi Ahmed

    You need to work with the Range object. Your code could look something like this:

    Word.Range rng = cellObject.Range;
    object oCollapseStart = Word.WdCollapseDirection.wdCollapseStart;
    object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
    rng.Collapse(ref oCollapseStart);
    rng.Text = "First text ";
    rng.Font.Name = "Arial";
    rng.Font.Size = 20;
    rng.Collapse(ref oCollapseEnd);
    rng.Text = "Second Text ";
    rng.Font.Name = "Times New Roman";
    rng.Font.Size = 8;
    rng.Collapse(ref oCollapseEnd);
    rng.Text = "Third text";
    rng.Font.Name = "Calibri";

    Please note, however, that "best practice" with Word would be to create styles to hold the formatting and apply them as required, rather than using what we call "direct formatting".


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by ahmed.elgamal Saturday, December 31, 2011 9:19 AM
    Thursday, December 29, 2011 5:21 PM
    Moderator

All replies

  • Hi Ahmed

    You need to work with the Range object. Your code could look something like this:

    Word.Range rng = cellObject.Range;
    object oCollapseStart = Word.WdCollapseDirection.wdCollapseStart;
    object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
    rng.Collapse(ref oCollapseStart);
    rng.Text = "First text ";
    rng.Font.Name = "Arial";
    rng.Font.Size = 20;
    rng.Collapse(ref oCollapseEnd);
    rng.Text = "Second Text ";
    rng.Font.Name = "Times New Roman";
    rng.Font.Size = 8;
    rng.Collapse(ref oCollapseEnd);
    rng.Text = "Third text";
    rng.Font.Name = "Calibri";

    Please note, however, that "best practice" with Word would be to create styles to hold the formatting and apply them as required, rather than using what we call "direct formatting".


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by ahmed.elgamal Saturday, December 31, 2011 9:19 AM
    Thursday, December 29, 2011 5:21 PM
    Moderator
  • Hi Ahmed

    As a moderator I can see the posts you delete :-) One thing that I can see in the code you post is that you aren't using a RANGE object, as I show you in the sample code I posted. You MUST use and manipulate a RANGE object in order to do what you want to do. It can't be done just using the Range property of a Paragraph object.

    You're trying to work at much too high a level, rather than with individual objects within the document.

    Starting with the first line of code in the last message you deleted to get the starting Range object in my sample:

    Word.Range rng = oDoc.Content.Paragraphs.Add(oTable.Cell(1,1).Range);


    Cindy Meister, VSTO/Word MVP
    Saturday, December 31, 2011 8:33 AM
    Moderator
  • thanks so much sir,

    it works fine,

    one last question is there any place where i can learn more about word automation?  cause i can't find examples in MSDN library.

    thnx,


    Thanks & Regards, ---------------- Ahmed ElGamal
    • Marked as answer by ahmed.elgamal Saturday, December 31, 2011 9:19 AM
    • Unmarked as answer by ahmed.elgamal Saturday, December 31, 2011 9:19 AM
    Saturday, December 31, 2011 9:19 AM
  • Hi ahmed

    <<one last question is there any place where i can learn more about word automation?  cause i can't find examples in MSDN library.>>

    Mmm, there's not a lot, and most of what there is is in a VB-language since VBA is Word's "native" programming language.

    You'll find some high-level information in this article and the articles to which it links:
     http://support.microsoft.com/kb/311452

    If you go to this page, then drill down through the Office [version number]/Word [version number] there's a section "Technical articles" that will go into what can be done with technology new for that version. These usually contain quite a bit of sample code as well as discussion that can help you understand how Word works. It can help to go back "into history" a ways as things that are "main stream" now were new at one point.

    This is also where you'll find the Developer Reference, which also contains sample code (mostly VBA, but that can be informative for C# as far as object model goes).

    The PIA section will be more the kind of documentation to which you're accustomed, but it doesn't really contain any code samples.


    Cindy Meister, VSTO/Word MVP
    Saturday, December 31, 2011 9:49 AM
    Moderator