呼叫被被叫方拒绝.(来自 HRESULT 的异常:0x80010001 (RPC_E_CALL_REJECTED)) [英] Call was rejected by callee. (Exception from HRESULT: 0x80010001 (RPC_E_CALL_REJECTED))

查看:219
本文介绍了呼叫被被叫方拒绝.(来自 HRESULT 的异常:0x80010001 (RPC_E_CALL_REJECTED))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小的 C# Winforms 应用程序,它使用 Word.Interop 来获取单个邮件合并文档,复制每个部分,将该部分粘贴到它自己的文档中,然后单独保存.

I have a small C# Winforms Application that is using Word.Interop to Take a Single Mail Merge Document, copy each section, paste that section into it's own document, and save it individually.

我一直(有时是随机地)收到错误消息:Call was denied by callee.(来自 HRESULT 的异常:0x80010001 (RPC_E_CALL_REJECTED)).我已经测试了下面的代码,当我使用断点时,我从未收到此消息.但是,如果我让它不受限制地运行,它似乎在我的行 oNewWord.ActiveDocument.Range(0, 0).Paste(); 处出错.更奇怪的是,有时我会按预期收到异常消息,有时处理似乎只是挂断了,当我在 Visual Studio 中按 PAUSE 时,它显示我当前在我的异常消息框行.

I keep (sometimes randomly) getting the error message: Call was rejected by callee. (Exception from HRESULT: 0x80010001 (RPC_E_CALL_REJECTED)). I have tested my below code and when I use breakpoints, I never receive this message. However, if I let it run uninhibited, it seems to error out at my line oNewWord.ActiveDocument.Range(0, 0).Paste();. What is even weirder, sometimes I get the Exception Message as expected, other times processing seems to just hang up and when I press PAUSE in Visual Studio, it shows me as currently at my Exception Message box line.

有人知道如何解决这个问题吗?

Anyone know how to resolve this?

代码:

