C# 以编程方式将 Word 文档插入现有文档 (Word 2007)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/689296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programatically insert a Word document into an existing document (Word 2007)
提问by Chris B
I have a Word 2007 document that I want to insert an exsiting Word document into - while preserving the header/footer, graphics, borders etc of both documents.
我有一个 Word 2007 文档,我想在其中插入一个现有的 Word 文档 - 同时保留两个文档的页眉/页脚、图形、边框等。
I'm doing this using the Word API in C#.
我正在使用 C# 中的 Word API 执行此操作。
It sounds pretty simple, I mean surely you just use the "InsertFile" method... except that in Word 2007 the "insert file" functionality now is actually "insert text from file" and it does just that - leaving out the page border, graphics and footer etc.
这听起来很简单,我的意思是你肯定只使用“InsertFile”方法......除了在 Word 2007 中,“插入文件”功能现在实际上是“从文件插入文本”,它就是这样做的 - 省略了页面边框,图形和页脚等。
OK then, I'll use copy and paste instead, like so...
好的,我将使用复制和粘贴来代替,就像这样......
_Document sourceDocument = wordApplication.Documents.Open(insert the 8 million by ref parameters Word requries)
sourceDocument.Activate(); // This is the document I am copying from
wordApplication.Selection.WholeStory();
wordApplication.Selection.Copy();
targetDocument.Activate(); // This is the document I am pasting into
wordApplication.Selection.InsertBreak(wdSectionBreakNextPage);
Selection.PasteAndFormat(wdFormatOriginalFormatting);
wordApplication.Selection.InsertBreak(wdSectionBreakNextPage);
which does what you would expect, takes the source document, selects everything, copies it then pastes it into the target document. Because I've added a section break before doing the paste it also preserves the borders, header/footer of both documents.
它按照您的预期执行,获取源文档,选择所有内容,将其复制然后将其粘贴到目标文档中。因为我在粘贴之前添加了一个分节符,它也保留了两个文档的边框、页眉/页脚。
However - now this is where I have the problem.The paste onlyincludes the borders, header etc if I paste at the endof the target document. If I paste it in the middle - despite there being a preceding section break, then only the text gets pasted and the header and borders etc are lost.
但是 -现在这是我遇到问题的地方。如果我粘贴在目标文档的末尾,则粘贴仅包括边框、标题等。如果我将它粘贴在中间——尽管前面有一个分节符,那么只有文本会被粘贴,标题和边框等会丢失。
回答by Matrim
Would the bookmark functionality work. The InsertFile contains parameters to take from this which may get around the problem. You may have considered this already though
书签功能是否有效。InsertFile 包含从中获取的参数,这可能会解决问题。你可能已经考虑过这个
http://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.bookmark.insertfile.aspx
http://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.bookmark.insertfile.aspx
回答by Chris J
I'm actually working on something similar at the moment strangely enough and found a powershell cmdlet library written in C# that you might find useful:
我实际上正在做一些类似的事情,奇怪的是,我发现了一个用 C# 编写的 powershell cmdlet 库,您可能会发现它很有用:
It is still a little buggy with inherited headers and footers as well as with image references not being copied correctly if the same image is in multiple parts of the document, but a lot of the structure is in place.
如果相同的图像位于文档的多个部分,继承的页眉和页脚以及图像引用没有被正确复制,这仍然是一个小问题,但是很多结构都已经到位。
回答by Universal Saint
TO INSERT A WORD DOCUMENT INTO ANOTHER WORD 2007 DOCUMENT The only way I found successful for WORD 2007 is... open the document you want to add the pages to, then goto "Insert" tab on the ribbon, look at the the "text" section (same place as text box, word art, etc) and select "object", a drop down menu appears, then select "Text from File". From here you simply select the document you want inserted and its done, you may need to make some slight adjustment, but it all there formatted correctly, be sure to place your cursor at the point you want the new stuff inserted. Hope this helps
将 WORD 文档插入另一个 WORD 2007 文档 我发现 WORD 2007 成功的唯一方法是...打开要添加页面的文档,然后转到功能区上的“插入”选项卡,查看“文本” ”部分(与文本框、艺术字等相同的位置)并选择“对象”,出现下拉菜单,然后选择“来自文件的文本”。从这里您只需选择要插入的文档并完成,您可能需要进行一些轻微的调整,但所有格式都正确,请确保将光标放在要插入新内容的位置。希望这可以帮助
回答by frs
This sort of worked for me. Still looking for a better solution. Be sure to add your reference to Interop.Word. I know this is an old thread and I am using Word 2016 but I searched for a long time and had to piece the solution together.
这对我有用。仍在寻找更好的解决方案。请务必添加对 Interop.Word 的引用。我知道这是一个旧线程,我使用的是 Word 2016,但我搜索了很长时间,不得不将解决方案拼凑在一起。
using Word = Microsoft.Office.Interop.Word;
var wordApp = new Word.Application();
wordApp.Visible = true;
wordApp.Documents.Add(@"C:\workingtemplate.dotx");
//Open is for an existing document.
//Add is to use a template.
//Get the range to be able to then collapse and have the correct insertion point
var rng = wordApp.ActiveDocument.Range();
rng.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
rng.InsertBreak(Word.WdBreakType.wdPageBreak);
rng.InsertFile(@"C:\temp.docx");