tags:

views:

197

answers:

3

Hello! I am actually trying some codes i found from http://php.net/manual/en/class.com.php

  <?php
    // starting word
    $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?

+1  A: 

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");
Andy E
Thanks for your reply. I am getting the same error message.
chupinette
@chupinette: try `$word->ActiveDocument->SaveAs("Useless test.doc");`
Andy E
Ive tried this also. Same error :(
chupinette
+1  A: 

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
0xA3
How do i disable all add ins?I have found the Normal file which is a macro enabled template. To which name should i rename it? thanks for your help
chupinette
Just backup the Normal.dot(x) or delete it. You can disable the add-ins one-by-one via Office button -> Word Options -> Add-ins, then select add-in type from the combo box and press Go...
0xA3
thanks a lot. I am going to try it right now.
chupinette
Ive tried all of the above but im getting the same error.
chupinette
What PHP version are you on?
0xA3
PHP 5.3.1 and im using Windows Vista
chupinette
I did my test on 5.3.2 on Windows 7 in case it matters.
0xA3
Do you suggest that i switch to 5.3.2?
chupinette
A: 

I solved it by using : http://www.phpbuilder.net/columns/venkatesan20030501.php3? Thanks for your replies

chupinette