C# 尝试将文本从一个Word文档复制到另一个Word文档时类型不匹配?

C# 尝试将文本从一个Word文档复制到另一个Word文档时类型不匹配?,c#,.net,winforms,interop,office-interop,C#,.net,Winforms,Interop,Office Interop,下面是我当前的代码。在我到达第ONEWORD.ActiveDocument.Range.FormattedText=rng.FormattedText行之前,执行工作正常;我在哪里捕捉到类型不匹配的异常?此时,我要做的是复制邮件合并文档的第一节页面,这是邮件合并的第一条记录,并将其放入新的Word文档实例中,以另存为自己的文档 因此,如果我为邮件合并文档选择了4条记录,这将为我提供一个名为Letters Form1的单字文档,共4页,每个记录收件人一封信。不知何故,我需要做的是将这4个页面中的每

下面是我当前的代码。在我到达第ONEWORD.ActiveDocument.Range.FormattedText=rng.FormattedText行之前,执行工作正常;我在哪里捕捉到类型不匹配的异常?此时,我要做的是复制邮件合并文档的第一节页面,这是邮件合并的第一条记录,并将其放入新的Word文档实例中,以另存为自己的文档

因此,如果我为邮件合并文档选择了4条记录,这将为我提供一个名为Letters Form1的单字文档,共4页,每个记录收件人一封信。不知何故,我需要做的是将这4个页面中的每一个都保存到它们自己的Word文档中,以便于存档和索引

在Word互操作方面有更多经验的人能帮我吗

public void OpenAndReview()
        {
            try
            {
                string docSave = "C:\\Users\\NAME\\Desktop\\Test.doc";

                //MergeDocLibrary mdl = new MergeDocLibrary();
                //mdl.mergeDocument(docSource, docLoc);

                // Original Mail Merge Document
                Word.Range rng;
                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();

                // Set 'False' in PROD, 'True' in DEV
                oWord.Visible = true;
                oNewWord.Visible = true;
                Object oTemplatePath = docLoc;

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

                // Open New Document
                oNewWrdDoc = oNewWord.Documents.Open(docSave);
                Object oMissing = System.Reflection.Missing.Value;

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

                MessageBox.Show(oWord.ActiveDocument.Name.ToString());
                MessageBox.Show(oWord.ActiveDocument.Sections.Count.ToString());

                // Execute Mail Merge
                oWrdDoc.MailMerge.Execute();

                // Set Mail Merge Document as Active Doc
                //oWrdDoc.Activate();

                MessageBox.Show(oWord.ActiveDocument.Name.ToString());
                MessageBox.Show(oWord.ActiveDocument.Sections.Count.ToString());

                // Select Section 1 of Mail Merge Doc?
                rng = oWord.ActiveDocument.Sections[1].Range;


                // ERROR! - Type Mismatch
                // Place selected text into the new Document???
                oNewWord.ActiveDocument.Range().FormattedText = rng.FormattedText;


                // Save new docuemnt...?
                oNewWrdDoc.SaveAs2("SuccesfullySavedTest.doc");

            }
            catch (Exception ex)
            {
                MessageBox.Show("Source:\t" + ex.Source + "\nMessage: \t" + ex.Message + "\nData:\t" + ex.Data);
            }
            finally
            {
                //


 }
        }

我和你的问题完全一样。 对我有效的是使用这个代码

range = wordApp.ActiveDocument.Sections[1].Range;
range.Copy();

//Special pasting onto new word document
newDocument.Content.PasteSpecial(DataType: Word.WdPasteOptions.wdKeepSourceFormatting);

FormattedText只能在同一Word应用程序中设置。如果两个文档处于不同的Word应用程序中,则会失败。不要使用单字,而只使用单字。

这只是一个友好的提示,您可以在字符串前面加上@符号,例如@C:\Users\etc而不是C:\\Users\\etcHey,谢谢!我不知道。关于我的类型匹配问题有什么想法吗?