473,700 Members | 2,593 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PHP COM MS Word Application

5 New Member
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";
  6.     //bring it to front
  7.     $word->Visible = 1;
  9.     //open an empty document
  10.     $word->Documents->Add();
  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");
  16.     //closing word
  17.     $word->Quit();
  19.     //free the object
  20.     $word = null;
  21. }
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 35064
5,058 Recognized Expert Expert
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");
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
5 New Member
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.d oc");

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
5,821 Recognized Expert Expert
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(...);
Oct 9 '08 #4
5 New Member
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.d oc");


$word->ActiveDocume nt->SaveAs("test.d oc");

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.applicati on': Server execution failed ' in C:\wamp\www\... \docgen.php:90 Stack trace: #0 C:\wamp\www\... \docgen.php(90) : com->com('word.appl icatio...') #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
5,821 Recognized Expert Expert
Which line is line 90?
Oct 10 '08 #6
5 New Member
That is the line:

$word = new COM("word.appli cation") or die("Unable to instantiate Word");
Oct 10 '08 #7
5,821 Recognized Expert Expert
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
5 New Member
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
2 New Member
after all i am having error like
Fatal error: Uncaught exception 'com_exception' with message 'Unable to lookup `ActiveDocument s': Unknown name. ' in C:\wamp\www\New Folder\index.ph p:12 Stack trace: #0 C:\wamp\www\New Folder\index.ph p(12): unknown() #1 {main} thrown in C:\wamp\www\New Folder\index.ph p on line 12

and on line 12...$word->ActiveDocument s[0]->SaveAs("Useles stest.doc");
Apr 3 '09 #10

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 Form_Load() Set AppWord = CreateObject("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 (0x800A0046) Permission denied: 'CreateObject' Does anybody know the correct way to do this? I don't want to link to a document, but rather display a word file inside the asp page.
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 memory. Also, I may have multiple copy of them... when you start/stop the application mutiple times. ?????? This is my vb6 code: Dim oApplication As Word.Application Dim oDocument As Word.Document
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 non-interactively I know, but I have explored alternatives, and they all introduced various other problems, leading me to discount them). The main issue with this solution is that when there are problems with the
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 actual error is: System.Runtime.InteropServices.COMException (0x80020005): Type mismatch. This method works when using the Office XP PIAs (not that this fact is
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 realeasing the Word.Application object. When I try to use the Quit() method I get an error: "Microsoft Word for Windows has encountered a problem and needs to close. We are sorry for the inconvenience" Either way I end up with multiple WinWord processes running.
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; // close word and quit word application WordDoc.Close(ref _newMissing, ref _newMissing, ref _newMissing); WordApp.Application.Quit(ref _notTrue, ref _newMissing, ref _newMissing);
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 because Word.Application is an unmanaged COM Interop object. I want to encapsulate the Word.Application in a Class that implements IDisposable to avoid this potential memory leak. Here is the code I have:
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 computer but not at the target computers. Private Sub Replace(ByRef WordApp As Word.Application, ByVal OLDText As String, ByVal NEWText As String) WordApp.Selection.Find.ClearFormatting() WordApp.Selection.Find.Text = OLDText ...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.