senny / sablon

Ruby Document Template Processor based on docx templates and Mail Merge fields.
MIT License
446 stars 127 forks source link

MailMerge field doesn't work in a Hyperlink field #48

Closed maggielei closed 6 years ago

maggielei commented 7 years ago

In my context object, I have a key called 'insights_url' whose value is a dynamic url. I'm trying to use this 'insights_url' mail merge field in my Microsoft Word docx template. I've been trying to do this for days, searching through pages of Google search results, with no answers. I just want a hyperlink that says "VIEW ALL" and the url would be a mail merge field. The mail merge field for 'insights_url' works fine on its own, but I need it to say "VIEW ALL".

I've tried all these steps listed in: http://stackoverflow.com/questions/17428891/add-variable-hyperlink-in-mail-merge-in-word-2013

Basically, create a Hyperlink field, edit the field codes to throw in a mail merge field. It looks like: { HYPERLINK "{ MERGEFIELD =insights_url \* MERGEFORMAT}" \* MERGEFORMAT }

So I've tried that, and multiple variations-- using the hyperlink dropdown rather than inserting a field, adding a bookmark after, and more. They always end up with a broken hyperlink that cannot be opened, or are empty. I've also tried to unzip the doc, edit the document.xml.rels file to change the target of a working hyperlink to the mail merge field. However, it seems making any edits and re-zipping, will result in a corrupt file.

Just wondering, is there an easier way to do this? Like support for hyperlinked mail merge fields? Thank you.

senny commented 7 years ago

Hi @maggielei

Thanks for reaching out. Sablon is not specifically supporting Hyperlinks at this time so it's definitely possible that this is not working as expected. You could work on a patch to add this functionality to Sablon.

@stadelmanma did you have any plans to do links on HTML insertion? Or is your project in need to use Sablon templates with Hyperlinks?

stadelmanma commented 7 years ago

@maggielei @senny Currently our workflow doesn't involve hyperlinks at all so I didn't plan on adding support for an <a> tag.

Reading that stack overflow answer made it seem like native mail merge support was shaky at best.

I saved a test document and unzipped it to see how MS Word handled hyperlinks and it looks like an HTML insertion would be the best way. You would use the href attribute to set the target in document.xml.rels and the actual content between the <a>...</a> to set up the w:hyperlink node in document.xml. However, the bigger issue is that currently HTML insertion replaces the entire paragraph which would be less than desirable in this case.

I don't know how hard it would be to allow "inline" elements <a>, <b>, <span>, etc. to replace an individual run instead.

maggielei commented 7 years ago

@senny @stadelmanma Thank you both for your quick responses! I'll be subscribing to #47 for more updates and in the meantime I'll try to see if I can come up with my own solution. Thanks again.

stadelmanma commented 6 years ago

@maggielei hyperlink creation is now possible on the master branch via HTML content insertion. However, it currently comes with the limitation that the entire paragraph must be replaced. Inline HTML/WordML insertion is something I am working on now.

stadelmanma commented 6 years ago

@maggielei See here for a method to insert HTML hyperlinks into a document without replacing the entire paragraph.