簡體   English   中英

呼叫被被叫方拒絕了。 (來自HRESULT的異常:0x80010001(RPC_E_CALL_REJECTED))

[英]Call was rejected by callee. (Exception from HRESULT: 0x80010001 (RPC_E_CALL_REJECTED))

我有一個小的C#Winforms應用程序,它使用Word.Interop獲取單個郵件合並文檔,復制每個部分,將該部分粘貼到它自己的文檔中,並單獨保存。

錯誤

我保持(有時隨機)收到錯誤消息: Call was rejected by callee. (Exception from HRESULT: 0x80010001 (RPC_E_CALL_REJECTED)) Call was rejected by callee. (Exception from HRESULT: 0x80010001 (RPC_E_CALL_REJECTED)) 我測試了下面的代碼,當我使用斷點時,我從未收到過這條消息。 但是,如果我讓它運行不受限制,它似乎在我的行oNewWord.ActiveDocument.Range(0, 0).Paste(); 什么是更奇怪的,有時我得到預期的消息,其他時候處理似乎只是掛起,當我在Visual Studio中按PAUSE時,它顯示我當前在我的異常消息框行。

有誰知道如何解決這個問題?

碼:

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
            {

            }
        }

正如安德魯巴伯指出的那樣,處理異常時我的方式會導致性能下降

Hans Passant引用的文章確實為選項3提供了一個很好的方式。

----以下會導致性能下降

當它很忙時,需要在一段時間后重試。

這個功能可能有助於重試

使用lambda(委托)作為參數

用法1

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:當interop MSword使用這個函數時,代碼_application.Selection.PasteSpecial(); 失敗


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

我有同樣的問題,從辦公室2010升級到辦公室2016(贏得10 64位),我的問題:單詞不是編輯文檔的默認程序,所以我從“控制面板\\所有控制面板項目”作為默認程序\\默認程序\\設置默認程序“,它解決了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM