Fwd: Code Optimization Help

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello Everyone,
 
I have some code that is using COM to interact with MS Word to create a mail 
merge based on my mysql database, however it is running dreadfully slow 
13.53846 seconds to be exact. This is only running on 34 records, i could 
imagine running this on a few hundred records not to mention thousand. Is 
COM usually this slow? These load times i believe to be accurate as they 
come from zend studio's profiler.
You can see screenshots of the profile @ 
http://codebowl.dontexist.net/bugs/MailMerge/ Below you will see my code, 
anything you guys see that i could do to speed this up quite a bit i would 
appreciate it. It seems the naughty methods are CreateHeader, 
CreateDataSource, CreateDocument.


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

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

public function __construct($list = null, $letter = 'Has_Site', $envelope = 
'Envelope', $data_dir = 'data/mailmerge') {
if(!is_array($list)) throw new Exception('Cannot Create A Mail Merge With An 
Empty List.');
$this->mm_data_dir = 'F:/htdocs/csaf/'.$data_dir;
$this->list = $list;
$this->letter_template = $letter;
$this->envelope_template = $envelope;
$this->initilize();

$this->CreateHeaderFile();
$this->CreateDataSource();
$this->CreateDocument($this->letter_template);
$this->CreateDocument($this->envelope_template);
}

public function __destruct() {
unlink($this->mm_data_dir.'/ds.doc');
unlink($this->mm_data_dir.'/header.doc');
}

private function initilize() {
$this->rowcnt = count($this->list);
$this->fieldcnt = count($this->list[0]);
}

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

for($i = 0; $i <= $this->rowcnt; $i++) {
foreach($this->list[$i] 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);

for($i = 0; $i <= $this->rowcnt; $i++) {
foreach($this->list[$i] 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 CreateDocument($template) {
$this->obj = new COM("word.application"); 
if(!is_object($this->obj)) throw new Exception('Unable to instanciate 
Word!');
$this->obj->Documents->Open($this->mm_data_dir.'/'.$template.'.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.'/'.$template.'.doc');
$this->Close();
$this->Quit();
$this->Release();
}
}
?> <codebowl@xxxxxxxxx>

-- 
Joseph Crawford Jr.
Codebowl Solutions, Inc.
1-802-671-2021
codebowl@xxxxxxxxx

[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux