This is my first post here - yay! This is my function that I am calling (just passing test arguments): - function makeDoc($name,$content){
-
// 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 document. Your name is $name and you said: $content");
-
$word->Documents[1]->SaveAs("test.doc");
-
-
//closing word
-
$word->Quit();
-
-
//free the object
-
$word = null;
-
}
-
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
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: -
$word->Documents[0]->SaveAs("test.doc");
-
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.
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
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.... -
$word->ActiveDocument->SaveAs(...);
-
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.
That is the line:
$word = new COM("word.application") or die("Unable to instantiate Word");
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.
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!
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");
I written this code... -
<?php
-
$word = new COM("word.application") or die ("Could not initialise MS Word object.");
-
$word->ActiveDocument->Open("doc1.doc");
-
// Extract content.
-
$content = (string) $word->ActiveDocument->Content;
-
echo $content;
-
$word->ActiveDocument->Close(false);
-
$word->Quit();
-
$word = null;
-
unset($word);
-
?>
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
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
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.
Did you solve the problem? I also met the same problem
Sign in to post your reply or Sign up for a free account.
Similar topics
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
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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;
...
|
by: Alan T |
last post by:
private Interop.Word.Application _wordApp;
What is the differences betwenn
_wordApp.Quit(...)
and _wordApp.Application.Quit(...) ?
|
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...
|
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...
|
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....
|
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...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
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...
|
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...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
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.
...
|
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...
|
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...
| |