Wednesday, June 20, 2007

Create Word Document Using PHP


//1. create the MS Word Object.s
$word = new COM("word.application") or die("Unable to instantiate Word");

//2. specify the MS Word template document (with Bookmark {firstname} inside)

$template_file = "template1.dot";

//3. open the template document

$word->Documents->Open($template_file);

//4. get the username from database.

$user_name = "Jignesh";

//5. get the bookmark and create a new MS Word Range (to enable text substitution)

$bookmarkname = "firstname";

$objBookmark = $word->ActiveDocument->Bookmarks($bookmarkname);

$range = $objBookmark->Range;

//6. now substitute the bookmark with actual value

$range->Text = $user_name;

//7. Write Image to word file.

$word->ActiveDocument->Shapes->AddPicture("logo.gif",TRUE,TRUE,100,100);

//8. save the template as a new document (c:/reminder_new.doc)

$new_file = "name2.doc";

$word->Documents[1]->SaveAs($new_file);

//9. free the object

$word->Quit();

$word = null;

echo "File Successfully Created.";


/* Check Bookmark available or not in the document.

if($word->ActiveDocument->Bookmarks->Exists($bookmarkname))
{
//then create a Range and perform the substitution

$objBookmark = $word->ActiveDocument->Bookmarks($bookmarkname);
$range = $objBookmark->Range;
//now substitute the bookmark with actual value
$range->Text = $current_date;

}
*/


?>

No comments: