Jump to content


Photo

Create Word Doc with PHP


  • Please log in to reply
1 reply to this topic

#1 nirvana

nirvana
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 27 March 2006 - 07:09 PM

Hi,
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
Nirvana

<?php
//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
$word->Documents->Open($dts_template);
//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";
$word->Documents[1]->SaveAs($new_file);
//8. free the object
$word->Quit();
$word->Release();
$word = null;

?>


#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,016 posts

Posted 28 March 2006 - 06:28 PM

I'm only guessing here but usually array indexes start at 0

Does this work?

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

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users