PHP Classes

it doesnot work me

Recommend this page to a friend!

      PHP MS Word  >  All threads  >  it doesnot work me  >  (Un) Subscribe thread alerts  
Subject:it doesnot work me
Summary:it doesnot work me
Messages:7
Author:petr totin
Date:2006-12-04 12:54:16
Update:2009-12-01 07:44:00
 

  1. it doesnot work me   Reply   Report abuse  
Picture of petr totin petr totin - 2006-12-04 12:54:16
Fatal error: Call to a member function Open() on a non-object in c:\...\mswordphp\clsWord.php on line 34

What's the problem?
Thanks Petr.

  2. Re: it doesnot work me   Reply   Report abuse  
Picture of Nico den Boer Nico den Boer - 2006-12-13 20:59:41 - In reply to message 1 from petr totin
The problem is instantiation. I've extended the class a bit.
In my case I'm still having problems opening a existing document, which does work when I do the same using a different programming language. The syntax while using the com object is correct, but word/php seem to have some problems in working together. Below the code as I have it now.

<?php
class msWord extends coreClass
{
/**
* Instance of MS Word
*
* @var int
* @access private
*/
private $_handle;
/**
* Initialise Word
*
* @access public
* @param bool $visible, optional
* @return bool for success
*/
public function init($visible = false)
{
$this->_handle = new COM("Word.application");
if (!is_object($this->_handle))
{
$this->addError("Error instantiating Word");
return false;
}
else
{
$this->_handle->Visible = $visible;
return true;
}
}
/**
* Open a document
*
* @access public
* @param string $dir
* @param string $file
* @return bool for success
*/
public function open($file, $dir = null)
{
if ($this->hasErrors()) return false;
try
{
if (isset($dir))
$this->_handle->ChangeFileOpenDirectory($dir);
$this->_handle->Documents->Open($file);
}
catch (Exception $e)
{
$this->addError($e->getMessage());
}
return !$this->hasErrors();
}
/**
* Create a new document
*
* @access public
* @return void
*/
public function newDoc()
{
if ($this->hasErrors()) return false;
$this->_handle->Documents->Add();
}
/**
* Select all text in document
*
* @access public
* @return void
*/
public function selectAll()
{
if ($this->hasErrors()) return false;
$this->_handle->Selection->WholeStory;
}
/**
* Get all text in document
*
* @access public
* @return string
*/
public function getAll()
{
if ($this->hasErrors()) return "";
$this->selectAll();
return $this->_handle->Selection->Text;
}
/**
* Write text to active document
*
* @access public
* @param string $text
* @return void
*/
public function write($text)
{
if ($this->hasErrors()) return;
$this->_handle->Selection->Typetext($text);
}
/**
* Write text at specified bookmark
*
* @access public
* @param string $bookmark
* @param string $text
* @return void
*/
public function writeBookmarkText($bookmark, $text)
{
if ($this->hasErrors()) return false;
try
{
$objBookmark = $this->_handle->ActiveDocument->Bookmarks($bookmark);
$range = $objBookmark->Range;
$range->Text = $text;
}
catch (Exception $e)
{
$this->addError($e->getMessage());
}
}
/**
* Get number of open documents
*
* @access public
* @return int
*/
public function getDocumentCount()
{
if ($this->hasErrors()) return 0;
return $this->_handle->Documents->Count;
}
/**
* Save active document
*
* @access public
* @return void
*/
public function save()
{
if ($this->hasErrors()) return 0;
$this->_handle->ActiveDocument->Save();
}
/**
* Save document as another file and/or format
*
* @access public
* @param string $file
* @param int $format, optional
* @return void
*/
public function saveAs($file, $format = 0)
{
if ($this->hasErrors()) return;
try
{
$this->_handle->ActiveDocument->SaveAs($file, $format);
}
catch (Exception $e)
{
$this->addError($e->getMessage());
}
}
/**
* Close active document.
*
* @access public
* @return void
*/
public function close()
{
try
{
$this->_handle->ActiveDocument->Close(false);
}
catch (Exception $e)
{
$this->addError($e->getMessage());
}
}
/**
* Get Word version
*
* @access public
* @return int
*/
public function getVersion()
{
if ($this->hasErrors()) 0;
return $this->_handle->Version;
}
/**
* Get Word handle
*
* @access public
* @return object
*/
public function getHandle()
{
return $this->_handle;
}
/**
* Clean up instance
*
* @access public
* @return void
*/
public function quit()
{
if ($this->_handle)
{
try
{
$this->_handle->Quit(null, null, null);
// free the object
$this->_handle->Release();
$this->_handle = null;
}
catch (Exception $e)
{}
}
}
}

