-1

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();
        }
    }
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • Have you tried the OpenXml SDK? https://learn.microsoft.com/en-us/office/open-xml/how-to-open-and-add-text-to-a-word-processing-document – Murray Foxcroft Feb 18 '19 at 09:48
  • 1
    Quote : `var wordApplication = new Application()`. How do you think the compiler is supposed to know what application to start??.. – Siyon DP Feb 18 '19 at 09:49
  • I also think the issue will be around using the correct `Application` class. There is one by default, and there is another one inside the Office add-in. You need to specify the namespace most probably. (https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.word.application?view=word-pia) – iSpain17 Feb 18 '19 at 10:20
  • I had declared the Word application both in: using Application = Microsoft.Office.Interop.Word.Application; And in var wordApplication = new Word.Application() { Visible = true }; But same error – Larry Petshow Feb 18 '19 at 19:38
  • Have you searched the error ("googled it")? There are a number of "hits" that outline various possible causes. The majority are things you need to investigate in your programming environment. For example https://stackoverflow.com/questions/20638978/call-was-rejected-by-callee-exception-from-hresult-0x80010001-rpc-e-call-rej – Cindy Meister Feb 19 '19 at 15:33
  • cannot use my software so i really need some help with this – Larry Petshow Feb 20 '19 at 09:43

1 Answers1

0

I found it. Users must activate the Office products to work with this code.