I need to export my form's result into a Word document. I could use PDF but my users need to add text to that document.
Generally I use a html template in which I put my data like this :
$content=file_get_contents('etats/AP.htm');
$content=str_replace('$valeur1',$valeur1,$content);
$content=str_replace('$valeur2',$valeur2,$content);
$filename='fichier'.doc';
touch($filename);
if (is_writable($filename)) {
if (!$handle = fopen($filename, 'a')) {
echo "Impossible d'ouvrir le fichier ($filename)";
exit;
}
if (fwrite($handle, $content) === FALSE) {
echo "Impossible d'écrire dans le fichier ($filename)";
exit;
}
echo "<a href='$filename'>Télécharger le fichier</a>";
fclose($handle);
} else {
echo "Le fichier $filename n'est pas accessible en écriture.";
}
But I can't do that now because I don't know how many pages I will get, and I want the file to open automatically.
Search on the web I sawmany people use COM components to do that. I tried a code displayed on developpez.net :
<?php
echo header('Content-Type: application/msword');
// Crée une instance word
$word = new COM('word.application');
// Appelle word au premier plan
$word->Visible = 1;
// Ajoute un texte au document
$word->Documents->Add();
$word->Selection->TypeText('Ceci est un test...');
// Enregistre le document sous test.doc
$word->Documents[1]->SaveAs('test.doc');
//Quitte et ferme word
$word->Quit();
$word->Release();
?>
I tried on my Chronoform (onSubmit area) and I got a index.php file opened with Word telling me strange things like this :
<br />
<b>Fatal error</b>: Uncaught exception 'com_exception' with message 'Error [0x80020003] Membre introuvable.
' in C:\wamp\www\SerfaApplications\components\com_chronocontact\libraries\customcode.php(51) : eval()'d code:19
Stack trace:
#0 C:\wamp\www\SerfaApplications\components\com_chronocontact\libraries\customcode.php(51) : eval()'d code(19): com->Release()
#1 C:\wamp\www\SerfaApplications\components\com_chronocontact\libraries\customcode.php(51): eval()
#2 C:\wamp\www\SerfaApplications\components\com_chronocontact\libraries\chronoform.php(465): CFCustomCode->runCode('onsubmitcode')
#3 C:\wamp\www\SerfaApplications\components\com_chronocontact\chronocontact.php(125): CFChronoForm->submitForm('PlanAttPresence', Array, false)
#4 C:\wamp\www\SerfaApplications\components\com_chronocontact\chronocontact.php(62): uploadandmail('PlanAttPresence')
#5 C:\wamp\www\SerfaApplications\libraries\joomla\application\component\helper.php(162): require_once('C:\wamp\www\Ser...')
#6 C:\wamp\www\SerfaApplications\includes\application.php(124): JCo in <b>C:\wamp\www\SerfaApplications\components\com_chronocontact\libraries\customcode.php(51) : eval()'d code</b> on line <b>19</b><br />
I first thought it was due to the "header" line of the code, so I commented it, but I got the same errors, only displayed on screen instead of Word document.
I have absolutely no idea who this "Membre introuvable" is ! Or do I use a joomla keyword in my code ?
Can anyone help and explain a poor french girl ?!
Thanks