使用 excel 2010 vba 创建一个包含来自多个 word 文件的文本的 word 文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18493028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 23:01:48  来源:igfitidea点击:

use excel 2010 vba to create a word document that consists text from multiple word files

excelvbaexcel-vbams-wordword-vba

提问by m.fi

This is what I am trying to do:

这就是我想要做的:

  • I have an Excel sheet where users can choose some keywords.
  • For each keyword, there is an individual Word document with the same name that contains some texts regarding the keyword.
  • After making their choices, I would like the users to click on a Command Button which will then create a new Word document.
  • This new Word document will contain the chosen keywords and texts from the correspondent Word document. Style and formating of the resulting Word document is not important. A blank line separating different text from different document will be good enough.
  • I would like to do this with Late Binding as I have trouble adding reference in VBA (Error Accessing the System Registry)
  • 我有一个 Excel 表,用户可以在其中选择一些关键字。
  • 对于每个关键字,都有一个同名的 Word 文档,其中包含有关该关键字的一些文本。
  • 做出选择后,我希望用户单击命令按钮,然后创建一个新的 Word 文档。
  • 这个新的 Word 文档将包含从相应的 Word 文档中选择的关键字和文本。生成的 Word 文档的样式和格式并不重要。将不同文本与不同文档分隔开的空行就足够了。
  • 我想使用后期绑定来做到这一点,因为我在 VBA 中添加引用时遇到问题(访问系统注册表时出错)

How should I start and where to look for examples? I have intermediate Excel VBA experience but completely unfamiliar with cross-application and Word properties.

我应该如何开始以及在哪里寻找示例?我有中级 Excel VBA 经验,但完全不熟悉跨应用程序和 Word 属性。

采纳答案by Instant Breakfast

As KazJaw said above, as an intermediate VBA user you should be able to create the userform and related code that allows your users to select the Word document as you describe. Once you get to working with the Word document things get a bit different from coding for Excel.

正如 KazJaw 上面所说,作为中级 VBA 用户,您应该能够创建用户表单和相关代码,允许您的用户按照您的描述选择 Word 文档。一旦您开始使用 Word 文档,事情就会与 Excel 编码有所不同。

Let me share the little I know about this:

分享一下我对这件事的了解:

First, make sure you've activated the Word Object Library: on the Tools menu, click References. In the list of available references, find and select the appropriate Microsoft Word Object Library

首先,确保您已激活 Word 对象库:在工具菜单上,单击引用。在可用参考列表中,查找并选择适当的 Microsoft Word 对象库

As I understand it, late binding just means declaring the object type when you assign the value. I have no idea if this will solve your 'Error Accessing the System Registry' issue. I have used late binding when I call Word Documents by first defining a variable as a generic object:

据我了解,后期绑定只是意味着在分配值时声明对象类型。我不知道这是否能解决您的“访问系统注册表时出错”的问题。通过首先将变量定义为通用对象来调用 Word 文档时,我使用了后期绑定:

Dim wdApp As Object
Dim wd As Object

Then defining the object(s) I created:

然后定义我创建的对象:

On Error Resume Next

    Set wdApp = GetObject(, "Word.Application") 'establishing the word application

    If Err.Number <> 0 Then
         Set wdApp = CreateObject("Word.Application")
    End If

On Error GoTo 0

    Set wd = wdApp.Documents.Open("C:\YourFilePath") 'establishing a file to use

Once you've done that, you can start manipulating Word with the commands available to you, all of which you should be able to find elsewhere on the web, or using the compiler's hints (start by entering Word.Application.ActiveDocument.for example and you will see a list of functions available for manipulating that document). Here are a few, with which I used a previously defined variable wdto refer to a specific document:

完成此操作后,您可以开始使用可用的命令操作 Word,所有这些命令您都应该能够在网络上的其他地方找到,或者使用编译器的提示(Word.Application.ActiveDocument.例如,首先输入,您将看到一个列表可用于操作该文档的功能)。这里有一些,我使用以前定义的变量wd来引用特定文档:

                wd.Activate 'activate the word doc
                wd.PrintOut 'printout the word doc
                wd.FormFields("BundleNumber1").Result = sBundleNumber 'fill in a pre-established form field with data stored in the variable 'sBundleNumber'
                wd.Close 'close the word doc

If you are selecting the entire content of the document, I think that should be fairly strait forward (something like Word.Application.ActiveDocument.SelectAllEditableRanges, but if you have to select a sub-section of the document you should know that ranges can be defined in Word in much the same way as they are defined in Excel, but the edges are not as neat as the cells in Excel. I believe they are defined by paragraphs and breaks, but you will have to research how this is done: I've never done it.

如果您要选择文档的整个内容,我认为这应该是相当严格的(类似于Word.Application.ActiveDocument.SelectAllEditableRanges,但是如果您必须选择文档的一个子部分,您应该知道可以在 Word 中以大致相同的方式定义范围就像它们在 Excel 中定义的那样,但边缘不像 Excel 中的单元格那么整齐。我相信它们是由段落和中断定义的,但您必须研究这是如何完成的:我从来没有这样做过。

Hope this will be of help to you creating a code that can then be wrangled (if necessary) by the community.

希望这对您创建一个代码有帮助,然后社区可以讨论(如果需要)。