This project is read-only.

Fatal error: Call to a member function loadTemplate() on a non-object in..

May 3, 2013 at 5:31 PM
Hi every body,
I'm just discovering PHPWord which seems to suit OK my needs.

I try to load template.docx to see how it complies to my requirements, but every time i run the script i get the flwg error
Fatal error: Call to a member function loadTemplate() on a non-object in /volume1/web/PHPWord_0/PHPWordEssai.php on line 6
My script code is
<?php
// Include the PHPWord.php, all other classes were loaded by an autoloader
require_once 'PHPWord.php';
require_once 'PHPWord/Template.php';

$template = $PHPWord->loadTemplate('Examples/Template.docx');
$template->setValue('Name', 'Somebody someone');
$template->setValue('Street', 'Coming-Undone-Street 32');

$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('../ComperioV1/dossierPerso/Template.docx');

// Create a new PHPWord Object
$PHPWord = new PHPWord();

// Every element you want to append to the word document is placed in a section. So you need a section:
$section = $PHPWord->createSection();

// After creating a section, you can append elements:
$section->addText('Bonjour tout le monde!', array('name'=>'Times', 'size'=>32, 'bold'=>true));

// You can directly style your text by giving the addText function an array:
$section->addText('Hello world! I am formatted.', array('name'=>'Tahoma', 'size'=>16, 'bold'=>true));

// If you often need the same style again you can create a user defined style to the word document
// and give the addText function the name of the style:
$PHPWord->addFontStyle('myOwnStyle', array('name'=>'Verdana', 'size'=>14, 'color'=>'1B2232'));
$section->addText('Hello world! I am formatted by a user defined style', 'myOwnStyle');

// You can also putthe appended element to local object an call functions like this:
$myTextElement = $section->addText('Hello World!');
//$myTextElement->setBold();
//$myTextElement->setName('Verdana');
//$myTextElement->setSize(22);

// At least write the document to webspace:
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('../ComperioV1/dossierPerso/helloWorld.docx');
?>
The file helloWorld.docx is generated OK.

Any help would appreciated

Many thks
May 3, 2013 at 5:42 PM
Edited May 3, 2013 at 5:42 PM
u must instantiate the object $PHPWord ;)
$PHPWord = new PHPWord();
$template = $PHPWord->loadTemplate('Examples/Template.docx');
May 3, 2013 at 8:09 PM
Edited May 4, 2013 at 7:52 AM
Many thks Mourad now i run into into an other trouble : the issue from the template is empty !

My code :
<?php
// Include the PHPWord.php, all other classes were loaded by an autoloader
require_once 'PHPWord.php';
require_once 'PHPWord/Template.php';

$PHPWord = new PHPWord();
$template = $PHPWord->loadTemplate('Examples/Template.docx');

$template->setValue('Value1', '101');
$template->setValue('Value2', '102');
$template->setValue('Value3', '103');
$template->setValue('Value4', '104');
$template->setValue('Value5', '105');
$template->setValue('Value6', '106');
$template->setValue('Value7', '107');
$template->setValue('Value8', '1008');
$template->setValue('Value9', '1000');
$template->setValue('Value10', '10067');

$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('../ComperioV1/dossierPerso/Template.docx');
?>
Why that ?

Up Anybody ?
May 6, 2013 at 8:17 AM
Edited May 6, 2013 at 8:18 AM
replace the following code:
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('../ComperioV1/dossierPerso/Template.docx');
by
$template->save('../ComperioV1/dossierPerso/Template.docx');
unset($template);
May 6, 2013 at 11:59 AM
THK U Mourad it works great.

Now i want to save file with date & time in the filename i get funny stuff like file name : modeleBP6-5-2013 12B.docx. Weird isn't it ?

My code :
<?php
// Include the PHPWord.php, all other classes were loaded by an autoloader
require_once 'PHPWord.php';
require_once 'PHPWord/Template.php';

$PHPWord = new PHPWord();
$template = $PHPWord->loadTemplate('Examples/Template.docx');

$date = new DateTime('now Europe/Paris');
$dateFormate = $date->format("j-n-Y h:i:s");
$timeFormate = $date->format("h:i:s");

$template->setValue('weekday', $date->format("l"));
$template->setValue('time', $dateFormate);  
$template->setValue('Value1', '101');
$template->setValue('Value2', '102');
$template->setValue('Value3', '103');
$template->setValue('Value4', '104');
$template->setValue('Value5', '105');
$template->setValue('Value6', '106');
$template->setValue('Value7', '107');
$template->setValue('Value8', '1008');
$template->setValue('Value9', '1000');
$template->setValue('Value10', '0067');


//  $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
//  $objWriter->save('../ComperioV1/dossierPerso/modeleBP.docx');
//  $template = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$template->save('../../ComperioV1/dossierPerso/modeleBP'.$dateFormate.'.docx');
unset($template);
?>
Any help would be appreciated.
Many thks
May 6, 2013 at 3:09 PM
File names do not accept the ":", tried to change the shape of the time to H-i-s
May 6, 2013 at 3:32 PM
Just great, it perfectly works.
Thousands of thanks !!!!
Dec 20, 2013 at 11:49 AM
Edited Dec 20, 2013 at 12:02 PM
hi, i would like to add a style to my variables comming from the data base for exemple let's say that i have a table users; in the table i have contact_first_name contact_last_name, adress, phone, and i want to show the full name of the client in red how can i do it with setvalue or with another function or what?
$sql="select * from users";
$req=mysql_query($sql);
$row=mysql_fetch_array($req);
foreach($row as $data)
      {
             $name=$data['contact_first_name'].' '.$data['contact_first_name'];
             $adress=$data['adress'];
             $phone=$data['phone'];
      }
$template->setValue('name', $name);
$template->setValue('adress', $adress);
$template->setValue('phone', $phone);
in the code above how can i set : $template->setValue('name', $name) to show the name in red, and is it possible to add icones before the content of the variables, i mean add icone of a phone before the phone number of the contact, and another one before the name and the adress as well.

Please any help is appreciated.
thanks.