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

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

问题描述

我正在尝试使用Excel数据对Word文档执行各种任务.但是,这将被其他人使用,因此我想将所有内容从单词doc(以及格式和表格)复制到一个新的单词doc并进行编辑,以防止其他人意外地在第一个(文档模板)上进行保存. ).我还想关闭模板(第一个)文档,并保持第二个(新)文档打开.

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:\Users\Windows\Documents\CRC 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 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.

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

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