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 dencom 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
 
personally, i'm not a fan of tutorials unless (i) the author has the credentials to convince you he's worth spending your time on; and (ii) the tutorial is very recent; and (iii) it's available for peer review.

my advice is to keep it simple for the time being. perhaps you could try this 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 
if (FALSE === odbc_fetch_row($rs)){
	die ('no records found');
}
$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!");

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

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

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

/*
 * create an instance of a new document based on the template
 * if it exists
 */
 if (file_exists($template_file)){
	$document = $word->Documents->Open(realpath($template_file)); 
} else {
	$document = $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
$string = <<<TEXT
$compName
$compLoc

$compContact

NS CONTACT:
$nsContacts

PERSONAL:
$personal

OVERVIEW:
$overview 

MAJOR LOCATIONS:
$majorLocations 

SYSTEM TRAFFIC:
$systemTraffic 

MAJOR ISSUES AND OPPORTUNITIES:
$mjrIssOpps

Notes:
$notes

TEXT;

$word->Selection->TypeText($string);

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

// close the connection to the COM component
$word->Quit();
$word->Release();
echo "document has been created<br/>";
?>
 
I received an error message.

Error:
Code:
Loading Word, v. 11.0

Fatal error: Uncaught exception 'com_exception' with message 'Parameter 0: Type mismatch. ' in C:\xampp\htdocs\profiles\view_word_profile.php:107 Stack trace: #0 C:\xampp\htdocs\profiles\view_word_profile.php(107): variant->SaveAs(false) #1 {main} thrown in C:\xampp\htdocs\profiles\view_word_profile.php on line 107
 
i suspect that this is because you have not given the php/webserver process permissions for this directory. assuming that you are using IIS make sure that IIS_[MACHINENAME] has write permissions for the dir.
 
I am using the following code and it works.

The problem is that is only works for one bookmark. How can I call for multiple bookmarks to be displayed within my php script?

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");
  $compLoc=odbc_result($rs,"compLoc");
  $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);

//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 = "G:/Grpratt/Profiles/profileTemplate08.doc";

//3. open the template document
$word->Documents->Open($template_file);

//get the current date MM/DD/YYYY
//$current_date = date("m/d/Y");


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

//5. now substitute the bookmark with actual value
$range->Text = $personal;

//6. save the template as a new document (c:/reminder_new.doc)
$new_file = "G:/Grpratt/Profiles/generated-Word-Profiles/$compName.doc";
$word->Documents[1]->SaveAs($new_file);

//7. free the object
$word->Quit();

// Redirect browser
header("location:[URL unfurl="true"]http://localhost/profiles/profilelist.php");[/URL]
?>
 
for example, this code snip should set you on the right track

Code:
$bookmarknames = array ("personal", 'compName', 'compLoc', 'compContact', 'nsContact'); //make sure the bookmark names are the same as the variables that hold the relevant information

foreach ($bookmarknames as $mark){
	$objBookmark = $word->ActiveDocument->Bookmarks($mark);
	$range = $objBookmark->Range;
	$range->Text = ${$mark}; //note the use of a variable variable
}
 
Thank you!!

That worked exactly as planned. Sorry I didn't explain my issue better earlier.

Have a great weekend!
 
Hi friends

I'm replying to this as my issue relates to pretty much the same thing.

I was just wondering, how can I play with other details, like add an image, add a header, add a footer, add page break, add alignment (right, left, justify), page layout, etc.

There must be some documentation somewhere where you guys have learned things, right?

Thanks.
 
you can generally work out the write COM commands from analysing the results of a recorded macro doing the same thing.

that's all COM is: an access extension to VBA.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top