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!
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.
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]
<?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;
}
}
};
?>
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
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