Jump to content


This topic is now archived and is closed to further replies.


Create Word Doc with PHP

Recommended Posts

I have the following codes written to create word document as per tutorial.
There are some modifications I did and when I tested apart from echoing all the data it doesn't generate the word doc and it doesn't give any errors either.
Is there anything I am missing to do? The server is installed with MS Word as well.
If this is not the right forum and I need to post this in COM forum, can someone please direct me to the right forum as this is the only forum I have been posting my problems?
thanks in advance

//This program is to generate datasheet

//Connect to Server and Database
include ("Connection.php");
//Get device number
$device = 2886;

//Minimum Insertion Loss Query
print "$device <br>\n";

$IL = @mysql_query("SELECT MIN(measured_parameter)
FROM test join test_data where test.test_number = test_data.test_number AND
test_descriptor = 'Min IL (dB)' AND tracked_component_number = '$device' AND attenuation = 0 GROUP BY temperature");
$row = mysql_num_rows($IL);
print "$row<br>";
$i = 0;
if ($i < $row) {
$value = mysql_fetch_row($IL);
$bmk_IL_0 = $value[0];
echo ("$bmk_IL_0 <br>");
$i = $i++;
if ($i < $row) {
$value = mysql_fetch_row($IL);
$bmk_IL_25 = $value[0];
echo ("$bmk_IL_25 <br>");
$i = $i++;
if ($i < $row) {
$value = mysql_fetch_row($IL);
$bmk_IL_70 = $value[0];
echo ("$bmk_IL_70 <br>");
// Instanciate Word
$word = new COM("word.application") or die("Unable to instantiate Word");
echo "$word\n";
// specify the MS Word template document
$dts_template = "/Inranet/departments/manufacturing/Customer_DataSheet_v2.doc";
// open the template document
//get the current date MM/DD/YYYY
$current_date = date("m/d/Y");
echo "$current_date\n";
//assign bookmark name to the varalbles
$bmk_date = "date";
$bmk_device = "device_sn";
$bmk_name = "name";

$bmk_IL_0 = "IL_0";
$bmk_IL_25 = "IL_25";
$bmk_IL_70 = "IL_70";
//create a new MS Word Range (to enable text substitution)
$dateobj = $word->ActiveDocument->Bookmarks($bmk_date);
$deviceobj = $word->ActiveDocument->Bookmarks($bmk_device);
$ilobj = $word->ActiveDocument->Bookmarks($bmk_IL_0);

$date_range = $dateobj->Range;
$device_range = $deviceobj->Range;
$il_range = $ilobj->Range;

//Substitute the bookmark with actual value
$date_range->Text = $bmk_date;
$device_range->Text = $bmk_device;

$il_range->Text = $bmk_IL_0;
//$il_range->Text = $bmk_IL_25;
//$il_range->Text = $bmk_IL_70;

//Save the template as a new document

$new_file = "/Intranet/departments/manufacturing/datasheet/'$device'_datasheet.doc";
//8. free the object
$word = null;


Share this post

Link to post
Share on other sites
I'm only guessing here but usually array indexes start at 0

Does this work?


Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.