简体   繁体   English

如何使用单词表作为邮件合并数据源

[英]How to use a word table as mailmerge datasource

I have a word document containing a table I need to use as mailmerge datasouce.我有一个包含需要用作邮件合并数据源的表格的 Word 文档。 I've reached my goal by copying data of the word table to excel and using the sheet as datasource.我已经通过将单词表的数据复制到 excel 并将工作表用作数据源来实现我的目标。

Now my question is: is there a way to use the word table as datasource (I mean create a datasource using the data in the word table) without calling excel?现在我的问题是:有没有办法在不调用 excel 的情况下使用 word table 作为数据源(我的意思是使用 word table 中的数据创建数据源)?

I'm asking this because I need to use a word-vba macro and I'm trying to avoid opening an excel instance.我问这个是因为我需要使用 word-vba 宏,并且我试图避免打开 excel 实例。

edit编辑

I'll try to clarify what I'm looking for with the following pseudocode (I added a "What for this?" in wrong code lines):我将尝试使用以下伪代码阐明我正在寻找的内容(我在错误的代码行中添加了“这是什么?”):

Dim mDoc As Document
Dim mTbl As Table
Set mDoc = ActiveDocument
Set mTbl = mDoc.Tables(1)

Dim mDS As MailMerge.DataSource 'What for this?

For C = 1 To mTbl.Columns.Count
    'Add Fields
    Dim FieldName As String
    FieldName = mTbl.Cell(1, C).Range.Text
    mDS.Fields.Add (FieldName) 'What for this?
    'Add Values
    For R = 2 To mTbl.Rows.Count
        mDS.Fields(FieldName)(R) = mTbl.Cell(R, C).Range.Text 'What for this?
    Next R
Next C

In Word, start with a blank document, then add one table.在 Word 中,从一个空白文档开始,然后添加一个表格。 Enter the category in the top row (ie First Name, Last Name, etc).在顶行输入类别(即名字、姓氏等)。 Then add the data in the rows below.然后在下面的行中添加数据。 Then you should be able to use a statement like this to connect:然后你应该能够使用这样的语句来连接:

ActiveDocument.MailMerge.OpenDataSource Name:="Y:\Test\Word\MailmergeSource.docx", LinkToSource:=True, SubType:=wdMergeSubTypeOther

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

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