469,330 Members | 1,270 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,330 developers. It's quick & easy.

PHP COM MS Word Application

This is my first post here - yay! This is my function that I am calling (just passing test arguments):

Expand|Select|Wrap|Line Numbers
  1. function makeDoc($name,$content){
  2.     // starting word
  3.     $word = new COM("word.application") or die("Unable to instantiate Word");
  4.     echo "Loaded Word, version {$word->Version}\n";
  5.  
  6.     //bring it to front
  7.     $word->Visible = 1;
  8.  
  9.     //open an empty document
  10.     $word->Documents->Add();
  11.  
  12.     //do some weird stuff
  13.     $word->Selection->TypeText("This is a test document. Your name is $name and you said: $content");
  14.     $word->Documents[1]->SaveAs("test.doc");
  15.  
  16.     //closing word
  17.     $word->Quit();
  18.  
  19.     //free the object
  20.     $word = null;
  21. }
  22.  
The output from the function is:
Loaded Word, version 12.0
Fatal error: Call to undefined method variant::SaveAs() in C:\wamp\www\...\docgen.php on line 93
Also, I'm assuming that you must have Word installed on the machine? That will be fine for my localhost, but will I also have to put Word on the server when the time comes?

Hey, thanks for your help!
- Scott
Oct 7 '08 #1
13 34099
Atli
5,058 Expert 4TB
Hi Scott. Welcome to Bytes!

Firstly, the first index of a typical array is 0, so you would probably want to do:
Expand|Select|Wrap|Line Numbers
  1. $word->Documents[0]->SaveAs("test.doc");
  2.  
Unless Microsoft somehow managed to mess that up as well.

Also, I'm assuming that you must have Word installed on the machine? That will be fine for my localhost, but will I also have to put Word on the server when the time comes?
To use this COM object on any server, that server will have to be running Windows, and it has to have Word installed.
Oct 7 '08 #2
Thanks Atli,

I changed line 93 (now line 95, or line 14 from the snippet in my first post) so it now reads:

$word->Documents[0]->SaveAs("test.doc");

I thought it was strange that it was accessing the second element of the array.

Now when I call the function (on my local machine still) I get this:

Loaded Word, version 12.0
Fatal error: Uncaught exception 'com_exception' with message '<b>Source:</b> Microsoft Word<br/><b>Description:</b> The requested member of the collection does not exist.' in C:\wamp\www\...\docgen.php:95 Stack trace: #0 C:\wamp\www\...\docgen.php(95): makedoc() #1 C:\wamp\www\...\docgen.php(25): makedoc('Scott', 'Time for winter.') #2 {main} thrown in C:\wamp\www\...\docgen.php on line 95

Just in case it's relevant line 25 reads: makedoc("Scott","Time for winter."); which is just where the function is called from.

What can I fix?

Thanks again,
- Scott
Oct 8 '08 #3
pbmods
5,821 Expert 4TB
Heya, Scott.

I looked over the PHP manual page for COM (http://php.net/manual/class.com.php), and I noticed a snippet about 3/4 of the way through the comments that used ActiveDocument instead of Documents[1].

Worth a try....

Expand|Select|Wrap|Line Numbers
  1. $word->ActiveDocument->SaveAs(...);
  2.  
Oct 9 '08 #4
Hey thanks pbmods, that is an awesome suggestions, and to me makes more sense the way you have suggested. After I made the change from

$word->Documents[1]->SaveAs("test.doc");

to

$word->ActiveDocument->SaveAs("test.doc");

it gave me a time a timeout error. When I added set_time_limit(0); at the first of the function and set_time_limit(30); at the end of the function it fixed the timeout error.

Now when I run it (on my localhost still) I get this:

Fatal error: Uncaught exception 'com_exception' with message 'Failed to create COM object `word.application': Server execution failed ' in C:\wamp\www\...\docgen.php:90 Stack trace: #0 C:\wamp\www\...\docgen.php(90): com->com('word.applicatio...') #1 C:\wamp\www\...\docgen.php(29): makedoc('Scott', 'Time for winter.') #2 {main} thrown in C:\wamp\www\...\docgen.php on line 90

Thanks for your help! It shouldn't be this hard! Ha ha.
Oct 9 '08 #5
pbmods
5,821 Expert 4TB
Which line is line 90?
Oct 10 '08 #6
That is the line:

$word = new COM("word.application") or die("Unable to instantiate Word");
Oct 10 '08 #7
pbmods
5,821 Expert 4TB
From what I can find from searching the web, it doesn't look like there's a problem with your code. My guess is that it either the COM extension or Microsoft Word is not behaving properly

I'd try reinstalling (upgrading if applicable) PHP and/or Word on your server and see if that helps.
Oct 10 '08 #8
Hey thanks, that is a great suggestion. I've been staring at the code and looking everywhere for examples and the code looks fine to me. It must be something in the configuration of either word or the configuration of php. I'll look into things further from both of those suggestions. Thanks!
Oct 10 '08 #9
after all i am having error like
Fatal error: Uncaught exception 'com_exception' with message 'Unable to lookup `ActiveDocuments': Unknown name. ' in C:\wamp\www\New Folder\index.php:12 Stack trace: #0 C:\wamp\www\New Folder\index.php(12): unknown() #1 {main} thrown in C:\wamp\www\New Folder\index.php on line 12


and on line 12...$word->ActiveDocuments[0]->SaveAs("Uselesstest.doc");
Apr 3 '09 #10
I written this code...
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $word = new COM("word.application") or die ("Could not initialise MS Word object.");
  3. $word->ActiveDocument->Open("doc1.doc");
  4. // Extract content.
  5. $content = (string) $word->ActiveDocument->Content;
  6. echo $content;
  7. $word->ActiveDocument->Close(false);
  8. $word->Quit();
  9. $word = null;
  10. unset($word);
  11. ?>

but it shows following error please help


Fatal error: Uncaught exception 'com_exception' with message '<b>Source:</b> Microsoft Word<br/><b>Description:</b> This command is not available because no document is open.' in C:\wamp\www\New Folder\index.php:3 Stack trace: #0 C:\wamp\www\New Folder\index.php(3): unknown() #1 {main} thrown in C:\wamp\www\New Folder\index.php on line 3
Apr 3 '09 #11
pbmods
5,821 Expert 4TB
Heya, sukhvir13.

Try $word->Documents->Open() instead of $word->ActiveDocument.

If that doesn't work, you might be able to get by with $word->Documents->Add() and then $word->ActiveDocument->open().

There might be more info here:
http://php.net/com
Apr 3 '09 #12
saimhe
1
I had similar problems with some (but not all) methods being undefined. Final solution: "[x] Allow to interact with the desktop" among the properties of the Apache service. A real user instead of SYSTEM might also work.
Mar 13 '12 #13
Did you solve the problem? I also met the same problem
Dec 21 '17 #14

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Microsoft | last post: by
reply views Thread by jorgedec | last post: by
reply views Thread by Alan T | last post: by
2 posts views Thread by Alan T | last post: by
1 post views Thread by =?Utf-8?B?c3VydHVyeg==?= | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.