Using COM to open Word
I am actually trying some codes i found from http://php.net/manual/en/class.com.php
<?php
// starting word
开发者_StackOverflow $word = new COM("word.application") or die("Unable to instantiate Word");
echo "Loaded Word, version {$word->Version}\n";
//bring it to front
$word->Visible = 1;
//open an empty document
$word->Documents->Add();
//do some weird stuff
$word->Selection->TypeText("This is a test...");
$word->Documents[1]->SaveAs("Useless test.doc");
//closing word
$word->Quit();
//free the object
$word = null;
?>
But this does not seem to work. I am using Word 2007 and i get the following:
Loaded Word, version 12.0 Fatal error: Call to undefined method variant::SaveAs() in C:\xampp\htdocs\final\testq.php on line 14
Can anyone solve this problem? Is it because i am using Word 2007?
The Documents
object is a Collection object, not an array. Try:
$word->Documents(1)->SaveAs("Useless test.doc");
Or
$word->ActiveDocument->SaveAs("Useless test.doc");
Your sample runs fine for me, both with Word 2003 and Word 2007 on Window 7. Therefore I assume that the problem might be an incorrectly installed/configured Word. For troubleshooting do the following:
- repair the Word installation
- make sure Word has been started at least once as the same user your script runs under
- disable all add-ins
- go to
%APPDATA%\Microsoft\Templates\
and rename the Normal.dot(x) file - make sure that you actually have permissions to save files to the specified location, try with an absolute path
I solved it by using : http://www.phpbuilder.net/columns/venkatesan20030501.php3? Thanks for your replies
精彩评论