Entering Text Into Word Document

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Entering Text Into Word Document

Post by Mr Tech »

Hi there,

Is it possible to have an existing word document on the server, and then use PHP to modify it?

For example, the word document on the server has this in it:

Amount: [amount]

And then I could use PHP to replace the [amount] field without overwriting the exisiting document (Create a new .doc file). E.g:

Amount: 500

Is this possible? if so how?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

retrieve the extract of the document data via various means: [php_man]COM[/php_man] or others.. Then search and replace.

It may be a better idea to store the documents in rtf formats for easier manipulation. (Save as RTF in Word)
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post by Mr Tech »

Thanks!

Sheesh, so much information :) Do you happen to know of any precoded scripts for this?

Also, you neeed PHP 5 for this right?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Last I checked, com was supported in php4 too.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

here is an article that ongray found the other day for dealing with com objects and php4, specifically dealing with word, exel, and adobe.
http://www.phpbuilder.net/columns/alain ... hp3?page=1
I've read it and it seems pretty good.
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post by Mr Tech »

Thanks guys!

I've decided to sue the COM to change a html document into a word document. here's my code:

Code: Select all

<?php
   // msword.inc.php
   
   // NOTE: Using COM with windows NT/2000/XP with apache as a service
   // - Run dcomcnfg.exe
   // - Find word application and click properties
   // - Click the Security tab
   // - Use Custom Access Permissions
   //      - Add the user who runs the web server service
   // - Use Custom Launch permissions
   //      - Add the user who runs the web server service
   
   $wdFormatDocument = 0;
   $wdFormatTemplate = 1;
   $wdFormatText = 2;
   $wdFormatTextLineBreaks = 3;
   $wdFormatDOSText = 4;
   $wdFormatDOSTextLineBreaks = 5;
   $wdFormatRTF = 6;
   $wdFormatUnicodeText = 7;
   $wdFormatHTML=8;

   class MSWord
   {
       // Vars:
       var $handle;
       
       // Create COM instance to word
       function MSWord($Visible = false)
       {
           $this->handle = new COM("word.application") or die("Unable to instanciate Word");
           $this->handle->Visible = $Visible;
       }
       
       // Open existing document
       function Open($File)
       {
           $this->handle->Documents->Open($File);
       }
       
       // Create new document
       function NewDocument()
       {
           $this->handle->Documents->Add();
       }
       
       // Write text to active document
       function WriteText( $Text )
       {
           $this->handle->Selection->Typetext( $Text );
       }
       
       // Number of documents open
       function DocumentCount()
       {
           return $this->handle->Documents->Count;
       }
       
       // Save document as another file and/or format
       function SaveAs($File, $Format = 0 )
       {
           $this->handle->ActiveDocument->SaveAs($File, $Format);
       }
       
       // Save active document
       function Save()
       {
           $this->handle->ActiveDocument->Save();
       }
       
       // close active document.
       function Close()
       {
           $this->handle->ActiveDocument->Close();
       }
       
       // Get word version
       function GetVersion()
       {
           return $this->handle->Version;
       }
       
       // get handle to word
       function GetHandle()
       {
           return $this->handle;
       }
   
       // Clean up instance with word
       function Quit()
       {
           if( $this->handle )
           {
               // close word
               $this->handle->Quit();
   
               // free the object
               $this->handle->Release();
               $this->handle = null;
           }
       }
   };

?>
Now that works fine but when it converts it into the word doc, it stuffs up all the margins and pushes the table off the paper.

Any ideas on changing the margins dynamicly?
Post Reply