MSWord and PHP

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
mpiaser
Forum Newbie
Posts: 12
Joined: Wed Sep 07, 2005 8:16 pm

MSWord and PHP

Post by mpiaser »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I have some PHP code which opens a Word doc and saves it as a different name.  I'm trying to add code to find and replace a string in the document.  Unfortunately, I find many references to Select.Find.Text, Selection.Find.Replacement.Text and Find.Execute but I can't figure out the correct syntax.

Code: Select all

function SendDocumentToWord($Letter,$FileName)
{
echo "Starting";
$word=new COM("word.application") or die ("Unable to instanciate word");
$word->Visible=1;

$FullInputFilename="C:/inetpub/wwwroot/$Letter";
$FullOutputFilename="C:/inetpub/wwwroot/$FileName";
echo "opening $FullInputFilename";
$word->Documents->Open($FullInputFilename);

$word->Selection.FindText('mkp');
$word->Selection.ReplaceText('dog');
$word->Selection.Find.Execute();

echo "<BR>Saving as $FullOutputFilename...";
$word->Selection->TypeText(" replaced ");
$word->ActiveDocument->SaveAs($FullOutputFilename);
echo "Saved";
$word->Quit();
$word=null;
unset($word);
exit;

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post by Mr Tech »

Check out this class:

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;

   $wdReplaceAll = 2;
   $wdFindContinue = 1;

   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);
       }

       function Replace($what, $with) {       
           $this->handle->Documents[1]->Activate();       
           $this->handle->ActiveDocument->Select();       
           $this->handle->Selection->Find->ClearFormatting();       
           $this->handle->Selection->Find->Replacement->ClearFormatting();       
           $this->handle->Selection->Find->Execute($what, false, false, false, false, false, true, 1, false, $with, 2, false, false, false, false);
       }
       
       // Create new document
       function NewDocument()
       {
           $this->handle->Documents->Add();
       }
       
       // Write text to active document
       function WriteText( $Text )
       {
           $this->handle->Selection->Typetext( $Text );
       }

       // Set page margins
       function SetMargins($top,$bottom,$left,$right)
       {
	    $this->handle->Documents[1]->PageSetup->TopMargin = "$top cm";
	    $this->handle->Documents[1]->PageSetup->BottomMargin = "$bottom cm";
	    $this->handle->Documents[1]->PageSetup->LeftMargin = "$left cm";
	    $this->handle->Documents[1]->PageSetup->RightMargin = "$right cm";
       }
       
       // 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;
           }
       }
   };

?>
Then use this to replace and SaveAs:

Code: Select all

$Word = new MSWord;
$Word->Open("original.doc");
$Word->Replace("[replaceme]", "You have been replaced!");
$Word->SaveAs("saved.doc");
$Word->Quit();
mpiaser
Forum Newbie
Posts: 12
Joined: Wed Sep 07, 2005 8:16 pm

Post by mpiaser »

This words good until the $Word->Quit() call.. that call returns:

Fatal error: Uncaught exception 'com_exception' with message 'Error [0x80020003] Member not found. ' in c:\Inetpub\wwwroot\SendDocumentToWord.php:95 Stack trace: #0 c:\Inetpub\wwwroot\SendDocumentToWord.php(95): com->Release() #1 c:\Inetpub\wwwroot\SendDocumentToWord.php(110): MSWord->Quit() #2 c:\Inetpub\wwwroot\testword.php(5): SendDocumentToWord('test.doc', 'temp.doc') #3 {main} thrown in c:\Inetpub\wwwroot\SendDocumentToWord.php on line 95
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post by Mr Tech »

I'm not an expert but all I know is that it works for me... Perhaps you need to double check that the folder and document is writable...
mpiaser
Forum Newbie
Posts: 12
Joined: Wed Sep 07, 2005 8:16 pm

Post by mpiaser »

The new file des contain the replaced data so that part is working. Perhaps it is a version of MS-Word issue...I though I read that somewhere. What version of Word does the Quit() function work in? I'm running Word 2000
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post by Mr Tech »

Same here... This is beyond me sorry...
Post Reply