user3165438
user3165438

Reputation: 2661

Insert text to Word document

I would like to insert text to Word document, in a specified format, using Interop.Word:

Something like this :

wordDoc.InsertText("Text \n", "Arial");  

or

wordDoc.InsertText("Text \n", "Bold");

Is it possible?

Upvotes: 0

Views: 1041

Answers (1)

Coder Panda
Coder Panda

Reputation: 678

There is no direct method like this AFAIK. However, you can write a wrapper over Word Interop and do it. Internally inside your InsertText() method you will have to do the following.

1: Use the Text property of a Range object to insert text in a document.

object start = 0;
object end = 12; 
Word.Range rng = this.Range(ref start, ref end); 
rng.Text = "New Text"; 
rng.Select();

2: Format text using a document-level customization.

// Set the Range to the first paragraph. 
Word.Range rng = this.Paragraphs[1].Range;
// Change the formatting. 
rng.Font.Size = 14; 
rng.Font.Name = "Arial"; 
rng.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
rng.Select();

For further details please see this and this which I have used sometime back with good results.

Hope this helps.

Upvotes: 1

Related Questions