将 Word 文档的内容从 Excel VBA 复制到新创建的 Word 文档 [英] Copying contents of Word doc to newly created Word doc from excel VBA

查看:75
本文介绍了将 Word 文档的内容从 Excel VBA 复制到新创建的 Word 文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 excel 数据对 Word 文档执行各种任务.但是,这将被其他人使用,所以我想将 word doc(以及格式和表格)中的所有内容复制到一个新的 word doc 并编辑新的 word 文档,以防止其他人不小心将第一个(模板一个)).我还想关闭模板(第一个)文档并保持第二个(新)文档处于打开状态.

I am trying to use excel data to perform various tasks on a word document. However, this will be used by other people, so I wanted to copy all contents from the word doc (and formatting and tables) to a new word doc and edit the new one to prevent others from accidentally saving over the first one (template one). I also want to close the template (first) doc and keep the second (new) one open.

我是 VBA 新手,无法理解如何处理这个问题.经过一堆谷歌搜索,我编译了这个(不工作)代码:

I am new to VBA and am having trouble understanding how to handle this. After a bunch of googling, I've compiled this (not working) code:

Dim WordApp As Object
Dim WordDoc As Word.Document
Dim NewWordDoc As Word.Document
'Dim other stuff'

'-------------------------------------------------------------------
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = True
Set WordDoc = WordApp.Documents.Open("C:UsersWindowsDocumentsCRC Labels 
test.doc")
Set NewWordDoc = WordApp.Documents.Add 'I think this gives an error'
'------------------------------------------------------------------------
'Line that actives WordDoc'
Selection.WholeStory 'Select whole document
Selection.Expand wdParagraph 'Expands your selection to current paragraph
Selection.Copy 'Copy your selection
'Line that closes WordDoc'
'Line that activates NewWordDoc'
Selection.PasteAndFormat wdPasteDefault 'Pastes in the content
'------------------------------------------------------------------------
'Do stuff with NewWordDoc'


'------------------------------------------------------------------------
Set WordDoc = Nothing
Set WordApp = Nothing

处理这种情况还有其他想法吗?感谢您的回复.

Any other ideas of handling this situation? Thank you for your response.

推荐答案

当您发现自己使用现有文档作为创建新文档的基础时,是时候考虑将该文档保存为 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.

这篇关于将 Word 文档的内容从 Excel VBA 复制到新创建的 Word 文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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