Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MS Word and COM

Status
Not open for further replies.

mThomas

Instructor
May 3, 2001
404
US

I have a project where users will need to open a Word document inside a browser. The users opening the Word document will allow/set IE to open Word in a browser. I figured using com would be the way to go.

I have a Windows server where I installed PHP 5.2.11. I have been able to write php scripts, however opening a Word document is driving me nuts.

I am getting a 500 error when I try the following code copied from php.net:

<?PHP

$word = new COM("word.application") or die("Unable to instantiate Word");

?>

The users will need to be able to open an existing Word document. Edit the document and then save it all within a broswer. The reason is the project must be HIPAA compliant and having the Word document opened in Word on a users computer presents HIPAA issues.

So, the main questions is, Can I open a Word document on a server running Windows 2008 64bit?

Can this be done?

Why is the above code producing an error? Is it because I do not have Word installed on the server?

Is there a setting in the PHP.ini file I need to set to allow IE to render a more detailed error report, sort of like debug="true"?

I've tried all sorts of various ways I've seen posted, most of which use the above code in some fashion.

Any help is appreciated.

mike
 
in order to use com to manipulate word, word must be installed on the server or on a server accessible to your webserver (in which case you set the second argument). you will also need to have com.allow_dcom set to true in php.ini (restart the web server afterwards).

please note that whatever happens in a browser is outside the control of php/com.

but let's back up a bit. can you explain the scenario in more detail? i am thinking that a non-com solution would be materially better but need more detail to verify that the solution i have in mind is viable.
 

The project is to create a HIPAA compliant Transcription web based application. The basic processes are:

1. A doctor dials an 800 number
2. Doctor is prompted to enter his or her ID
3. Doctor follows prompts and completes medical dictation
4. Autio file is accessed by transcriber
5. Transcriber opens appropriate medical word document
6. Transcriber transcribes audio file into word document
7. Transcriber saves word document
8. Word document is auto faxed to recipient
9. Word document is available on website for download

That is a brief outline of the basic processes. The audio/telcom portion will use Asterisk and thier AGI can be written in PHP/PERL and most any CGI. I am subcontracting this portion out and will modify as necessary.

The Word document must allow rich text features like bold etc... The transcriber must be able to save and continue, save and finish and these options need to be in the form of a button.

I do not have Word on the server, but can upload it. allow_dcom is set to true, and transcribers will allow Word to be opened in browser.

tia...mike
 
ok. i think my suggestion will work. i'll refine my thinking a bit re the formatting constraints and post back later on this evening.

another question, is it mandatory that MS Word is used or would openoffice be acceptable? i don't think it makes much difference but it might open up yet another option.
 

The hospitals, medical clinics and doctors want MS Word (probably more of a comfort thing), but maybe offering the option for both might be useful.

 
i was thinking of recommending phpLiveDocx but on closer inspection i see that you cannot dynamically control the formatting. however if your documents are template driven then this might well be a very good solution. note that you do not need to use the zend framework just to have liveDocx. it's not difficult to write your own soap interface.

the alternative that i was considering is to use a wysiwyg javascript control to allow the users to create their input. and keep their input in html form to allow for easy editing in the wysiwyg editor etc, and version control through some easy php script (or you could use wordpress to manage the whole document creation and versioning process).

then when you come to output the document to MS Word, rather than the whole hassle of parsing the input and creating a document using COM (not to mention the licensing costs of purchasing MS Word for the server and having to install it (not just upload it, of course), you could push the html output for the document through an rtf generator which will be retain formatting and be openable and manipulable in MS Word.

Here is an rtf generator (takes html input):
another alternative is to use the PEAR Open XML class to create oXML docs which MS Word should be able to read just fine and, from MS Office 10, will be the native format for MS Word. This does not get over the problem of parsing html (or bbcode input) etc and manually creating the documents, but will at least avoid the licensing costs and maintain wider compatibility than pure MS Word.

as a last alternative (for the moment), for true compatibility across platforms and printers etc, make the clients keep their documents on the server and when they want output, dump the output to pdf using dompdf or fpdf or html2pdf or any of the other libraries. that way you stop edits from being done outside of your control, maintain version control through whatever document management system you have, and ensure that audit trail remains good (for example you can watermark documents with a draft sigil until such time as the client accepts the document as final, in which case a version stamp, document number can be appended to the footer of the pdf and the draft watermark removed).

of all of these, i like the last best. it sounds a lot of work but in fact it's probably less than an hour if you use pre-built tools. wordpress handles versioning and auto-save as well as user auth, but if you don't want the whole WP platform you can just borrow the post creation and auto-save scripts to save you time. and the output to pdf: there are tons of libraries, some of which are slightly better than others.
 
I've installed the small business version of MS Office 2007 on the server.

I can create the com object.

I've tried most all the code I can find on the net.

For instance:

$word = new COM("word.application") or die ("Could not initialise MS Word object.");
$word->Documents->Open(realpath("admissions.doc"));
// Extract content.
$content = (string) $word->ActiveDocument->Content;
echo $content;
$word->ActiveDocument->Close(false);
$word->Quit();
$word = null;

The above code throws this error:

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.' ....

Now I've tried most of the paths, but nothing seems to work.

This has got to be possible to open a Word Document in a browser. I know it can be done in .NET as my client currently leases an application written for .NET.

I might end up having to code it in either vb.net or c#.

I have taken a look at your suggestions. I would like, at this point, to be able to simply open a Word doc in a browser through a com. That has got to be possible.

mike
 
Code:
I would like, at this point, to be able to simply open a Word doc in a browser through a com.

nope. php is server side and does not concern itself with browsers. php can manipulate a com object (such as for ms word) but that all happens server side. whether or not a word document opens in a browser on the client-side is dependent on the browser and platform in question. you cannot do anything serverside to ensure one behaviour or another.
 

I understand that the user can control whether Word will open up in a browser. The transcribers will tell their instance of Word to open in a browser.

I just want to open a word document in PHP through a com object.

That is seeming to be impossible. At this point, I would be happy to see PHP just open a Word document, even in Word through a com object.

mike

 
you can easily open a word document with php. that's not an issue. if you are observing the server computer too, you should see the document actually open in a word instance on the screen when you set visibility to true.

try the script from the php manual, which is known to work
Code:
<?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;
?>

if this works we can try opening pre-existing documents. there is nothing obviously wrong with your previous code; i'd guess it's because the file reference is not accurate.
 

I tried that from the php.net site, but here is the error it throws. Maybe this is a clue :)