//$input = "c:/doc1.doc";
//$output = "c:/doc2.txt";
//
//$word = new msWord();
//$word->open($input);
//file_put_contents($output, $word->getAll());
//$word->close();
//$word->quit();
//return;
//
//$input = "c:/doc1.doc";
//$output = "d:/doc2.doc";
//
//$word = new msWord();
//$word->open($input);
//$word->write("This is a test ");
//$word->writeBookmarkText("test", "this is a bookmark text");
//$word->saveAs($output);
//$word->quit();

/**
*/
class coreClass
{

private $_error = array();

/**
* Add a error
*
* @access public
* @param mixed $key
* @param string $descr
* @return void
*/
public function addError($key, $descr)
{
$this->_error[$key] = $descr;
}

/**
* Get a array with errors
*
* @return array
*/
public function getErrors()
{
return $this->_error;
}

/**
* Check if there are any errors
*
* @access public
* @return bool
*/
public function hasErrors()
{
return count($this->_error);
}
/**
* Reset errors
*
* @access public
* @return void
*/
public function resetError()
{
unset($this->_error);
$this->_error = array();
}
}

?>

  3. Re: it doesnot work me   Reply   Report abuse  
Picture of Neeraj Thakur Neeraj Thakur - 2007-11-20 09:29:33 - In reply to message 2 from Nico den Boer
Thanks for query.. please let me know the error or warning that you are getting.


  4. Re: it doesnot work me   Reply   Report abuse  
Picture of faiz hanifah faiz hanifah - 2008-10-31 00:19:57 - In reply to message 3 from Neeraj Thakur
I have try according manual in readme.txt , but i got erro likea this
Fatal error: Uncaught exception 'com_exception' with message 'Source: Microsoft Word
Description: This file could not be found. Try one or more of the following: * Check the spelling of the name of the document. * Try a different file name. (C:\Doc1.doc)' in C:\AppServ\www\word\clsWord.php:34 Stack trace: #0 C:\AppServ\www\word\clsWord.php(34): variant->Open('C:\Doc1.doc') #1 C:\AppServ\www\word\index.php(11): clsMSWord->Open('C:\Doc1.doc') #2 {main} thrown in C:\AppServ\www\word\clsWord.php on line 34

Please helpme...

my ym id = dj_faizha

thk before

  5. Re: it doesnot work me   Reply   Report abuse  
Picture of Neeraj Thakur Neeraj Thakur - 2008-10-31 07:43:14 - In reply to message 4 from faiz hanifah
The error description shows that file name or path is not accurate.
Can you please check it once again.

Thanks
Neeraj

  6. Re: it doesnot work me   Reply   Report abuse  
Picture of Beeta Kaljukits Beeta Kaljukits - 2008-12-10 07:03:45 - In reply to message 2 from Nico den Boer
Its still not working correctly.Is there any shance to make step-by-step what to do next?

  7. Re: it doesnot work me   Reply   Report abuse  
Picture of chetan gadgilwar chetan gadgilwar - 2009-12-01 07:44:00 - In reply to message 1 from petr totin
Hi guys this is still not working for me , i am geting this error ....

Fatal error: Uncaught exception 'com_exception' with message 'Failed to create COM object `word.application': Server execution failed ' in C:\wamp\www\msword\clsWord.php:27 Stack trace: #0 C:\wamp\www\msword\clsWord.php(27): com->com('word.applicatio...') #1 C:\wamp\www\msword\index.php(8): clsMSWord->clsMSWord() #2 {main} thrown in C:\wamp\www\msword\clsWord.php on line 27


your feedback is valuable for me.