使用vba将一个word文档的内容复制到另一个word文档中 [英] Using vba to copy the contents of a word document into another word document

查看:662
本文介绍了使用vba将一个word文档的内容复制到另一个word文档中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经多年没有使用 VB,所以如果这很明显,请原谅我.我正在尝试编写一个 word vba 宏,以便在显示用户表单的模板中使用,然后根据用户表单导入 fileA.docx、fileB.docx 或 fileC.docx 的内容.(之后我将使用书签来填写一些表单数据,我不知道这是否相关).文件 A、B 和 C 将包含具有一些基本格式(例如列表)的文本,但没什么特别的.

I haven't used VB for years, so please forgive me if this turns out to be obvious. I'm trying to write a word vba macro for use in a template which will display a userform and then import the contents of fileA.docx, fileB.docx, or fileC.docx depending on the userform. (After that I'm going to use bookmarks to fill in some form data, I don't know if that's relevant). Files A, B, and C will contain text with some basic formatting such as lists, but nothing fancy.

我在网上看到的解决方案可以将文件的内容复制到一个新文件,但理想情况下,我想将其中一个文件的全部导入到我从模板中获取的当前未命名的新文件中.我认为我遇到问题的地方是将选择切换到这些文件之一,然后返回到新的未命名文档,尽管我可以用手确保我也正确复制.

The solutions I've seen online can copy the contents of file to a new file, but ideally I would like to import the entirety of one of those files into the new, currently unnamed file that I'm getting from the template. I think where I'm running into problems is with switching the selection to one of those files, and then back to the new unnamed document, though I could use a hand to make sure I'm copying correctly as well.

更新:我把事情弄得太难了,尽管这里的答案让我指出了正确的方向(谢谢!).最后我只做了

Update: I was making things too hard, though the answers here got me pointed in the right direction (thanks!). In the end I just did

ThisDocument.Activate

Selection.InsertFile("fileA")

它给了我我想要的一切的原始转储.

which gives me the raw dump of everything that I wanted.

推荐答案

使用这些命令,您可以在您使用的 Document 以及复制和粘贴元素之间切换:

Using commands such as these you can switch between which Document you're using and copy and paste elements:

ThisDocument.Activate 'Sets the main document active
Documents("Name.doc").Activate 'Activates another document

您可以使用复制命令在文档中插入、复制和粘贴内容.

You can insert, copy and paste things in and out of documents using copy commands.

ThisDocument.Range.InsertAfter("String") 'Insert text

Selection.WholeStory 'Select whole document
Selection.Expand wdParagraph 'Expands your selection to current paragraph
Selection.Copy 'Copy your selection
Documents("name.doc").Activate 'Activate the other document
Selection.EndKey wdStory 'Move to end of document
Selection.PasteAndFormat wdPasteDefault 'Pastes in the content

然后,您可以对其进行格式化,或使用之前的原始格式复制并粘贴它们.

You can then go and format such, or copy and paste them with original formatting from before.

这篇关于使用vba将一个word文档的内容复制到另一个word文档中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