以编程方式将 Word 文档插入现有文档 (Word 2007)

发布于 2024-07-15 13:34:16 字数 1007 浏览 6 评论 0原文

我有一个 Word 2007 文档,我想将现有的 Word 文档插入其中,同时保留两个文档的页眉/页脚、图形、边框等。

我正在使用 C# 中的 Word API 执行此操作。

听起来很简单,我的意思是你肯定只需使用“InsertFile”方法...... 除了在 Word 2007 中,“插入文件”功能现在实际上是“从文件插入文本”,它就是这样做的 - 省略页面边框、图形和页脚等。

好吧,我将使用复制和粘贴,例如所以...

_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);

它执行您所期望的操作,获取源文档,选择所有内容,复制它然后将其粘贴到目标文档中。 因为我在粘贴之前添加了分节符,所以它还保留了两个文档的边框、页眉/页脚。

但是 - 现在这就是我遇到问题的地方。如果我粘贴在目标的末尾,粘贴包含边框、标题等文档。 如果我将它粘贴在中间 - 尽管前面有分节符,那么只有文本被粘贴,标题和边框等都会丢失。

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.

I'm doing this using the Word API in C#.

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.

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 only includes the borders, header etc if I paste at the end of 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

耳根太软 2024-07-22 13:34:16

书签功能可以使用吗? InsertFile 包含从中获取的参数,这可能会解决该问题。 您可能已经考虑过这一点

http ://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.bookmark.insertfile.aspx

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

http://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.bookmark.insertfile.aspx

呆头 2024-07-22 13:34:16

这对我有用。 仍在寻找更好的解决方案。 请务必添加对 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");

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.

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");
戏剧牡丹亭 2024-07-22 13:34:16

实际上,我目前正在研究类似的东西,奇怪的是,我发现了一个用 C# 编写的 powershell cmdlet 库,您可能会发现它很有用:

Powertools for Open XML

如果同一图像位于文档的多个部分中,但继承的页眉和页脚以及图像引用无法正确复制,仍然存在一些问题,但很多结构都在地方。

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:

Powertools for Open XML

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.

孤单情人 2024-07-22 13:34:16

将一个 WORD 文档插入另一个 WORD 2007 文档
我发现 WORD 2007 成功的唯一方法是......
打开要添加页面的文档,然后转到功能区上的“插入”选项卡,查看“文本”部分(与文本框、艺术字等相同的位置)并选择“对象”,一个下拉菜单出现菜单,然后选择“文件中的文本”。 从这里,您只需选择要插入的文档即可完成,您可能需要进行一些细微的调整,但所有格式都正确,请务必将光标放在要插入新内容的位置。 希望这可以帮助

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文