使用excel 2010 vba创建一个包含多个单词文件的文本的单词文档 [英] use excel 2010 vba to create a word document that consists text from multiple word files

查看:247
本文介绍了使用excel 2010 vba创建一个包含多个单词文件的文本的单词文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我想要做的:




  • 我有一个Excel表格,用户可以选择一些关键字。

  • 对于每个关键字,都有一个单独的Word文档,名称相同,包含一些关于该关键字的文本。

  • 在做出选择后,我希望用户点击命令按钮,然后创建一个新的Word文档。

  • 这个新的Word文档将包含所选的关键字和相应的Word文档中的文本。生成的Word文档的样式和格式并不重要。将不同文本与不同文档分开的空白行将足够好。

  • 我想使用Late Binding这样做,因为我无法在VBA中添加引用(错误访问系统注册表)



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

解决方案

正如KazJaw所说,作为中间VBA用户您应该能够创建用户表单和相关代码,以便用户可以按照您的描述选择Word文档。一旦你使用Word文档,与Excel的编码有所不同。



让我分享一下我所知道的一些:



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



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

  Dim wdApp As Object 
Dim wd As Object

然后定义我创建的对象:

  On Error Resume Next 

设置wdApp = GetObject(,Word.Application)'建立单词应用程序

如果Err.Number<> 0然后
设置wdApp = CreateObject(Word.Application)
结束如果

错误GoTo 0

设置wd = wdApp.Documents。打开(C:\YourFilePath)'建立文件以使用

一旦你完成您可以使用可用的命令开始操作Word,所有这些命令都可以在网络上的其他位置找到,或使用编译器的提示(首先输入 Word.Application.ActiveDocument。,您将看到可用于操作该文档的函数列表)。这里有一些,我使用以前定义的变量 wd 来引用一个特定的文档:

  wd.Activate'激活word doc 
wd.PrintOut'打印单词doc
wd.FormFields(BundleNumber1)。Result = sBundleNumber'建立的表单域,数据存储在变量'sBundleNumber'
wd.Close'中关闭word doc

如果您正在选择文档的整个内容,我认为应该是相当的前进(像 Word.Application.ActiveDocument.SelectAllEditableRanges ,但如果你有要选择文档的子部分,您应该知道范围可以在Word中以与Excel中定义的方式大致相同的方式定义,但边缘并不像Excel中的单元格那样整齐。我相信它们由段落和休息时间,但你必须研究这是怎么回事完成:我从来没有这样做。



希望这将有助于您创建一个代码,然后可以被社区争取(如果有必要)。 p>

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)

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.

解决方案

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.

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

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:

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 wd to refer to a specific document:

                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.

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

这篇关于使用excel 2010 vba创建一个包含多个单词文件的文本的单词文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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