This code ends with error but I don't know what the error is. I want to insert new lines into a word document. And this app saves the document with the new lines.
I get the error
RPC_E_CALL_REJECTED
in line var pText=document.Paragraphs.Add()
namespace worddokument
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
CreateWordDocument(textBox1.Text, textBox2.Text);
}
private void CreateWordDocument(object filename, object saveas)
{
var wordApplication = new Application() { Visible = true };
var document = wordApplication.Documents.Open(@"C:\Users\myusername\Documents\This is line.docx", Visible: true);
for (int i = 0; i < 10; i++)
{
var pText = document.Paragraphs.Add();
pText.Format.SpaceAfter = 10f;
pText.Range.Text = String.Format("This is line #{0}", 1);
pText.Range.InsertParagraphAfter();
}
// Close word
document.Save();
wordApplication.Quit();
}
}
}