chrisfcarroll / MailMerge

Merge component for OfficeOpenXml .docx (i.e. Microsoft Word) documents
GNU General Public License v3.0
20 stars 7 forks source link

Feature: Listing all merge fields #3

Open apumex opened 4 years ago

apumex commented 4 years ago

It would be great if we have an api to list all the merge fields.

chrisfcarroll commented 4 years ago

I'm unlikely to work on this near-term, but I think the choices are:

1) Refactor the methods for SimpleMergeFields() and ComplexMergeFields() from https://github.com/chrisfcarroll/MailMerge/blob/master/MailMerge/KnownWordProcessingMLTransforms.cs so as to turn each method into two steps, the first of which would be 'list all the mergefields'

2) Be able to write an XPath query to replace the loop in ComplexMergeFields() with a single query. Then the XPath extension method at https://github.com/chrisfcarroll/MailMerge#extension-methods--helpers would make it a one liner. e.g. for simple merge fields the one liner starts with the XPath query:

var simpleFieldNames=stream
    .AsXPathDocOfWordprocessingMainDocument(isEditable)
    .SelectNodes("//w:fldSimple[contains(@w:instr,'MERGEFIELD ')]"
    , OoXmlNamespace.Manager);

And then AllMergeFields is the union of the SimpleMergeFields and the ComplexMergeFields