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 :Fatal error: 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:19Stack 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 C:\wamp\www\SerfaApplications\components\com_chronocontact\libraries\customcode.php(51) : eval()'d code on line 19I 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"> Using COM to generate MsWord documents - Forums

Forums

Using COM to generate MsWord documents

V@lentine 26 Aug, 2010
Hello

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
GreyHead 26 Aug, 2010
Hi V@lentine,

The error is from this line
$word->Release();
and I think it means that the Release() method isn't being found in the class.

Can't say much more about that without seeing the class code.

I've had success creating Word files by saving valid HTML into a file. The pagination is handled automatically so that isn't a problem.

Bob
V@lentine 26 Aug, 2010
Thanks for your reply.
The issue of saving to a Word file does not suit my users needs, they'll use the Joomla application to edit important documents such as contracts, invoices...
Exporting directly saves time and the actual Access applications does it so the new one must too !

In the meantime, I manage to export my data using fpdf, but the problem is that the pdf documents cannot be modified...
I'm looking now for a means to add a commentary zone to my pdfs so that the users can add some remarks when they need or something like that.
If I manage,I'll keep fpdf !

Thanks for your kind support
nml375 26 Aug, 2010
Hi,
Reading up on the COM classes in php (http://www.php.net/manual/en/class.com.php) actually suggests that you should not use the Release() method. Instead, simply let the PHP GC (garbage collector) deal with the instantiated class once you're done with it.

<?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 = null;

?>


/Fredrik
V@lentine 27 Aug, 2010
Hi Fredrik !

I'll try again without the Release() function and tell you
V@lentine 27 Aug, 2010
Yes it works, except that I don't see Word opening though I have this
// définir la visibilité de l'application sur 0 (faux)
// pour ouvrir l'application au premier plan, utiliser 1 (vrai)
$word->Visible = 1;

But I think I'll find why🙂
"Thanks-a-lot-once-again"
This topic is locked and no more replies can be posted.