Inspecting a Word mail merge data source programmatically

Posted by Guy Marom on Stack Overflow See other posts from Stack Overflow or by Guy Marom
Published on 2009-03-23T06:57:40Z Indexed on 2012/06/10 4:40 UTC
Read the original article Hit count: 229

Filed under:
|
|
|

I want to iterate over all rows of a MS-Word mail merge data source and extract the relevant data into an XML.

I'm currently using this code:

Imports Microsoft.Office.Interop
Do
  objXW.WriteStartElement("Recipient")
  Dim objDataFields As Word.MailMergeDataFields = DataSource.DataFields
  For Each FieldIndex As Integer In mdictMergeFields.Keys
    strValue = objDataFields.Item(FieldIndex).Value
    If Not String.IsNullOrEmpty(strValue) Then
      strName = mdictMergeFields(FieldIndex)
      objXW.WriteElementString(strName, strValue)
    End If
  Next
  objXW.WriteEndElement()
  If DataSource.ActiveRecord = LastRecord Then
    Exit Do
  Else
    DataSource.ActiveRecord = Word.WdMailMergeActiveRecord.wdNextDataSourceRecord
  End If
Loop

And it turns out to be a little sluggish (About 1 second for each row). Is there any way to do it faster?

My fantasy is finding a function like MailMergeDataSource.ToDatatable and then inspecting the datatable.

© Stack Overflow or respective owner

Related posts about vb.net

Related posts about interop