Beste PHPers,

Heb een probleempje met het volgende script:

<?php
ini_set("error_handling", 1);
error_reporting(E_ALL);

class PHPtoWORD
{
	// Defining filepath and extension
	var $FilePath = "C:/";
	var $FileExt = "doc";
	
	// Making vars for filename and text
	var $FileName;
	var $FileText;
	
	// Var for running the application (WORD)
	var $Application;
	
	function __construct()
	{
		if(!$this->Application = new COM("word.application"))
		{
			echo "[001] An error occured when launching the <i>Word Application</i>, either the application couldn't get launched, or <i>Microsoft Office</i> was not found!";
		}
		else
		{
			$this->Application->Visible = 1;
			if(!empty($this->FileText) && !empty($this->FileName))
			{
				$this->AddTextToFile();
			}
			//else
			//{
			//	echo "[002] An error occured, the text for the document could not be found!";
			//}
		}
	}
	
	function AddTextToFile()
	{
		$this->Application->Add();
		$this->Application->Selection->TypeText($this->filetext);
	}
	
	function SaveFile($FilePath = "C:/", $FileExt = "doc")
	{
		if(!$this->Application->Documents[1]->SaveAs("C:/test.doc"))
		{
			echo "[003] An error occured, the file could not be saved!";
		}
		else
		{
			$this->ExitApplication();
		}
	}
	
	function ExitApplication()
	{
		$this->Application->Quit();
		$this->Application = NULL;
	}
	
	function SetFileName($filename)
	{
		if(!empty($filename))
		{
			$this->FileName = $filename;
		}
		else
		{
			echo "[004] An error occured, the file's name could not be changed!";
		}
	}
	
	function SetFileExt($fileext)
	{
		if(!empty($fileext))
		{
			$fileext = strtolower($fileext);
			if($fileext == "doc" || $fileext == "docx")
			{
				$this->FileExt = $fileext;
			}
			else
			{
				echo "[005] An error occured, the file's extension is unknown, or not allowed!";
			}
		}
		else
		{
			echo "[006] An error occured, the file's extension could not be changed!";
		}
	}
	
	function SetFileText($filetext)
	{
		if(!empty($filetext))
		{
			$this->FileText = $filetext;
		}
		else
		{
			echo "[007] An error occured, the text for the file could not be set!";
		}
	}
}

$PHP2WORD = new PHPtoWord();
$PHP2WORD->SetFileName('JUSTATEST');
$PHP2WORD->SetFileText('BLAHBLAHBLAHALVALHASDFL');
$PHP2WORD->SetFileExt('DOC');

$PHP2WORD->SaveFile("C:/", "doc");
?>

De foutmelding die ik krijg is de volgende:
Fatal error: Uncaught exception 'com_exception' with message '<b>Source:</b> Microsoft Word<br/><b>Description:</b> Het gevraagde lid van de collectie bestaat niet.' in C:\wamp\www\phpword\index.php:47 Stack trace: #0 C:\wamp\www\phpword\index.php(47): PHPtoWORD::SaveFile() #1 C:\wamp\www\phpword\index.php(113): PHPtoWORD->SaveFile('C:/', 'doc') #2 {main} thrown in C:\wamp\www\phpword\index.php on line 47

Wat zou betekenen dat het in deze regel fout zou moeten gaan:
<?PHP	if(!$this->Application->Documents[1]->SaveAs("C:/test.doc"))
{
	echo "[003] An error occured, the file could not be saved!";
}
?>

Als jullie een oplossing weten zou ik dit graag horen!

Alvast bedankt

Jeroen
Je moet de Exceptions afvangen, met een try-catch blok, en dan de error weergeven, of iets zinnigs doen met de error.
De naam van je klasse lijkt meer op een naam voor een functie, wanneer je OOP gaat toepassen zijn de benaming van klassen methodes en attributen erg belangrijk.

Zie het als objecten, in dit geval maak je eigenlijk gewoon een nieuw word bestand aan. Dus een betere benaming voor de klasse zou bijvoorbeeld zijn: WordDocument.

Ook stamt het var gebeuren uit PHP4, beter is dus om de toegang van attributen aan te geven net zoals je bij de methoden ( functies binnen een klasse ) doet.

Bijvoorbeeld:
private $varnaam;
Probeer jij een file op te slaan op locatie "C:/..."?
Dan is dat toch niet de bedoeling om online te zetten, op een server?

Zorg gewoon eens dat je die file op een submap opslaat, misschien helpt dat.

Reageren