如何在C#中合并两个Word文档对象(不在文件系统级别)?

时间:2019-05-05 15:13:47

标签: c# ms-word office-interop

我想在我的C#代码中将一个Word文档对象附加到另一个。

当我将两个文档都作为对象时,是否可以使用Word Interop Functions立即执行此操作?

我尝试了几种使用合并功能的方法,但是该功能期望文件名是字符串,没有对象。我不想保存两个文档并再次加载它们。

//schematic version of my code:

doc_template = wordapp.Documents.Open(ref filedocA,
                 ref readOnly, ref missing, ref missing, ref missing,
                 ref missing, ref missing, ref missing, ref missing,
                 ref missing);          
doc_final = wordapp.Documents.Add(ref missing, ref missing, ref missing, ref missing);
wordapp.Document tempdoc = null;

foreach (customer cust in dt_customers)
{
    tempdoc = doc_template;
    //Do some search and replace functions in tempdoc
    ...
    //here somehow the complete tempdoc should be appended to doc_final

}

doc_final应该包含每个客户的所有修改过的tempdoc

    //Update: Heres what i am doing now: Copying the contents within ranges
object start = tempdoc.Content.Start;
object end = tempdoc.Content.End; 
tempdoc.Range(ref start, ref end).Copy();
doc_final.Activate();
start = Destination.Content.End;
end = Destination.Content.End;


start = wordapp.ActiveDocument.Content.End - 1;
rng = Destination.Range(ref start, ref missing);
rng.Select();
rng.Paste();

但是有比简单复制文档内容更好的选择吗?

0 个答案:

没有答案