Page 2 of 2

Posted: Sat Nov 13, 2004 12:54 am
by Mr Tech
OK I've go the numerical values:

Global Const wdReplaceAll = 2
Global Const wdFindContinue = 1

So what do I need to do with these to make it work? Any idea where I can get the code for this?

Thanks

Ben

Posted: Sat Nov 13, 2004 12:45 pm
by Weirdan

Code: Select all

<?php   
$wdFormatDocument = 0;   
$wdFormatTemplate = 1;   
$wdFormatText = 2;   
$wdFormatTextLineBreaks = 3;   
$wdFormatDOSText = 4;   
$wdFormatDOSTextLineBreaks = 5;   
$wdFormatRTF = 6;   
$wdFormatUnicodeText = 7;   
$wdFormatHTML=8;

// added constants for search function
$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);       
  }
  // search and replace
  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, 0, 0, 0, 0, 0, 1, $wdFindContinue, 0, $with, $wdReplaceAll);
  }
// .... all other methods here
}
use the Replace method

Posted: Sun Nov 14, 2004 5:01 pm
by Mr Tech
Thanks. I appreciate your help on this. :)

I tried your code and got these errors:

Warning: Type not supported or not yet implemented. in c:\program files\easyphp\www\created\msword\msword.inc.php on line 49

Warning: Type not supported or not yet implemented. in c:\program files\easyphp\www\created\msword\msword.inc.php on line 49

Warning: Invoke() failed: Exception occurred. Source: Unavailable Description: Unavailable in c:\program files\easyphp\www\created\msword\msword.inc.php on line 49


But I no longer the the errors when I put the // infront of the $this->handle->Selection->Find->Execute() function...

Any ideas what is wrong?

Posted: Mon Nov 15, 2004 3:05 am
by Weirdan
perhaps something with types juggling.

Posted: Mon Nov 15, 2004 5:14 pm
by Mr Tech
What do you mean by "types juggling"?

Posted: Wed Nov 17, 2004 3:26 am
by Weirdan
I meant you had passed incorrect data of incorrect type or Word interpreted it as such. Finally I installed win-php and tested the class - it works perfectly with php-cli v4.3.9 & Word 2003 (I had to modify some code, here the latest working version):

Code: Select all

class MSWord {
//........skipped
       // search and replace
       function Replace($what, $with) {
           $this->handle->Documents[1]->Activate();
		   $this->handle->ActiveDocument->Select();
		   $this->handle->Selection->Find->ClearFormatting();
		   $this->handle->Selection->Find->Replacement->ClearFormatting();
/*
Function Execute([FindText], [MatchCase], [MatchWholeWord], [MatchWildcards], [MatchSoundsLike], [MatchAllWordForms], [Forward], [Wrap], [Format], [ReplaceWith], [Replace], [MatchKashida], [MatchDiacritics], [MatchAlefHamza], [MatchControl]) As Boolean
    Member of Word.Find
*/
		   $this->handle->Selection->Find->Execute($what, false, false, false, false, false, true, 1, false, $with, 2, false, false, false, false);
	  }
   }
// testing
$w =& new MSWord(true);
$w->Open('t.doc');
$w->Replace('sdf', '!!!!!!');

Posted: Thu Nov 18, 2004 7:27 pm
by Mr Tech
Woohoo it works! You're a champion!

Much aprpeciated Weirdan, you have helped me a lot!

have a great day!