且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将 Word 文档的内容从 Excel VBA 复制到新创建的 Word 文档

更新时间:2023-12-04 14:33:07

当您发现自己使用现有文档作为创建新文档的基础时,是时候考虑将该文档保存为 Word 模板.这是一种特定类型的文件(*.dotx 或 *.dotm),可能包含

When you find yourself using an existing document as the basis for creating new documents it's time to consider saving that document as a Word Template. This is a particular type of file (*.dotx or *.dotm) that may contain

  • 样板文本
  • 根据需要插入带有额外样板的构建块
  • 样式(格式化命令集)
  • 自定义键盘快捷键
  • 功能区自定义
  • VBA 代码(宏,仅在 *.dotm 中)

由模板生成的所有文档将继承和/或共享.

that will be inherited and/or shared by all documents generated from the template.

为了从模板创建新文档,请使用 Documents.Add 方法,指定路径和文件名:

In order to create a new document from a template, use the Documents.Add method, specifying the path and file name:

Dim wdDoc as Word.Document
Set wdDoc = Documents.Add(path & filename) 
'From outside Word: Set wdDoc = wdApplication.Documents.Add

这将从模板创建一个新文档,复制完整的模板内容而不影响模板本身.

This will create a new document from the template, copying the complete template content without affecting the template, itself.

注意:您还可以将普通文档"(*.docx) 与 Documents.Add 方法一起使用,它会复制内容.但是,它不会链接回该文档,因此无法共享宏.

Note: You can also use a "plain document" (*.docx) with the Documents.Add method and it will copy the content. It will not, however, link back to that document so it can't share macros, for instance.