Categories > OpenTBS with DOCX >

Ampersand in creating MS Word Document is corrupting the file

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Dee Dee
Date: 2013-07-13
Time: 22:49

Ampersand in creating MS Word Document is corrupting the file

I have a title such as "JOURNAL & PROCEEDINGS OF THE ASIATIC SOCIETY OF BENGAL" that I am attempting to place into a MS Word template such as this:

$TBS = new clsTinyButStrong(); // new instance of TBS
$TBS->Plugin(TBS_INSTALL, OPENTBS_PLUGIN);
$TBS->LoadTemplate($templatePath, OPENTBS_ALREADY_UTF8);

$TBS->MergeBlock('a', array(array("title" => "JOURNAL & PROCEEDINGS OF THE ASIATIC SOCIETY OF BENGAL")));
$TBS->Show(OPENTBS_FILE, $filesName);

Doing this causes the title to be stripped down to "JOURNAL &amp" when placed in the document.xml file.  I have tried to replace the & with & in my code as well as & but TBS still strips it down.  Any ideas?
Thanks!
By: Sarah Kemp
Date: 2013-07-15
Time: 17:52

Re: Ampersand in creating MS Word Document is corrupting the file

You can use parameter strconv=no to prevent TBS from changing your string, but the ampersand may still corrupt the document if Word does not like it. In that case I would try putting an ampersand into a fresh Word document, zipping it, and looking at the document.xml to see how Word represents it.
By: Skrol29
Date: 2013-07-16
Time: 15:59

Re: Ampersand in creating MS Word Document is corrupting the file

Hi Dee Dee,

I cannot reproduce your problem.
All is merged correctly for me using your snippet.
My template is just a [a.title;block=tbs:row] inside a cell of a Word Table.

Are you sure your title is not fetched from a database or a file ?
By: Dee Dee
Date: 2013-07-16
Time: 16:32

Re: Ampersand in creating MS Word Document is corrupting the file

I had reduced my code down to try to simplify the problem.  However, I forgot that my template uses a comparison which seems to be causing the problem.  The TBS documentation says this about fields in the template: "If the value contains spaces, semicolons or quotes, then you can use single quotes as delimiters."  I believe this is what is actually causing the problem.  For example, if I have an array like this:

array("name" => '', "title" => "JOURNAL & PROCEEDINGS OF THE ASIATIC SOCIETY OF BENGAL");

then the & confuses my template due to the semicolon.  The text terminates with "JOURNAL &amp" but also corrupts my file.

Here is what I have in my template:
[a.name;if [val] =’’; then [a.title];else ‘[a.name]’; block=tbs:row]

I've gone down the path of single-quoting the & as both '&' or &amp';' as the documentation suggests.  The first results in the title being JOURNAL '&' PROCEEDINGS OF THE ASIATIC SOCIETY OF BENGAL, which is incorrect since it shouldn't have the quotes around the ampersand.  The latter still causes corruption.

I need the comparison because if the name is empty, the title should come first and not leave a blank space.  Is there a way to do this without the use of comparisons?
By: Dee Dee
Date: 2013-07-16
Time: 17:27

Re: Ampersand in creating MS Word Document is corrupting the file

I did some more reading and understand that I can use 'magnet' for my solution instead which clears up this problem for me:

[a.name;block=tbs:row;magnet=w:p]

[a.title]