C#Word另存为RPC服务器不可用

时间:2019-01-03 19:25:29

标签: c# ms-word

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);
         }

上面的代码运行良好,但有时在word_doc.SaveAs2(ref ob);处出现错误 RPC服务器不可用。 (来自HRESULT的异常:0x800706BA)。我已经检查了很少的建议,但是都没有解决我的rpc问题。

创建.docx文件后,它也会自动打开。我不想打开.docx一旦创建了文件,因为我的工具一次创建了100个文件。如果每个文档都打开,我的电脑将使用很多资源。 (只是我想安静地保存文件)。

这是windowsforms-我正在使用vs2017。

2 个答案:

答案 0 :(得分:0)

如果没有正确释放代码中生成的所有COM对象,则在不超出范围的情况下执行代码多次时可能会发生这种情况。该错误表明Application对象(RPC服务器)仍在使用中,因此无法实例化

例如,代码孤立了对象para。因此para可以“阻止” word_app对象。

使用C#时,最好使用特定的对象而不是点符号,以便也可以显式地释放它们。 (它也更有效。)例如,代替para.Range.Font

Word.Range paraRange = para.Range;
Word.Font paraFont = paraRange.Font;

答案 1 :(得分:-1)

遇到此异常时,您是否关闭了文档?这将切断与Word的连接。由于您偶尔而不是始终遇到这种情况(并且您声明首先不希望打开文档),我怀疑是这种情况。

对于它的价值,我建议完全不要使用Word自动化,而应该使用直接构成docx文件的压缩XML。除非您必须明确支持2007年之前的Microsoft Word版本,否则由于发布的限制,我不知道为什么您不能这样做。当我们在2007年进行过渡时,在相同的硬件上通过自动化创建需要45分钟的文档花费了15秒,这在很大程度上是因为自动化对象必须考虑您特定用例之外的很多事情。