Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

Creating Word Documents on the Fly
Substituting with the actual value
The following code opens the specified MS Word template document, substitutes the value and saves it as a new file. We defined a Bookmark named "TODAYDATE" in the document, which will be replaced with today's date.

<?php
//1. Instanciate Word
$word = new COM("word.application") or die("Unable to instantiate Word");
//2. specify the MS Word template document (with Bookmark TODAYDATE inside)
$template_file = "C:/reminder.doc";
//3. open the template document
$word->Documents->Open($template_file);
//4. get the current date MM/DD/YYYY
$current_date = date("m/d/Y");
//5. get the bookmark and create a new MS Word Range (to enable text substitution)
$bookmarkname = "TODAYDATE";
$objBookmark = $word->ActiveDocument->Bookmarks($bookmarkname);
$range = $objBookmark->Range;
//6. now substitute the bookmark with actual value
$range->Text = $current_date;
//7. save the template as a new document (c:/reminder_new.doc)
$new_file = "c:/reminder_new.doc";
$word->Documents[1]->SaveAs($new_file);
//8. free the object
$word->Quit();
$word->Release();
$word = null;
?>
That's it. Open the new document (c:/reminder_new.doc) and you will see today's date at the bookmark's location.
  1. Initially we instantiated the Word object using this code:

    <?php
    $word
    = new COM("word.application") or die("Unable to instanciate Word");
    ?>
  2. Then we specified the template document that contains the sample output and the Bookmark TODAYDATE:

    <?php
    $template_file
    = "C:/reminder.doc";
    ?>
  3. Next we opened the document:

    <?php
    $word
    ->Documents->Open($template_file);
    ?>
  4. Then we got today's date using date() function. The Bookmark TODAYDATE will be replaced with this value:

    <?php
    $current_date
    = date("m/d/Y");
    ?>
  5. Next we found the Bookmark in the document and created a new MS Word Range. Range is used to perform text substitution or insertion.

    <?php
    $bookmarkname
    = "TODAYDATE";
    $objBookmark = $word->ActiveDocument->Bookmarks($bookmarkname);
    $range = $objBookmark->Range;
    ?>
  6. Then we substituted the bookmark with actual value:

    <?php
    $range
    ->Text = $current_date;
    ?>
  7. Saved the template as a new document (c:/reminder_new.doc)

    <?php
    $new_file
    = "c:/reminder_new.doc";
    $word->Documents[1]->SaveAs($new_file);
    ?>
  8. Finally, we free the object:

    <?php
    $word
    ->Quit();
    $word->Release();
    $word = null;
    ?>

 
[ Next Page ]

[Page 1]  [Page 2]  


Comments:
thank youFra Orv01/27/09 07:39
RE: Inserting Line Breaksbobby.tables10/28/08 07:55
Word on server vs. mime-type tricksSteve09/18/07 10:00
Fatal error: com class not definedHans07/08/07 16:31
how to instantiate COMmienard lumad06/01/07 03:51
write word scriptSandr05/25/07 17:17
Inserting Line BreaksJon01/14/06 08:19
Getting Errorswetha10/28/05 06:13
RE: Pulling out MS Word document propertiesFeierabendbier07/25/05 06:39
create word 2000 filedinanath07/22/05 08:01
Anders Nielsen - RE: A better solution...Object OR .COM07/15/05 17:57
A better solution...Anders Nielsen02/20/05 00:37
RE: No Word on ServerSteven Vanderhoydonks02/08/05 04:32
ideas on parsing Dean01/26/05 12:16
Pulling out MS Word document propertiesRahul01/04/05 19:19
display a word document in html format with pDeepthi Valmikam11/28/04 02:21
generat .doc with tables?anna11/18/04 09:26
RE: Are there other options?Cristian Muraru06/01/04 08:43
RE: Are there other options?tibim03/02/04 15:50
import xhtmlnick02/26/04 17:09
why bother?madis01/04/04 10:40
RE: Are there other options?Mr. Mechano12/30/03 04:51
There is a better RTF-DOC-PDF exampleJon Ritchey12/29/03 14:15
PDF v. WORDsep12/23/03 16:33
RE: Are there other options?Khairul Amri Yunus12/20/03 03:30
RE: Are there other options?Yosh12/13/03 16:05
RE: Word on Server - Not recommended at allAlex12/11/03 16:03
Not as bad as it sounds....andrew taylor12/10/03 06:26
Templates in RTFJeroen Keppens12/09/03 03:42
RE: Are there other options?Eric Esquibel12/08/03 17:42
poor choice..Paul12/06/03 18:46
PDF a better choice for printingDrew F12/05/03 12:05
example using rtf... jon roig12/05/03 11:59
RE: doc poor choice for printingnightowl12/05/03 06:00
RE: Are there other options?nightowl12/05/03 05:58
No Word on ServerMartin Roesch12/04/03 07:38
ex vb programmer?Joe PHP12/03/03 16:28
doc poor choice for printingjohnnyv12/02/03 16:31
Are there other options?Luiz Guilherme12/02/03 14:22
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.