简体   繁体   English

使用excel 2010 vba创建一个Word文档,该文档包含多个Word文件中的文本

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

This is what I am trying to do: 这就是我想要做的:

  • I have an Excel sheet where users can choose some keywords. 我有一个Excel工作表,用户可以在其中选择一些关键字。
  • For each keyword, there is an individual Word document with the same name that contains some texts regarding the keyword. 对于每个关键字,都有一个单独的具有相同名称的Word文档,其中包含有关该关键字的一些文本。
  • After making their choices, I would like the users to click on a Command Button which will then create a new Word document. 做出选择后,我希望用户单击“命令按钮”,然后将创建一个新的Word文档。
  • This new Word document will contain the chosen keywords and texts from the correspondent Word document. 这个新的Word文档将包含从相应的Word文档中选择的关键字和文本。 Style and formating of the resulting Word document is not important. 生成的Word文档的样式和格式并不重要。 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) 我想通过后期绑定来完成此操作,因为我在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属性。

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. 正如KazJaw所说,作为VBA中级用户,您应该能够创建用户窗体和相关代码,以使您的用户可以选择所描述的Word文档。 Once you get to working with the Word document things get a bit different from coding for Excel. 一旦您开始使用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. 首先,确保已激活Word对象库:在“工具”菜单上,单击“引用”。 In the list of available references, find and select the appropriate Microsoft Word Object Library 在可用引用列表中,找到并选择适当的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 Documents时使用了后期绑定:

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). 完成此操作后,您可以开始使用可用的命令来操作Word,您应该可以在Web上的其他位置找到这些命令,也可以使用编译器的提示(例如,首先输入Word.Application.ActiveDocument.您将看到可用于处理该文档的功能列表。 Here are a few, with which I used a previously defined variable wd to refer to a specific document: 以下是一些wd ,我使用它们使用先前定义的变量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.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. 希望这会对您创建代码,然后由社区进行整理(如有必要)有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM