C# Word SaveAs RPC server is unavailable

0
private void CreateDocument(string date, string name, string phone, string father_name, string address, string village, string post, string taluka, string city, string district, string pincode, string product, int price)
        {
string currentPath = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
            // Get the Word application object.
            Word._Application word_app = new Word.Application();

                // Make Word visible (optional).
                word_app.Visible = true;

                // Create the Word document.
                object missing = Type.Missing;
                Word._Document word_doc = word_app.Documents.Add(
                    ref missing, ref missing, ref missing, ref missing);

                // Create a header paragraph.
                Word.Paragraph para = word_doc.Paragraphs.Add(ref missing);
                //para.Range.Text = "Chrysanthemum Curve";
                object style_name = "Heading 1";
                para.Range.set_Style(ref style_name);
                para.Range.InsertParagraphAfter();

            // Add more text.
                  para.Range.Text = "";
                para.Range.InsertParagraphAfter();

                // Save the current font and start using Courier New.
                string old_font = para.Range.Font.Name;
                para.Range.Font.Name = "Courier New";

            // Add the equations.
            para.Range.Font.Bold = 1;
            para.Range.Font.Size = 16;
                para.Range.Text = "EXPRESS PARCEL WITH COD" +"\v" + "ADVANCE PAYMENT" + "\v" + "CODE NO 560023100235" + "\v" + "BOOKING AT PBC BG 560046" + "\v" + "COD FOR RS " + price + "/ -" + "\v" + "PLEASE COLLECT CASH " + price + "/-" + "\v" + "(" + NumberToWords(price) + ")" + "\v" + "BILLER 5792" + Environment.NewLine + Environment.NewLine + Environment.NewLine;


                // Start a new paragraph and then
                // switch back to the original font.
                para.Range.InsertParagraphAfter();
            para.Range.Font.Name = "Courier New";
            para.Range.Font.Bold = 1;
            para.Range.Font.Size = 16;
            para.Range.Text = "TO\v" + name + father_name + "\v" + address + "\v" + village + "\v" + post + "\v" + taluka + "\v" + city + "\v" + district + "\v" + "PINCODE-" + pincode + "\v" + "MOBILE-" + phone + Environment.NewLine+Environment.NewLine;


            // Start a new paragraph and then
            // switch back to the original font.
            para.Range.InsertParagraphAfter();
            para.Range.Font.Name = "Times New Roman";
            para.Range.Font.Size = 12;
            para.Range.Text = "FROM" + "\v" + "AVK SHOPPING PVT LTD" + "\v" + "NO U16, BHUVANESHWARINAGAR" + "\v" + "MAGADI ROAD BANGALORE - 23" + Environment.NewLine+Environment.NewLine+ Environment.NewLine+ Environment.NewLine;

            // Start a new paragraph and then
            // switch back to the original font.           para.Range.InsertParagraphAfter();
            para.Range.Font.Name = "Times New Roman";
            para.Range.Font.Size = 12;
            para.Range.Text = "PARCEL CONTAINS" + " (" + product + ") " + "AND  THE PARCEL DO" + "\v" + "NOT CONTAIN ANY DANGEROUS ARTICLE PROHIBITED BY" + "\v" + "POSTAL REGULATIONS";


            Thread.Sleep(10000);
            //if(!(File.Exists(@currentPath + name + date + ".docx")))
            //{

            //}
            //else
            //{
            //    //File.Create(@currentPath + name + date + ".docx");

            //}
            string filename = name + "-" + phone + "-" + ".docx";
            MessageBox.Show(currentPath);
            object ob = @currentPath + filename;
            word_doc.SaveAs2(ref ob);
                MessageBox.Show("Document created successfully !");

            // Close.

            Marshal.ReleaseComObject(word_doc);
            Marshal.ReleaseComObject(word_app);
         }

The above code is working good but some times at word_doc.SaveAs2(ref ob);its giving error The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)' I already checked few suggestion but none of them solved my rpc issue.

Also once the .docx file was creating it is opening automatically. I don't want to open .docx once its created the file because my tool create 100 files at a time. If every doc opens my pc will use so many resources. (Simply I want to save the file silently).

This is windowsforms - vs2017 I am using.

c#
ms-word
asked on Stack Overflow Jan 3, 2019 by nav • edited Jan 4, 2019 by nav

2 Answers

0

If you don't properly release all the COM objects generated in the code then this can happen when code is executed more than once without going out of scope. The error indicates that the Application object (RPC Server) is still being used and therefore cannot be instantiated

The code orphans the object para, for example. So para can "block" the word_app object.

When using C# it's also a good idea to use specific objects instead of dot-notation so that they can be explicitly released, as well. (It's also more efficient.) For example, instead of para.Range.Font

Word.Range paraRange = para.Range;
Word.Font paraFont = paraRange.Font;
answered on Stack Overflow Jan 3, 2019 by Cindy Meister
-1

When you encounter this exception, have you closed the document? This severs the connection to Word. Since you encounter this occasionally rather than consistently (and you state that you don't want the documents to open in the first place) I suspect that this is the case.

For what it's worth, I'd recommend not using Word automation for this at all and just using the zipped XML that compose the docx files directly. Unless you have to explicitly support pre-2007 versions of Microsoft Word, I don't know why you couldn't do this given the constraints you posted. When we made that transition in 2007, the documents that required 45 minutes to create via automation took 15 seconds on the same hardware largely because the automation objects have to account for so many things outside of your specific use case.

answered on Stack Overflow Jan 3, 2019 by mattbbpl

User contributions licensed under CC BY-SA 3.0