472,328 Members | 1,102 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

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 34814
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

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

Similar topics

5
by: The Roys | last post by:
Hi Im doing something wrong in quitting the Word.Application in my VB program. I have General Declarations Dim AppWord As Word.Application ...
4
by: Microsoft | last post by:
I'm trying to display a word document inside a web page, but everytime I do I get this error: Error Type: Microsoft VBScript runtime...
2
by: Helene Day | last post by:
I did some experimentation in VB6 and VB.Net. With both applications, setting my Word.Application to nothing will not release winword.exe from...
0
by: David Akerman | last post by:
Hi, Background ======== I've written a Web App in C# which controls a pool of Word Applications for mailmerge purposes (not ideal using Word...
1
by: webstuff | last post by:
Hi, I'm getting a 'Type mismatch' exception when calling the Word.Application.Documents.Open method when using the Office XP 2003 PIAs. the...
0
by: jorgedec | last post by:
I'm trying to use Office's FileSearch object to search for files. I'm using Visual Studio 2002 c# and Word 2000. The problem I am having is...
0
by: Alan T | last post by:
I tried to close the word document object and word application: private Interop.Word.Application WordApp; private Interop.Word.Document WordDoc; ...
2
by: Alan T | last post by:
private Interop.Word.Application _wordApp; What is the differences betwenn _wordApp.Quit(...) and _wordApp.Application.Quit(...) ?
1
by: =?Utf-8?B?c3VydHVyeg==?= | last post by:
Hi, I'm using VB2005 + Office XP Enterprise. If you create a Word.Application object, you risk a memory leak if your application crashes...
0
by: asedt | last post by:
I have problem creating a private sub to replace text in a wordfile, is the use of ByRef good? The problem is that this works on my developing...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.