none
How To Change Font in WordDocument.Content ? RRS feed

  • Question

  • Hi all, I'm still newbie word developer using C#. I've problem to change font in my current document content. This is my document format plan:

    ______________________________________________________________________________________

    Title (title has bold style with font name : verdana and size : 12)

    [image here]

    Image Anotation (image anotation has regular style with font name : verdana and size : 9)

    Content (content has regular style with font name : verdana and size : 10)

    ______________________________________________________________________________________

    Can't I formatting my document like that by using word.interop ? Thanks all.

    Thursday, May 19, 2011 6:36 AM

Answers

All replies

  • Hello,

     

    You can do that if you use Paragraphs, and Range.Font. You can find on this forum more information about this two. If you don`t find, I will give you a simple example later here.

     

    Hope it helps,

    Silviu.


    http://www.rosoftlab.net/
    Thursday, May 19, 2011 8:31 AM
  • Thanks Lupu. Can I get your simple example? I need a simple explanation about those things. Thanks.
    Thursday, May 19, 2011 8:45 AM
  • Hello

     

    First of all, the sample you gave here, means that in the document you have that structure repeating itself, or that is the hole document?

     

    Is this what you have?

    First paragraph: Title

    Second paragraph: [image here]

    Third paragraph: Image Anotation

    Rest of document: Content

     

    or in a single document there are multiple structures of this kind (if this is the case, Content has more than one paragraph?):

    First paragraph: Title

    Second paragraph: [image here]

    Third paragraph: Image Anotation

    Forth paragraph: Content

     

    Best regards,

    Silviu.


    http://www.rosoftlab.net/
    Thursday, May 19, 2011 12:57 PM
  • It's multiple structure like the second structure above, Silviu. Thanks ...
    Thursday, May 19, 2011 1:17 PM
  • Hi Lufti

    I'm taking a chance on muddying the waters, here, jumping in on Silviu...

    The best way to control formatting in Word (and how Word was designed to be used) is to use STYLES to control the formatting. This has two advantages:

    1. You can create the formatting set once, and re-use it.

    2. Changing formatting is simple. If formatting needs to be changed (the client says, "Oh, I changed my mind. I don't want Verdana any more, I want Arial") then you need change it in only one place: the style definition.

    So assume you set up three styles named theTitle, ImageAnnotation and Content. You've created the document and you're inserting the text, formatting as you go. Something like this will work:

    Dim rng as Word.Range = theDocument.Content;
    rng.Text = "The title text";
    object otheTitle = theTitle;
    rng.set_Style(ref otheTitle);
    object collapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
    rng.Collapse(ref collapseEnd);
    rng.Text = "The annotation";
    object oImageAnnotation = ImageAnnoation;
    rng.set_Style(ref oImageAnnotation);
    rng.Collapse(ref collapseEnd);
    //and so on...


    Cindy Meister, VSTO/Word MVP
    Thursday, May 19, 2011 1:18 PM
    Moderator
  • Hello Cindy,

     

    Any help is good help, so no problem for stepping in. The last question would be:

     

    Are the structures found in already existing documents, or are you generating them? If you are generating them, then Cindy`s example is the best way to approach this.

     

    Best regards,

    Silviu.


    http://www.rosoftlab.net/
    Thursday, May 19, 2011 1:23 PM
  • Ok. I'll try Cindy's example. Thanks all.
    Thursday, May 19, 2011 1:26 PM
  • Sorry guys. I'm not generating like Cindy example. I need to format my document content like the second Silviu structure. Can I look for Silviu example (Paragraph and Range.Font)  ? Thanks all.
    Thursday, May 19, 2011 4:23 PM
  • I've found simple format paragraph example on this page (http://social.msdn.microsoft.com/Forums/eu/worddev/thread/04f852fe-237d-4df3-a856-77c2a2d5d9d2). Thanks all.
    Friday, May 20, 2011 3:25 PM