public void MergeSplitAndReview()
        {
            try
            {
                // Mail Merge Template
                Word.Application oWord = new Word.Application();
                Word.Document oWrdDoc = new Word.Document();

                // New Document Instance
                Word.Application oNewWord = new Word.Application();
                Word.Document oNewWrdDoc = new Word.Document();

                object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;

                // Documents must be visible for code to Activate()
                oWord.Visible = true;
                oNewWord.Visible = true;

                Object oTemplatePath = docLoc;
                Object oMissing = System.Reflection.Missing.Value;

                // Open Mail Merge Template
                oWrdDoc = oWord.Documents.Open(oTemplatePath);

                // Open New Document (Empty)
                // Note: I tried programmatically starting a new word document instead of opening an exisitng "blank",
                //       bu when the copy/paste operation occurred, formatting was way off. The blank document below was
                //       generated by taking a copy of the FullMailMerge.doc, clearing it out, and saving it, thus providing
                //       a kind of formatted "template".
                string newDocument = projectDirectory + "\NewDocument.doc";
                oNewWrdDoc = oNewWord.Documents.Open(newDocument);

                // Open Mail Merge Datasource
                oWrdDoc.MailMerge.OpenDataSource(docSource, oMissing, oMissing, oMissing,
                   oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);

                // Execute Mail Merge (Opens Completed Mail Merge Documents Titled "Letters1")
                oWrdDoc.MailMerge.Execute();

                int docCnt = oWord.ActiveDocument.Sections.Count - 1;
                int cnt = 0;
                while (cnt != docCnt)
                {
                    cnt++;
                    string newFilename = "";

                    // Copy Desired Section from Mail Merge
                    oWord.ActiveDocument.Sections[cnt].Range.Copy();
                    // Set focus to the New Word Doc instance
                    oNewWord.Activate();
                    // Paste copied range to New Word Doc




                    oNewWord.ActiveDocument.Range(0, 0).Paste(); // THIS IS THE POINT WHERE I GET THE ERROR MENTIONED WHEN NOT USING A BREAKPOINT.



                    foreach (ListViewItem lvI in lvData.Items)
                    {
                        if (lvI.Checked) // Get first checked lvI in lvData to use for generating filename
                        {
                            updateAddrChngHistory(lvI.SubItems[18].Text);

                            string fileSys = lvI.SubItems[14].Text.ToUpper();
                            string memNo = lvI.SubItems[0].Text;

                            newFilename = fileSys + "%" + memNo + "%" + "" + "%" + "" + "%" + "CORRESPONDENCE%OUTGOING - ACKNOWLEDGEMENT%" + DateTime.Now.ToString("yyyy-MM-dd-hh.mm.ss.ffffff") + ".doc";

                            lvI.Remove(); // Delete from listview the lvI used for newFilename
                            break;        // Break out of foreach loop
                        }
                    }

                    // Save New Word Doc
                    oNewWord.ActiveDocument.SaveAs2(docTempDir + newFilename);
                    // Clear New Word Doc
                    oNewWord.ActiveDocument.Content.Select();
                    oNewWord.Selection.TypeBackspace();
                }
                // Hides my new word instance used to save each individual section of the full Mail Merge Doc
                oNewWord.Visible = false;
                // MessageBox.Show(new Form() { TopMost = true }, "Click OK when finished.");
                MessageBox.Show(new Form() { TopMost = true }, "Click OK when finished.");

                oNewWord.ActiveDocument.Close(doNotSaveChanges); // Close the Individual Record Document
                oNewWord.Quit();                                 // Close Word Instance for Individual Record
                oWord.ActiveDocument.Close(doNotSaveChanges);    // Close the Full Mail Merge Document (Currently ALSO closes the Template document)
                // oWord.Documents.Open(docTempDir + "FullMailMerge.doc");

                oWord.Quit(doNotSaveChanges);                    // Close the Mail Merge Template
                MessageBox.Show("Mail Merge Completed, Individual Documents Saved, Instances Closed.");
            }
            catch (Exception ex)
            {
                LogException(ex);
                MessageBox.Show("Source:	" + ex.Source + "
Message: 	" + ex.Message + "
Data:	" + ex.Data);
                // Close all Word processes
                Process[] processes = Process.GetProcessesByName("winword");
                foreach (var process in processes)
                {
                    process.Close();
                }
            }
            finally
            {

            }
        }

推荐答案

As Andrew Barber 指出我的方式在处理异常时会导致性能损失.

Hans Passant 引用的文章确实提供了一个很棒的选项 3.

And the article referenced by Hans Passant did provide a GREAT way with option 3.

----下面会造成性能损失

----below will cause performance loss

忙时,需要一段时间后重试.

when it is busy, need a retry after some period of time.

这个功能可以帮助重试吗

may this function be helpful to retry

使用 lambda(委托)作为参数

use lambda (delegate) as parameter

用法一

var selectionLocal = selection; 
var range = RunWithOutRejected(() => selectionLocal.Range);

用法 2

RunWithOutRejected(
   () =>
       following.Value.Range.FormattedText.HighlightColorIndex =
         WdColorIndex.wdGray25);

用法 3

var nameLocal = name;
var bookmark = RunWithOutRejected(() =>  
   winWordControl
   .GetDocument()
   .Bookmarks.Add(nameLocal, range));
name = RunWithOutRejected(() => bookmark.Name);
return new KeyValuePair(name, bookmark);

ps:使用该函数互操作MSword时,代码_application.Selection.PasteSpecial();失败

ps: when interop MSword using this function, the code _application.Selection.PasteSpecial(); failed

    public static T RunWithOutRejected<T>(Func<T> func)
    {
        var result = default(T);
        bool hasException;

        do
        {
            try
            {
                result = func();
                hasException = false;
            }
            catch (COMException e)
            {
                if (e.ErrorCode == -2147418111)
                {
                    hasException = true;
                }
                else
                {
                    throw;
                }
            }
            catch (Exception)
            {
                throw;
            }
        } while (hasException);

        return result;
    }
}

这篇关于呼叫被被叫方拒绝.(来自 HRESULT 的异常:0x80010001 (RPC_E_CALL_REJECTED))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