Jump to content

PHP to generate a Word document


gregcaulder

Recommended Posts

Ok I want to generate a word document and i keep getting an error.

 

Here is my setup

Server 2003

IIS6

 

here is my code.

 

<?php

$word = new COM("word.application") or die("Unable to instantiate Word");

//To see the version of Microsoft Word, just use $word->Version

echo "I'm using MS Word {$word->Version}";

//It's better to keep Word invisible

$word->Visible = 0;

//Creating new document

$word->Documents->Add();

//Setting 2 inches margin on the both sides

$word->Selection->PageSetup->LeftMargin = '2"';

$word->Selection->PageSetup->RightMargin = '2"';

//Setup the font

$word->Selection->Font->Name = 'Verdana';

$word->Selection->Font->Size = 8;

//Write some text

$word->Selection->TypeText("Hello, universe!");

//Save the document as DOC file

$word->Documents[1]->SaveAs("C:\hello2.doc");

// or use: $word->Documents[1]->SaveAs("C:htdocshello2.rtf",6); to save as RTF file

// or use: $word->Documents[1]->SaveAs("C:htdocshello2.htm",8); to save as HTML file

//And of course, quit Word

$word->quit();

$word->Release();

$word = null;

//Give the user a download link

echo '<a href="hello2.doc">Download file as .doc</a>';

?>

 

 

And here is my error message

 

I'm using MS Word 11.0

Fatal error: Call to a member function TypeText() on a non-object in D:\HTTP\SDC\stores\barcode\generate.php on line 17

 

 

Any help would be much appreciated.

 

 

Greg

Link to comment
https://forums.phpfreaks.com/topic/44044-php-to-generate-a-word-document/
Share on other sites

ok I have tried something different. I'm trying to open the dam document and i am sure that I have the Dirictory correct, but it still cant open the damn file.

 

 

What gives.

 

 

<?php

$input = "D:/HTTP/SDC/include/dortemplate/dymo_30252.doc";

$word = new COM("word.application") or die("Unable to instantiate Word");

print "Loaded Word, version {$word->Version}\n<br>";

$word->Visible = 1;

print "Set Word To Visible<br>";

$word->Documents->Open($input);

print "Opened $input<br>";

$word->Activate;

print "Activated Word<br>";

 

print "Editing Fields<br>";

$Field = $word->ActiveDocument->Fields(1);

$fieldData = $Field->Result;

$fieldData->Text = "Testing";

 

?>

 

 

This is the error im getting:

Loaded Word, version 11.0

Set Word To Visible

 

Fatal error: Uncaught exception 'com_exception' with message '<b>Source:</b> Microsoft Word<br/><b>Description:</b> The document name or path is not valid. Try one or more of the following: * Check the path to make sure it was typed correctly. * On the File menu, click Open. Search for the file using this dialog box. (D:/HTTP/SDC/include/dortemplate/dymo_...)' in D:\HTTP\SDC\stores\barcode\generate.php:8 Stack trace: #0 D:\HTTP\SDC\stores\barcode\generate.php(8): variant->Open('D:/HTTP/SDC/inc...') #1 {main} thrown in D:\HTTP\SDC\stores\barcode\generate.php on line 8

Archived

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

×
×
  • Create New...

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.