Loaded Word, version 12.0
Fatal error: Call to a member function TypeText() on a non-object in D:\domain.com\testword.php on line 12

i would post the link, but not sure I should.

mike
 
ok. does word instantiate? can you see it on the screen? i suggest also rebooting the web server to make sure that php reloads (assuming you have not done this after installing ms word).
 
and just in case it's relevant, do check the permissions for word and dcom.
from the manual note
---
After one week of trying to understand what was wrong with my PHP communication with MS Word, i finally got it working...
It seems that if you're running IIS, the COM object are invoked with restricted privileges.
If you're having permission problems like not being able to opening or saving a document and you're getting errors like:

- This command is not available because no document is open
or
- Command failed

try this (if you're running IIS):

- Execute "dcomcnfg"
- Open Component Services > Computers > My Computer > DCOM Config
- Search for Microsoft Office Word 97-2003 Document (it will be something like this translated to your language, so take a while and search for it)
- Right-Click on it and open the properties
- Choose "Identity" tab
- Normally this is set to "the launching user". You have to change this to "the interactive user" or a admin user of your choice.
- Apply these new settings and test your COM application. It should work fine now.

I hope I save lots and lots of hours of headaches to some of you :)
---
 

this what I did.


In Windows:
1. Start
2. Run
3. Type "dcomcnfg"
4 Press OK
5. Expand Component Services
6. Expand Computers
7. Right click on My Computer
8. Go to Default Properties Tab
9. Make sure Enable COM Internet Services on this computer and Enable Distributed COM on this computer are checked.
10. Set Default Authentication Level to Default
11. Set Default Impersonation Level
12. Click the COM Security tab
13. Under the Access Permissions area, click Edit Limits...
14. Make sure ANONYMOUS LOGIN has remote access allowed
15. Click Add...
16. Under the "Enter object names to select" textarea enter "Everyone"
17. Give Everyone Local Access and Remote Access
18. Press Okay
19. Click Edit Default under the Access Permissions Area
20. Make sure SELF and SYSTEM have remote access
21. Also click Add... and repeat steps 16 and 17
22. Press OK
23. Under Launch and Activation Permissions click Edit Limits...
24. Make sure the Administrator user has remote launch and remote activation checked.
25. If Everyone group does not exist follow steps 16 and 17
26. Under the Everyone group allow local launch, remote launch, local activation, and remote activation.
27. Press OK
28. Click Edit Default... under Launch and Activation Permissions
29. Make sure Administrator, INTERACTIVE, and SYSTEM have remote launch and remote activation allowed.
30. Follow steps 16 and 17 to create an Everyone group
31. In the Everyone group allow local launch, remote launch, local activation, and remote activation.

In PHP.ini
1. Open PHP.INI
2. search for com.allow.dcom
3. uncomment it and set it to true
4. Save and close PHP.ini


 
after editing php.ini remember to restart the web server.

you said in post 3 that you had set allow.dcom to true. had the setting not stuck?

and it's not quite clear: your steps all look sound but you have not said whether your applet is now working?
 

Yes, I restarted the server after I made an edit to the php.ini file. It appears the settings stuck :)

Here are the com setting:

[COM]
; path to a file containing GUIDs, IIDs or filenames of files with TypeLibs
;com.typelib_file =
; allow Distributed-COM calls
com.allow_dcom = true
; autoregister constants of a components typlib on com_load()
;com.autoregister_typelib = true
; register constants casesensitive
;com.autoregister_casesensitive = false
; show warnings on duplicate constant registrations
;com.autoregister_verbose = true

