locked
Write a single line with two different formats to a word file with Office.Interop RRS feed

  • Question

  • User1262737873 posted

    Hi, I really am new to the interop. So far i've created a document and write paragraphs into a word document but cannot write a line with 2 different formats (bold, italic, etc.)

    Word.Paragraph oPara1;
    oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
    oPara1.Range.Text = "Heading 1";
    oPara1.Range.Font.Bold = 1;
    oPara1.Format.SpaceAfter = 24;    //24 pt spacing after paragraph.
    oPara1.Range.InsertParagraphAfter();


    I've found some results on the net but could not find a solution that i understand.

    Any help would be appreciated. Thanks.

    Tuesday, September 25, 2012 10:11 AM

Answers

  • User-741800283 posted

    Hi, you canuse like this,

    Word.Paragraph oPara = oDoc.Content.Paragraphs.Add(ref oMissing);
    oPara
    .Range.Text = "hello planet earth here's what I want to do";
    object oStart = oPara.Range.Start + 13;
    object oEnd = oPara.Range.Start + 18;

    Word.Range rBold = oDoc.Range(ref oStart, ref oEnd);
    rBold
    .Bold = 1;
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 27, 2012 4:57 AM

All replies

  • User-434868552 posted

    @ aykut.bulut     welcome to forums.asp.net

    Please note:  when it's not obvious how your question relates to ASP.NET, you should explicitly tell your peers here at forums.asp.net the connection to developing for ASP.NET; the focus of these forums is ASP.NET.  other forums such as stackoverflow are broader.

    regarding your question, i do not know the answer off the top of my head ...

    FWIW, the approach that i would take would be to use Word's macro recorder which generates VBA ... i would then clean up the VBA macro, if necessary -- once the VBA was working to my satisfaction, i would translate it to c#.

    g.

    Tuesday, September 25, 2012 12:41 PM
  • User-1910946339 posted

    You know that installing Office on a server is unsuported and will result in tears before bedtime, right?

    You don't seem to have any statements which refer to 'italic', could this be the reason that text is not appearing as italic?

     

     

     

     

     

     

     

    Never having used Word interop I would suggest

    oPara1.Range.Font.Italic = 1;

     

    Tuesday, September 25, 2012 6:25 PM
  • User-741800283 posted

    Hi, you canuse like this,

    Word.Paragraph oPara = oDoc.Content.Paragraphs.Add(ref oMissing);
    oPara
    .Range.Text = "hello planet earth here's what I want to do";
    object oStart = oPara.Range.Start + 13;
    object oEnd = oPara.Range.Start + 18;

    Word.Range rBold = oDoc.Range(ref oStart, ref oEnd);
    rBold
    .Bold = 1;
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 27, 2012 4:57 AM