Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP - Create word doc file from Access database text 1

Status
Not open for further replies.

bigcat48

Programmer
Aug 27, 2008
72
US
All - I want to create a word doc file from text pulled from my Access 2003 database.

Once figured out I also have a question on formatting this doc as well.

Can anyone help?


Thanks in advance,

BC
 
Well, I had an identical question on opening HTML document using excel from within PHP scripts.

jpadie suggested I use COM. I searched around and found this page


Ironically, the article actually shows how to create a word document from within PHP, add text to the document ...

As far as formatting, word handles HTML pretty well. I would suggest you create your content using standard HTML tags and then feed it to word using sample as per above linked article.

good luck!


--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Thank you 'southbeach', this helps alot.

In addition, I want to be able to display my database text in th word doc but it would only save my a default id instead of the id that I choose.

Confused please help.
 
I am not sure what you mean by "default ID" and "the ID I choose".

Could you please elaborate? Also, sample snippet/code will help as one can evaluate your intended results versus your written code.

Regards,


--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Sorry didn't explain my issue very well.

I have a table setup with a column for action buttons.
One of the buttons is a word icon. Once clicked, you it would perform the word doc task (create, add text, close doc) based upon the row id.

Example:
row 1 = company ABC
row 2 = company XYZ

The problem I am having is that once I click on the icon for that row of data the (let's say row 1 = id 1) it would perform the word doc task for id=4 instead of id=1.


Here is the code
Code:
<?php
// create database connection
include("includes/connection.php");
  
// call data to be viewed
$sql="SELECT * FROM tblProfileList ORDER BY tblProfileList.companyName ASC";

// submit the query
$rs=odbc_exec($conn,$sql);		

// if query is invalid do this 
if (!$rs)
  {exit("Error in SQL");}
  
// if query is valid do this 
while (odbc_fetch_row($rs))
{
  $id=odbc_result($rs,"ID");
  $compName=odbc_result($rs,"companyName");
  $compContact=odbc_result($rs,"companyContact");
  $nsContacts=odbc_result($rs,"nsContacts");
  $personal=odbc_result($rs,"personal");
  $overview=odbc_result($rs,"overview");
  $majorLocations=odbc_result($rs,"majorLocations");
  $systemTraffic=odbc_result($rs,"systemTraffic");
  $mjrIssOpps=odbc_result($rs,"mjrIssOpps");
  $notes=odbc_result($rs,"notes");
  $compInfoUpdated=odbc_result($rs,"compInfoUpdated");
  $persInfoUpdated=odbc_result($rs,"persInfoUpdated");
}

//odbc_close($conn);

// create a reference to a new COM component (Word)
$word = new COM("word.application") or die("Can't start Word!");

// print the version of Word that's now in use
//echo "Loading Word, v. {$word->Version}<br>";

// set the visibility of the application to 0 (false)
// to open the application in the forefront, use 1 (true)
$word->Visible = 0;

// create a new document in Word
$word->Documents->Add();

// add text to the new document
$word->Selection->TypeText("$id $compName");

//save the document in the Windows temp directory
$word->Documents[1]->SaveAs("/xampp/htdocs/profiles/$compName.doc");

// close the connection to the COM component
$word->Quit();

// print another message to the screen
//echo "Check for the file...";

?>
 
Your while loop is effectively overwriting the values for all the variables but doesn't actually do anything with them.

Once the While loop is done your variables then retain the last available value. Pretty much the values for the last row the query returned.

Which is what you use to produce the word document.

You do nothing with all the data you obtain form the database query, and nowhere do I see you actually using a value you get from pressing a button in another page.

For what you want, you need to pass a value form the page where you select the record to be written into a Word document that identifies that record. Then use it to run your query and then use those returned results to write it into the Word Document.







----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
All,

I need to know how to write a php redirect.

This is what I want to happen:

There is a list page with an action buttons column, once the word icon button is clicked it would create the word document, save the document in a prearranged file location and close application, from the what is stored in the database for the selected entry (which already works).

The problem that I am having is that once the button is clicked, there is a blank page.

I need to have this page or somehow in php to jump back to my list page.


Please help.

I hope I explained this well.

Thanks in advance.
 
At the very end of your PHP script, passed the PHP closing tag "?>"; add the JS:

Code:
<script type="text/javascript">
	window.location="[URL unfurl="true"]http://www.mypage.com/";[/URL]
</script>

I think this will do what you are looking for.


--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
No need to go the Javascript route.

Assuming you output nothing to the screen prior to this,, you can use the
Code:
header("location:[URL unfurl="true"]http:///www.domain.com/anotherpage.html");[/URL]
function to redirect anywhere you want to.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Because of the possibility that something may have been sent to screen or header is why I suggested the JS ... it will work no matter what ...

Good option though

--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
vacunita & southbeach,

Both options worked!

Thank you.

 
All - Everything works lovely with the script, but how can I format the word .doc file in php?

Like surround text with a gray box with a black outline and print margins.
 
How can I use COM to set the format and print margins.

Could you give me an example. Here's my code.

Code:
// create a reference to a new COM component (Word)
$word = new COM("word.application") or die("Can't start Word!");

// print the version of Word that's now in use
//echo "Loading Word, v. {$word->Version}<br>";

// set the visibility of the application to 0 (false)
// to open the application in the forefront, use 1 (true)
$word->Visible = 0;

// create a new document in Word
$word->Documents->Add();

// add text to the new document
$word->Selection->TypeText("$compName
$compContact
$nsContacts

Personal: $personal

Overview: $overview 

Major Locations: $majorLocations 

System Traffic: $systemTraffic 

Major Issue and Opportunities: $mjrIssOpps

Notes: $notes");

//save the document in the web directory
$word->Documents[1]->SaveAs("/xampp/htdocs/profiles/$compName.doc");

// close the connection to the COM component
$word->Quit();
 
c'mon! bit of googling for some self-help perhaps?

to set margins, for example

Code:
$word->Selection->PageSetup->LeftMargin = '2"'; //2 inches
$word->Selection->PageSetup->RightMargin = '2"';

to set font
Code:
$word->Selection->Font->Name = 'Arial';
$word->Selection->Font->Size = 8;

 
All - After research on creating formatted word docs through PHP, I found a link with the right information I need.

Unfortunately I receive an error message when I clicked my create word doc action button.

"Fatal error: Uncaught exception 'com_exception' with message '<b>Source:</b> Microsoft Word<br/><b>Description:</b> This command is not available because no document is open.' in C:\xampp\htdocs\profiles\view_word_profile.php:46 Stack trace: #0 C:\xampp\htdocs\profiles\view_word_profile.php(46): unknown() #1 {main} thrown in C:\xampp\htdocs\profiles\view_word_profile.php on line 46"

I searched this in Google and find information but it didn't make sense to me.

Here is my code.

Code:
<?php
// create database connection
include("includes/connection.php");
  
// call data to be viewed
$id=$_GET[id];
$sql="SELECT * FROM tblProfileList WHERE ID = $id";

// submit the query
$rs=odbc_exec($conn,$sql);		

// if query is invalid do this 
if (!$rs)
  {exit("Error in SQL");}
  
// if query is valid do this 
while (odbc_fetch_row($rs))
{
  $id=odbc_result($rs,"ID");
  $compName=odbc_result($rs,"companyName");
  $compContact=odbc_result($rs,"companyContact");
  $nsContacts=odbc_result($rs,"nsContacts");
  $personal=odbc_result($rs,"personal");
  $overview=odbc_result($rs,"overview");
  $majorLocations=odbc_result($rs,"majorLocations");
  $systemTraffic=odbc_result($rs,"systemTraffic");
  $mjrIssOpps=odbc_result($rs,"mjrIssOpps");
  $notes=odbc_result($rs,"notes");
  $compInfoUpdated=odbc_result($rs,"compInfoUpdated");
  $persInfoUpdated=odbc_result($rs,"persInfoUpdated");
}

odbc_close($conn);

// create a reference to a new COM component (Word)
$word = new COM("word.application") or die("Can't start Word!");

// print the version of Word that's now in use
//echo "Loading Word, v. {$word->Version}<br>";

// specify the MS Word template document ( with bookmarks inside )
$template_file = "G:/Grpratt/Profiles/profileTemplate08.doc";

// get the bookmark and create a new MS Word Range (to enable text substitution)
$compName = "compName";
$objBookmark = $word->ActiveDocument->Bookmarks($compName);
$range = $objBookmark->Range;

// now substitute the bookmark with actual value
$range->Text = $compName;

// set the visibility of the application to 0 (false)
// to open the application in the forefront, use 1 (true)
$word->Visible = 0;

// create a new document in Word
$word->Documents->Add();

// set the margins
$word->Selection->PageSetup->LeftMargin = '1"'; //1 inch
$word->Selection->PageSetup->TopMargin = '1"';
$word->Selection->PageSetup->RightMargin = '1"';
$word->Selection->PageSetup->BottomMargin = '1"';

// set the font
$word->Selection->Font->Name = 'Arial';
$word->Selection->Font->Size = 10;

// add text to the new document
/*$word->Selection->TypeText("$compName
$compLoc

$compContact

NS CONTACT:
$nsContacts

PERSONAL:
$personal

OVERVIEW:
$overview 

MAJOR LOCATIONS:
$majorLocations 

SYSTEM TRAFFIC:
$systemTraffic 

MAJOR ISSUES AND OPPORTUNITIES:
$mjrIssOpps

Notes:
$notes
");*/

//save the document in the web directory
//$word->Documents[1]->SaveAs("/xampp/htdocs/profiles/$compName.doc");
$word->Documents[1]->SaveAs("G:/Grpratt/Profiles/generated-Word-Profiles/$compName.doc");

// close the connection to the COM component
$word->Quit();

// Redirect browser
header("location:[URL unfurl="true"]http://localhost/profiles/profilelist.php");[/URL]
?>

Please help.

Thank you.
 
where have you told com to open a new word document?
i would expect to see a line like this
Code:
$word->Documents->Open(realpath($template_file));
before you start manipulating the document.
 
Is there a COM list out there somewhere?

Code:
// create a new document in Word
$word->Documents->Add();

This is listed before I start formatting.

I need to be able to add values from the database into a new word doc based on the set template.
 
what do you mean by a COM List? i think we're pretty well qualified to help you here but if you want to find an alternative then google is your friend.

look you are referencing things like ActiveDocument and range before you've actually opened a document. that's a bit too much to expect COM to do for you.

the basic premise is that you have to establish a hierarchy and use each node appropriately:

COM
--WORD
----Document
------range etc

create each node in the right order otherwise you can see why, surely, COM would find it difficult to create a range when it does not have a document.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top