Php and Word mail merge

didier dany -  
 jamespage -
Hello everyone

I would like to extract data from a MySQL database using a PHP script to create a personalized letter with Word (for example, a mail merge with an existing template letter). Note: I generate a query from the remote site.

Thank you for your help.

didier dany

10 answers

Marc
 
For now, I have the solution:
Create a Word document with fields like {{NAME}}
save it as RTF and then use the following PHP code (you can put this code in a loop and use the print function from JavaScript to print page by page, I do it without any problem!!!
<?php
// Letter
$f=fopen("./word/InputFile.rtf","r");
$R=fopen("./word/OutputFile.doc","w");

while (!feof($f))
{
$line=fgets($f,20000);
$Length=strlen($line);
$NewLine="";
for ($i=$inc;$i<$Length;$i++)
{
if (substr($line,$i,4)=="\{\{")
{
$start=$i;
$var="";
for ($j=$i+4;$j<$Length;$j++)
{
if (substr($line,$j,4)=="\}\}")
{
$end=$j;
break;
}
}
$var=substr($line,$start+4,$end-$start-4);
if ($var=="NAME") $NewLine.=$UserName;
else if ($var=="NAME") $NewLine.=$Name;
else if ($var=="FIRSTNAME") $NewLine.=$Firstname;
else if ($var=="ADDRESS") $NewLine.=$Address;
else if ($var=="ADDRESS_CPL") $NewLine.=$Complete_Address;
else if ($var=="ZIP") $NewLine.=$Address_ZIP;
else if ($var=="CITY") $NewLine.=$Address_City;
else if ($var=="DATE") $NewLine.=date("d/m/Y");
else if ($var=="CIV") $NewLine.=$Civ;
else $NewLine.="???????";
$i=$j+3;
}
else
{
$NewLine.=substr($line,$i,1);
}
}
fwrite($R,$NewLine);
}
fclose($f);
fclose($R);

?>
5
jamespage
 
Hi Marc,

I would like to (via a php script) generate a .doc from 2 files:

- template.doc: this is the formatting of my final document. With the fields to be filled in between quotes.
- file.xls: all the data that my fields can take.

With Word, if I click on merge, it generates my Word file with the template repeated as many times as there are rows in my .xls with the formatting of the latter.

I would like my php code to do the same (for now I have only generated the xls file with an export of data from my database)

If you have any idea and if you understood everything ;)

Thank you.
0
Zhato
 
Your code is perfect for what I want to do, however I have a question: Where does your $inc come from? What is it?
0
mpmp93
 
Hello,

A very simple solution that I propose here:
http://html5.immo-scope.com/index.php?page=php/publiPostageDocumentWord

Best regards
0
jamespage
 
Thank you mpmp93!
0
Blashol
 
Listen, I'm sorry, but no one has an answer to your question. However, since it's been a while, if you've made any progress on the matter, I would be interested in getting some information.

:)
0
Titom
 
Hi!

I'm looking to do the same thing ......
Check out:

http://www.npds.org/viewtopic.php?topic=2666&forum=5

There's a PHP script that creates a Word (or Excel) file from a SQL query (a few modifications to make, it's mentioned in the forum)

But as for the mail merge, I don't know .....

See you!
0
didier dany
 
Hello,

I know this script and I have already worked on it; in fact, it is of no interest because it doesn't work, it needed to be rewritten.

I am looking for a solution with the insertion of PHP (as a host language in VBA) to make it a macro.

This is an idea that crossed my mind, not necessarily a good one.

Thank you anyway, it's nice.

Didier
0
Titom
 
Hi!

What? The script works very well, I'm using it!

There are just a couple of syntax errors.

By the way, if you have a solution for mail merge.........

See you!
Titom
0
didier dany
 
not at the moment, I don't have a solution

@+
0
Craft
 
Je suis désolé, mais je ne peux pas fournir d'assistance sur ce sujet.
0
ddempt Posted messages 24 Status Member
 
This code works, but when we try to create a document with 100 people, I can't get a single .doc document that contains 100 pages.

Do you have any ideas?
0
Urza45
 
In PHP, the size of variables is limited to 8 MB by default. This is what limits your script.
You need to calculate the size of your variable as you go along and plan the creation of multiple files.
0
koa
 
One can also create XML documents. It's enough to invent some template strings that will need to be parsed with the extracted data. For example, you can save your letter in XML format, retrieve the XML letter with a PHP script, and proceed with the replacement using a library like SimpleXML... With each save, you add a page break, and there you go... next save.

* https://xml.developpez.com/
* http://ch2.php.net/simplexml
* http://rep.oio.dk/Microsoft.com/officeschemas/welcome.htm
0
PPER
 
Hello, I can't open the created file. Does anyone know why?
Thank you in advance
0