简体   繁体   English

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

[英]C# Word SaveAs RPC server is unavailable

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); 上面的代码运行良好,但有时在word_doc.SaveAs2(ref ob); its giving error The RPC server is unavailable. 其给定错误RPC服务器不可用。 (Exception from HRESULT: 0x800706BA)' I already checked few suggestion but none of them solved my rpc issue. (来自HRESULT的异常:0x800706BA)'我已经检查了很少的建议,但是没有一个解决了我的rpc问题。

Also once the .docx file was creating it is opening automatically. 同样,一旦创建了.docx文件,它就会自动打开。 I don't want to open .docx once its created the file because my tool create 100 files at a time. 我不想打开.docx一旦创建了文件,因为我的工具一次创建了100个文件。 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. 这是windowsforms-我正在使用vs2017。

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. 如果您没有正确释放代码中生成的所有COM对象,则在不超出范围的情况下执行代码多次时可能会发生这种情况。 The error indicates that the Application object (RPC Server) is still being used and therefore cannot be instantiated 该错误表明Application对象(RPC服务器)仍在使用中,因此无法实例化

The code orphans the object para , for example. 例如,代码使对象para孤。 So para can "block" the word_app object. 因此para可以“阻止” word_app对象。

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. 使用C#时,最好使用特定的对象而不是点号,以便也可以显式地释放它们。 (It's also more efficient.) For example, instead of para.Range.Font (它也更有效。)例如,代替para.Range.Font

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

When you encounter this exception, have you closed the document? 遇到此异常时,您是否关闭了文档? This severs the connection to Word. 这将切断与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. 对于它的价值,我建议完全不要使用Word自动化,而应该使用直接构成docx文件的压缩XML。 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. 除非您必须明确支持2007年之前的Microsoft Word版本,否则由于发布的限制,我不知道为什么您不能这样做。 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. 当我们在2007年进行过渡时,在同一硬件上通过自动化创建需要45分钟的文档花费了15秒,这在很大程度上是因为自动化对象必须考虑您特定用例之外的许多事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM