Page 1 of 1

Replacing data in Word Doc MergeFields

Posted: Thu May 15, 2008 2:25 pm
by jkmglasgow
:D I'm a complete newbie, this is what i'm trying to do.
Allow our users to hit a button on a page (done this part), which would allow them to save a word doc (doc is allready in the directory) and then print it, have the code to do this and it saves the doc on the client's machine, however when they press the button I need to get data from MySQL(know how to do this) in order to replace the Mergefields in the word doc, I have included a sample of the code and the word doc, can this be done without using the COM feature of MsWord,
any help would be greatly appreciated as I have no idea of the syntax to do this. Also I need to presevre the template to be used again, would this require saving the doc as a temp doc and sending the temp doc. :?:
Running Unix, Apache web server and PHP 5.0

Code: Select all

<?
require_once 'REnewalFTCE.doc';
$lname = "Smith";
$fname = "John";
$frstlstname = "John Smith";
header("Content-Type: application/msword");
header('Content-Disposition: attachment; filename="REnewalFTCE.doc"');
?>

Code: Select all

Partial Contents of word doc

school of Medicine
Faculty Agreement 2007-2008

Letter of Renewal
«TENURE», Clinician Educator Category

«FacultyName», «DEGREE»
Department of «DEPARTMENT»

Dear Dr. «LNAME»:

Re: Replacing data in Word Doc MergeFields

Posted: Thu May 15, 2008 5:53 pm
by yacahuma
You can use COM BUT, I believe, when you use COM interface for word, it actually create a link with an instance of microsoft word. Can you change your document to be rtf? rtf is recognized just as a word document, but is saved in text instead of binary

Re: Replacing data in Word Doc MergeFields

Posted: Tue May 20, 2008 12:03 pm
by jkmglasgow
I changed the file to rtf and was able to use the str_replace to alter the contents of the rtf file, and was able
to save it on the server under a different name and then send it to the user as a word document on their machine.