呼叫由被叫方拒绝。 (从HRESULT异常:0x80010001(RPC

编程入门 行业动态 更新时间:2024-10-26 06:39:14
本文介绍了呼叫由被叫方拒绝。 (从HRESULT异常:0x80010001(RPC_E_CALL_REJECTED))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个使用Word.Interop采取单一邮件合并文档,复制的每个部分,贴节到它自己的文件,并单独保存一个小的C#WinForms应用程序。

我把(有时随机)收到错误消息:呼叫由被叫方拒绝。 (从HRESULT异常:0x80010001(RPC_E_CALL_REJECTED))。我测试了我的下面code,当我使用断点,我从来没有收到此消息。但是,如果我让它运行不羁,似乎在我行 oNewWord.ActiveDocument.Range错误了(0,0).Paste(); 。什么是更加古怪,有时我得到的异常消息符合市场预期,其他时间的处理似乎只是挂起来,当我preSS暂停在Visual Studio中,它显示我作为目前我的异常消息框线。

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

code:

公共无效MergeSplitAndReview()        {            尝试            {                //邮件合并模板                Word.Application oWord =新Word.Application();                Word.Document oWrdDoc =新Word.Document();                //新建文档实例                Word.Application oNewWord =新Word.Application();                Word.Document oNewWrdDoc =新Word.Document();                反对doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;                //文件必须为code激活可见()                oWord.Visible = TRUE;                oNewWord.Visible = TRUE;                对象oTemplatePath = docLoc;                对象oMissing = System.Reflection.Missing.Value;                //打开邮件合并模板                oWrdDoc = oWord.Documents.Open(oTemplatePath);                //打开新文件(空)                //注:我尝试编程开始一个新的Word文档,而不是打开一个exisitng空白,                //府发生了复制/粘贴操作时,格式化是路要走。下面的空白文档是                //通过采取FullMailMerge.doc的副本,清理出来,并将其保存,从而提供产生                //一种格式化的模板的。                串新建文档= projectDirectory +\\\\ NewDocument.doc                oNewWrdDoc = oNewWord.Documents.Open(新建文档);                //打开邮件合并数据源                oWrdDoc.MailMerge.OpenDataSource(docSource,oMissing,oMissing,oMissing,                   oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing);                //执行邮件合并(在完成邮件合并名为Letters1文档)                oWrdDoc.MailMerge.Execute();                INT docCnt = oWord.ActiveDocument.Sections.Count - 1;                INT CNT = 0;                而(CNT!= docCnt)                {                    CNT ++;                    字符串newFilename =;                    从邮件合并//复制所需的部分                    oWord.ActiveDocument.Sections [CNT] .Range.Copy();                    //将焦点设置到新的Word文档实例                    oNewWord.Activate();                    //复制粘贴至新的Word文档                    oNewWord.ActiveDocument.Range(0,0).Paste(); //这是这种地步,我得到时提到不使用断点的错误。                    的foreach(ListViewItem的LVI在lvData.Items)                    {                        如果(lvI.Checked)//获取首先检查LVI在lvData用于生成的文件名                        {                            updateAddrChngHistory(lvI.SubItems [18]。文本);                            串的filesys = lvI.SubItems [14] .Text.ToUpper();                            字符串memNo = lvI.SubItems [0]。文本;                            newFilename = +的filesys%+ memNo +%++%++%+函授大学%出局 - 鸣谢%+ DateTime.Now.ToString(YYYY-MM-DD- hh.mm.ss.ffffff)+的.doc;                            lvI.Remove(); //从列表视图的LVI用于newFilename删除                            打破; //打破了foreach循环                        }                    }                    //保存新Word文档                    oNewWord.ActiveDocument.SaveAs2(docTempDir + newFilename);                    //清除新的Word文档                    oNewWord.ActiveDocument.Content.Select();                    oNewWord.Selection.TypeBackspace();                }                //隐藏用于保存完整的邮件合并文档的每个部分我的新词实例                oNewWord.Visible = FALSE;                // MessageBox.Show(新表(){最顶层=真},完成后单击确定。);                MessageBox.Show(新表(){最顶层=真},完成后单击确定。);                oNewWord.ActiveDocument.Close(doNotSaveChanges); //关闭单个记录文档                oNewWord.Quit(); //关闭Word实例单个记录                oWord.ActiveDocument.Close(doNotSaveChanges); //关闭全部邮件合并文档(目前还在关闭模板文件)                // oWord.Documents.Open(docTempDir +FullMailMerge.doc);                oWord.Quit(doNotSaveChanges); //关闭邮件合并模板                的MessageBox.show(邮件合并已完成,节省了单独的文档,实例关闭。);            }            赶上(异常前)            {                LogException(除息);                MessageBox.Show(稿件来源:\\ t+ ex.Source +\\ n消息:\\ t+ ex.Message +\\ n数据:\\ t+ ex.Data);                //关闭所有Word过程                流程[] =流程Process.GetProcessesByName(WINWORD);                的foreach(在工艺过程VAR)                {                    process.Close();                }            }            最后            {            }        }

解决方案

的安德鲁理发的指出的是,我的方式引起的性能损失处理异常的时候。

和 的汉斯帕桑特的没有引用的文章提供的伟大的方式与选项3。

下面----会导致性能损失。

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

可能这个功能有助于重试

使用拉姆达(代表)为参数

用法1

VAR selectionLocal =选择;VAR范围= RunWithOutRejected(()=> selectionLocal.Range);

用法2

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

用法3

VAR nameLocal =名称;VAR书签= RunWithOutRejected(()=>   winWordControl   .GetDocument()   .Bookmarks.Add(nameLocal,范围));名称= RunWithOutRejected(()=> bookmark.Name);返回新KeyValuePair(姓名,书签);

PS:在使用互操作MSWORD此功能,code _application.Selection.PasteSpecial(); 失败

公共静态牛逼RunWithOutRejected< T>(Func键< T> FUNC)    {        VAR的结果=默认(T);        布尔hasException;        做        {            尝试            {                结果= FUNC();                hasException = FALSE;            }            赶上(收到COMException E)            {                如果(e.Error code == -2147418111)                {                    hasException = TRUE;                }                其他                {                    扔;                }            }            赶上(例外)            {                扔;            }        }而(hasException);        返回结果;    }}

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.

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?

CODE:

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:\t" + ex.Source + "\nMessage: \t" + ex.Message + "\nData:\t" + ex.Data); // Close all Word processes Process[] processes = Process.GetProcessesByName("winword"); foreach (var process in processes) { process.Close(); } } finally { } }

解决方案

As Andrew Barber point out that my way cause performance loss when handling exception.

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

use lambda (delegate) as parameter

Usage 1

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

Usage 2

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

Usage 3

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

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

本文发布于:2023-11-06 01:43:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1562404.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:异常   HRESULT   RPC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!