COM and MS Word Margins?

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

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

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post 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?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

perhaps something with types juggling.
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post by Mr Tech »

What do you mean by "types juggling"?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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', '!!!!!!');
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post by Mr Tech »

Woohoo it works! You're a champion!

Much aprpeciated Weirdan, you have helped me a lot!

have a great day!
Post Reply