且构网

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

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

更新时间:2023-12-04 14:06:35

当您发现自己使用现有文档作为创建新文档的基础时,就该考虑将该文档另存为Word Template 了. .这是一种可能包含的特殊类型的文件(* .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.

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

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.