简体   繁体   中英

How to use a word table as mailmerge datasource

I have a word document containing a table I need to use as mailmerge datasouce. I've reached my goal by copying data of the word table to excel and using the sheet as datasource.

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?

I'm asking this because I need to use a word-vba macro and I'm trying to avoid opening an excel instance.

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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