Replacing data in Word Doc MergeFields

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jkmglasgow
Forum Newbie
Posts: 2
Joined: Thu Feb 07, 2008 1:27 pm

Replacing data in Word Doc MergeFields

Post 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»:
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: Replacing data in Word Doc MergeFields

Post 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
jkmglasgow
Forum Newbie
Posts: 2
Joined: Thu Feb 07, 2008 1:27 pm

Re: Replacing data in Word Doc MergeFields

Post 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.
Post Reply