|
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.
- Initially we instantiated the Word object using this code:
<?php
$word = new COM("word.application") or die("Unable to instanciate Word");
?>
- Then we specified the template document that contains the sample output and
the Bookmark TODAYDATE:
<?php
$template_file = "C:/reminder.doc";
?>
- Next we opened the document:
<?php
$word->Documents->Open($template_file);
?>
- 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");
?>
- 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;
?>
- Then we substituted the bookmark with actual value:
<?php
$range->Text = $current_date;
?>
- Saved the template as a new document (c:/reminder_new.doc)
<?php
$new_file = "c:/reminder_new.doc";
$word->Documents[1]->SaveAs($new_file);
?>
- Finally, we free the object:
<?php
$word->Quit();
$word->Release();
$word = null;
?>
[ Next Page ]
| Comments: | ||
| thank you | Fra Orv | 01/27/09 07:39 |
| RE: Inserting Line Breaks | bobby.tables | 10/28/08 07:55 |
| Word on server vs. mime-type tricks | Steve | 09/18/07 10:00 |
| Fatal error: com class not defined | Hans | 07/08/07 16:31 |
| how to instantiate COM | mienard lumad | 06/01/07 03:51 |
| write word script | Sandr | 05/25/07 17:17 |
| Inserting Line Breaks | Jon | 01/14/06 08:19 |
| Getting Error | swetha | 10/28/05 06:13 |
| RE: Pulling out MS Word document properties | Feierabendbier | 07/25/05 06:39 |
| create word 2000 file | dinanath | 07/22/05 08:01 |
| Anders Nielsen - RE: A better solution... | Object OR .COM | 07/15/05 17:57 |
| A better solution... | Anders Nielsen | 02/20/05 00:37 |
| RE: No Word on Server | Steven Vanderhoydonks | 02/08/05 04:32 |
| ideas on parsing | Dean | 01/26/05 12:16 |
| Pulling out MS Word document properties | Rahul | 01/04/05 19:19 |
| display a word document in html format with p | Deepthi Valmikam | 11/28/04 02:21 |
| generat .doc with tables? | anna | 11/18/04 09:26 |
| RE: Are there other options? | Cristian Muraru | 06/01/04 08:43 |
| RE: Are there other options? | tibim | 03/02/04 15:50 |
| import xhtml | nick | 02/26/04 17:09 |
| why bother? | madis | 01/04/04 10:40 |
| RE: Are there other options? | Mr. Mechano | 12/30/03 04:51 |
| There is a better RTF-DOC-PDF example | Jon Ritchey | 12/29/03 14:15 |
| PDF v. WORD | sep | 12/23/03 16:33 |
| RE: Are there other options? | Khairul Amri Yunus | 12/20/03 03:30 |
| RE: Are there other options? | Yosh | 12/13/03 16:05 |
| RE: Word on Server - Not recommended at all | Alex | 12/11/03 16:03 |
| Not as bad as it sounds.... | andrew taylor | 12/10/03 06:26 |
| Templates in RTF | Jeroen Keppens | 12/09/03 03:42 |
| RE: Are there other options? | Eric Esquibel | 12/08/03 17:42 |
| poor choice.. | Paul | 12/06/03 18:46 |
| PDF a better choice for printing | Drew F | 12/05/03 12:05 |
| example using rtf... | jon roig | 12/05/03 11:59 |
| RE: doc poor choice for printing | nightowl | 12/05/03 06:00 |
| RE: Are there other options? | nightowl | 12/05/03 05:58 |
| No Word on Server | Martin Roesch | 12/04/03 07:38 |
| ex vb programmer? | Joe PHP | 12/03/03 16:28 |
| doc poor choice for printing | johnnyv | 12/02/03 16:31 |
| Are there other options? | Luiz Guilherme | 12/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. | ||


