Bouke / docx-mailmerge

Mail merge for Office Open XML (docx) files without the need for Microsoft Office Word.
MIT License
273 stars 104 forks source link

How to manipulate a if conditional mail merge field in docx-mailmerge #78

Open schinnaswamy opened 4 years ago

schinnaswamy commented 4 years ago

We want to show certain text in the word document based on mail merge field conditions. Basically trying to make use of if conditional merge fields.

Let say we have different annexure documents attached with the main document. The annexures vary based on the client's terms and conditions.

For Example:

If a client's document has 2 annexures in the main document text should load with dynamic text like

This agreement contains the following annexures Annexure A Annexure B

If a client's document has 4 annexures in the main document text should load with dynamic text like

This agreement contains the following annexures Annexure A Annexure B Annexure C Annexure D

I am trying to add the if conditional merge field in the template as follows ======== Doc template =======

This agreement contains the following annexures

{ IF {MERGEFIELD AnnexA} - "Y" "Annexure A" ""}
{ IF {MERGEFIELD AnnexB} - "Y" "Annexure B" ""}
{ IF {MERGEFIELD AnnexC} - "Y" "Annexure C" ""}
{ IF {MERGEFIELD AnnexD} - "Y" "Annexure D" ""}

Expected Behavior

The AnnexA, AnnexB, AnnexC & AnnexD should be returned when getMergeFields() method is invoked. Instead, the method doesn't recognize if conditional mail merge fields

Please share your thoughts

sujayramaiah commented 4 years ago

Hi,

We are also experiencing the same issue. The IF condition does not seem to get evaluated. Is this feature not supported? Is there any workaround? Please advise.

Thanks, Sujay

diegolimaf commented 4 years ago

Hi,

I am facing the same issue now. I want to include some IF conditions in my template but it does not work as expected. Did you find any solution?

Thanks

sujayramaiah commented 4 years ago

Hi @diegolimaf,

We came up with a workaround. Basically reformatted the data to handle the conditional structure outside of mail merge and then fed it into the mail merge. Within the template there will be both form field for condition A or B in the same block. If A has a value, B will not have a value in the restructured data we are feeling in.

Please let me know if you need any further information.

Thanks, Sujay

O7lo commented 3 years ago

hi @sujayramaiah ,

i am also facing this issue right now and i would love to have more information about your workaround. This could be really helpfull to me.

thanks for your help