NYCPHP Meetup

NYPHP.org

[nycphp-talk] PHP / Word 2003 COM

Joseph Crawford codebowl at gmail.com
Fri Sep 16 12:38:29 EDT 2005


ok so i have figured it out well almost. I am having one issue that i cannot 
seem to figure out how to fix.

When i run my script it goes through the process of creating the mail merge. 
If i refresh to attempt to do it again my script hangs trying to create the 
COM object. I have to manually restart my computer before i can run the 
merge again. Obviously this is not acceptable. Have any of you guys had this 
issue before when using COM objects?

Below is my code for my MailMerge class completed with help from 
http://www.zend.com/tips/tips.php?id=262&single=1


CODE
==========================================
<?php

class MailMerge {
private $mm_data_dir;
private $obj;
private $fieldcnt;
private $rowcnt;

public function __construct($list = null, $data_dir = 'data/mailmerge') {
if(!is_array($list)) throw new Exception('Cannot Create A Mail Merge With An 
Empty List.');
$this->mm_data_dir = BASE_PATH.'/'.$data_dir;
$this->list = $list;
$this->initilize();

$this->CreateHeaderFile();
$this->CreateDataSource();
$this->CreateFile();
return($this->mm_data_dir.'/merge.doc');
}

private function initilize() {
$this->rowcnt = count($this->list);
$fieldcnt = 0;
foreach($this->list as $key => $item) {
if($fieldcnt == 0) $fieldcnt = count($item);
$this->columns[] = $key;
}
$this->fieldcnt = $fieldcnt;
}

private function Close() {
$this->obj->Documents->Close();
}

private function Quit() {
$this->obj->Quit(); 
}

private function Release() {
$this->obj = NULL; 
}

private function CreateHeaderFile() {
$this->obj = new COM("word.application") or die('Couldnt load Word!');
if(!is_object($this->obj)) throw new Exception('Unable to instanciate 
Word!');
$this->obj->Documents->Add();
$this->obj->ActiveDocument->Tables->Add($this->obj->Selection->Range,1,$this->fieldcnt);

foreach($this->list as $item) {
foreach($item as $key => $value) {
$this->obj->Selection->TypeText($key);
$this->obj->Selection->MoveRight();
} 
}
$this->obj->ActiveDocument->SaveAs($this->mm_data_dir.'/header.doc');
$this->Close();
$this->Quit();
$this->Release();
}

private function CreateDataSource() {
$this->obj = new COM("word.application");
if(!is_object($this->obj)) throw new Exception('Unable to instanciate 
Word!');
$this->obj->Documents->Add();
$this->obj->ActiveDocument->Tables->Add($this->obj->Selection->Range,$this->rowcnt,$this->fieldcnt);

foreach($this->list as $key => $item) {
foreach($item as $key => $value) {
$this->obj->Selection->TypeText($value);
$this->obj->Selection->MoveRight();
}
}
$this->obj->ActiveDocument->SaveAs($this->mm_data_dir.'/ds.doc');
$this->Close();
$this->Quit();
$this->Release();
}

private function CreateFile() {
$this->obj = new COM("word.application"); 
if(!is_object($this->obj)) throw new Exception('Unable to instanciate 
Word!');
echo $this->mm_data_dir.'/has_sites.dot';
$this->obj->Documents->Open($this->mm_data_dir.'/has_sites.dot');
$this->obj->ActiveDocument->MailMerge->OpenHeaderSource($this->mm_data_dir.'/header.doc');
$this->obj->ActiveDocument->MailMerge->OpenDataSource($this->mm_data_dir.'/ds.doc');
$this->obj->ActiveDocument->MailMerge->Execute();
$this->obj->ActiveDocument->SaveAs($this->mm_data_dir.'/merge.doc');
$this->Close();
$this->Quit();
$this->Release();
unlink($this->mm_data_dir.'/header.doc');
unlink($this->mm_data_dir.'/ds.doc');
}
}
?>
==========================================

Any help would be appreciated.


-- 
Joseph Crawford Jr.
Codebowl Solutions, Inc.
1-802-671-2021
codebowl at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20050916/e4f989a5/attachment.html>


More information about the talk mailing list