2
using Word = Microsoft.Office.Interop.Word;

public static void CreateWord()
{
    Word.Application objWord = new Word.Application();
    Word.Document objDoc = objWord.Documents.Add();

    Word.Paragraph objPara;
    objPara = objDoc.Paragraphs.Add();
    objPara.Range.Text = "Bold text aligned center";
    objPara.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
    objPara.Range.Bold = 1;
    objPara.Range.InsertParagraphAfter();

    objPara = objDoc.Paragraphs.Add();
    objPara.Range.Text = "Regular text aligned left";
    objPara.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
    objPara.Range.Bold = 0;
    objPara.Range.InsertParagraphAfter();

    objPara = objDoc.Paragraphs.Add();
    objPara.Range.Text = "Regular text aligned center\nwith something on the next line";
    objPara.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
    objPara.Range.Bold = 0;
    objPara.Range.InsertParagraphAfter();

    objPara = objDoc.Paragraphs.Add();
    objPara.Range.Text = "Regular text aligned left, with some Bold text here\nand some regular text on next line";
    objPara.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
    objPara.Range.Bold = 0;
    objPara.Range.InsertParagraphAfter();

    objWord.Visible = true;
    objWord.WindowState = Word.WdWindowState.wdWindowStateNormal;
}

The code above creates a Word document and writes text to it, but it looks like this: Word screenshot

I'm assuming that text in Microsoft.Office.Interop.Word is not supposed to act like that, or text should be added in some other way, but I'm failing to find any good information on how to do this.

So, my question is - how to add text from c# to a newly created word document with the right properties, so the result looks like this: Desired result

P.S. tips about creating tables using .Interop.Word would also be appreciated.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
KBOPYM
  • 33
  • 6
  • Possible duplicate of [How do I write bold text to a Word document programmatically without bolding the entire document?](https://stackoverflow.com/questions/11564073/how-do-i-write-bold-text-to-a-word-document-programmatically-without-bolding-the) – AndrewG Aug 21 '18 at 19:28
  • @AndrewG basically, add text and edit its format later, using its relative positions in string? That helps a lot. – KBOPYM Aug 21 '18 at 19:54
  • Yeah seems really bad Lol Personally maybe want to look around some more because if that's the solution its not the best. – AndrewG Aug 21 '18 at 20:29

1 Answers1

3

The key to working with content in Word documents is to understand how to work with Range objects. There's more than one way to achieve a goal of this nature. The following code snippet is my preference: collapsing the Range for each step (think of it like pressing the right-arrow key to "collapse" a selection to a point), rather than mixing Add, InsertAfter and similar methods.

For each change of formatting the Range must be "collapsed". So if you have bold in the middle of a line, that's a separate step. If numerous paragraphs should have the same formatting, they can be combined.

Environment.NewLine and \n can be used interchangeably.

Generally, assign text content, then format the Range.

    object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
    doc.Paragraphs.Add();
    Word.Range objRange = doc.Content;
    objRange.Collapse(ref oCollapseEnd);

    objRange.Text = "Bold text aligned center" + Environment.NewLine;
    objRange.Bold = 1;
    objRange.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
    objRange.Collapse(ref oCollapseEnd);

    objRange.Text = "Regular text aligned left" + Environment.NewLine;
    objRange.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
    objRange.Bold = 0;
    objRange.Collapse(ref oCollapseEnd);

    objRange.Text = "Regular text aligned center\nwith something on the next line" + Environment.NewLine;
    objRange.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
    objRange.Bold = 0;
    objRange.Collapse(ref oCollapseEnd);

    objRange.Text = "Regular text aligned left, with some ";
    objRange.Collapse(ref oCollapseEnd);
    objRange.Text = "Bold text here";
    objRange.Bold = 1;
    objRange.Collapse(ref oCollapseEnd);
    objRange.Text = "\nand some regular text on next line" + Environment.NewLine;
    objRange.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
    objRange.Bold = 0;
Cindy Meister
  • 25,071
  • 21
  • 34
  • 43