Not sure what you mean by applet in this context. I just started the project, and figured I would tackle what I thought would be the most difficult first and the rest would be easier, so at this point the only thing I've tried is opening a Word document, and that's not going to well.

I'm sure it is somethng simple I'm doing wrong ;)

mike
 
the only thing i can suggest now is to make sure that your error reporting settings are at E_ALL and that errors are being displayed
Code:
error_reporting (E_ALL);
ini_set('display_errors', true);


can i confirm that in dcomcfg you have explicitly set the identity properties for MS Word Documents to the interactive user or an admin user? as per my previous post? your response showed lots of steps you had gone through in dcomcfg but not the ones that we are told are critical.

but anyway that is unlikely to be the issue as you are getting an error at the object level (the Selection object) rather than an access denied level.

you also have not answered as to whether you can see ms word instantiate and appear on the screen.
 

No, I do not see an instance of Word appear either in the browser or Word actualy launching. In the vb.net version of what I'm trying to do will actauly open Word if the document does not exist, which is what I'm thinking you are refering to.

Let's review and expand. I have a project to create a HIPAA compliant transcription web application. It is a totally cool project. The telcom/audio portion of the project is pretty straight forward and not an issue. The front end of the site is basic database work and some techno graphics. The only part of the project which concerns me, is the opening of Word in a browser.

I knew that the user has to explicitly tell Windows through File Types to open the .doc extension in IE. Whether that works in Vista I have yet to visit. I’m sure there is a file types dialog somewhere in Vista. Having used Excel in a previous large web application, I knew it was possible to access Office applications.

1. I familiarized myself with the basics of Windows Server 2008. I’m familiar with 2003.

2. I was able to create a dev site on the server.

3. I installed on my local computer Visual Web Developer Express 2008 and created a website where I could create a button, and upon clicking the button open a Word document. This did involve adding a com reference to Word.

4. I then installed PHP 5.2.11 on the remote server. I test PHP, and for what I tested PHP works fine.

5. I used PHP to replicate the front end of my client’s site. That went fine, though the person who built the original site used Front-Page, so I had to strip out all that nonsense.

5. At that point, I have a server, I’ve installed PHP and tested it, I’m able to open a local Word document through a local .net site, and now I want to open a Word document using PHP.

6. I then tried opening a Word document through PHP running on the remote server.

7. I got an error, which lead me to installing Office on the server.

8. I received an access denied error which leads me to a permissions issue.

9. I then Installed, on the server, Visual C# Express, Visual Web Developer Express 2008 and created the same site as I had done on my local computer.

10. I got the same access error.

11. I went, on the server, to component services and looked at DCOM config. I did not find Word or any reference that I can find for Microsoft Word Application. Excel is there, Web Developer is there but Visual C# is also not there, which I had also installed.

12. Being a little discouraged, I found some (common to multiple posts) component services instructions (through google search), which had and “if that doesn’t work try this” part which are the steps I previously posted in this thread.

13. Having gone in added and added an Everyone account editing all the default permission settings for the Administrative user and the Everyone user, I got past the accessed denied error.

14. I am able to open a Word document through the vb.net code on the server by right clicking on the document in the Web Developer Solutions Explorer and select View in Browser. I can also do this on my local machine, so I publish the .net site and the page live on the net with the buttons opens, as expected, and pressing the button does not though, open an instance of Word.

15. I think it is just not displaying the Word doc.

16. So, I have a server with the .NET platform (standard stuff), a trial version of Office, PHP 5.2.11, and I cannot open a Word document through a PHP script. I can open a Word document through the Web Developer Solutions Explorer in .net locally and on the server, but not live on the net.

17. If I just run

<?php
// starting word
$word = new COM("word.application") or die("Unable to instantiate Word");
echo $word->Version;
?>

I get the version of Word, which is 12.0

18. Because I can do that, I do not think it is a dcom issue (I, of course could/probably be/am wrong).

19. I can access/start/connect to Word, but cannot open a Word document on the site.

I have been thinking more about the rich text way. Have a text parser on the site and have the documents readable by Word. I pretty sure I can get what I want done in .net, because I’ve seen it working on my client’s current leased system.

I just wish I could get PHP to even open a Word document of any kind.

Sorry for the length of this post.

mike
 
that's encouraging. don't worry about the file extensions. not relevant as you will be telling word itself to open the document.

the encouraging thing is the return of the version number. this means that you can actively connect to com to create an instance of word and that you can at least retrieve information about the instance.

i am hoping that making word visible will be something you can do without needing particular access credentials.

my assumption is that you don't need higher level credentials until you try to do something that is not idempotent.

can you run the following script please

Code:
<?php
ini_set('display_errors', true);
error_reporting(E_ALL);
$word = new COM("word.application");
$word->visible = true;
sleep (300);
?>

basically the intent is to let word load up (this can take an age on underpowered system) and then become visible. whilst not terminating the script for 5 minutes.

if we can get it to become visible that's a big start.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top