Accessing host MS COM system from Laravel service (hosted by Apache)

I have a mystery on my hands and hoping someone may have some experience with determining what the problem might be here.
I have a web service build with Laravel (actually Lumen) which has an endpoint that takes, among other things, a list of files as payload.
It stores these files locally with a uniquely identified directory, e.g.,

and from here, it will process any word files (doc or docx) by attempting to get the page count of the file.
This is accomplished by using the COM service like this:

public static function getWordDocPageCount(string $fileName): int {
        $word = new COM("Word.Application") or die("unable to instantiate Word");
        $word->visible = false;
        $pageCount = $word->ActiveDocument->ComputeStatistics(self::WORD_STATISTIC_PAGES);
        $word = null;
        return $pageCount;

Of course this has the dependency that the host system has a properly installed and functioning Office installation.

Tests of this on my local system is working as expected. We also have a staging server where this is running perfectly as well.

However, on our production server when attempting this, we’re getting a runtime exception like this:

<b>Source:</b> Microsoft Word<br/><b>Description:</b> This command is not available because no document is open.

on its attempt to call ComputeStatistics , which indicates to me that the host preventing the file from being loaded into the word instance of that office installation.

The admin has tested the host system in “standalone mode” from running this php script:

       $fileName = {fully qualified file location to a word (doc or docx) file)}
        $word = new COM("Word.Application") or die("unable to instantiate Word");
        $word->visible = false;
        $pageCount = $word->ActiveDocument->ComputeStatistics(self::WORD_STATISTIC_PAGES);
        $word = null;
        echo $pageCount;

and run either directly from a php prompt or using the interactive php mode this works fine.
so it would appear that both PHP, COM and Office are functioning as expected, at least if run from the context of the admin user on that system.

As I’m not allowed access this production server personally and unable to perform any further diagnostics directly, but the admin insists that the production server and associated dependencies are
configured exactly as they are on the staging system.
Obviously this can’t be true or we wouldn’t have this problem…
The only remaining possibility that I can come up with is the permissions of the Apache service running on that host isn’t recognized as having the proper permissions to load a file into that host’s Word application.
Are there any other possibilities/ideas of what else this might be and best approach(es) to debug and rectify this condition?
I guess another approach would find a solution that does not try to use host resources in order to obtain page counts of a word document, but I can find no such reliable mechanism for this.

Well, Lumen server is not a Windows server as far as I know. Try checking either phpinfo() on the live server or run this line to see what the server is:


It will tell you what type of server it is running on. If the server is not a Windows or IIS server, then you can not use the commands you show in your code. In that case, you have to use a Word library. There are dozens out there that will let you open a Word doc and get page counts. One is PHPword another is PHPoffice… Since these are PHP libraries, you can just copy them to the live server and use them to do what you are attempting without needing word installed on the server. Should work for you!

Huh? Lumen/Larvel is a application framework for php, not a “server”. The server in this case is Apache… it is hosting this Lumen/Laravel web appication.

Yes and no! It’s on a Apache system therefore Windows programs do not run on it. Out of luck doing it that way. Just install another library for Word, such as this one: PHPWord

Then, you will need to use that library on your three servers, so you will need to install them on each of your home test server, your staging server and the live server. You can just use the manual install and copy it to all three.

Remember, Apache by itself does not support Microsoft’s Office suite…

Sorry, I’m obviously not making myself clear on my issue to you.
Libraries that you’re referring do nothing more than wrap the com services on the hosting server (which are windows systems) and these too suffer from the same issue. I simplified my code to eliminate the overhead of a 3rd party package like phpword or phpdocx and make the same exact COM calls they would for performing this same process. I’ve already tried these, this is not the issue, but thanks.

I have three Linux Apache servers running live. I use Word docs on them all the time. They do not use COM at all. You can use COM, but, for the Word file reading and decoding or creation, you use a library of code that lets you access the files. They work well with Word files and can open, read and create them. But, I can not run $word = new COM(“Word.Application”) calls on any of them.

Component Object Model (COM) communicates between different languages, applications or platforms. But, they MUST be installed on the server to do so. You can NOT use COM to access Word or Office applications without them being installed on your server. You said it was an Apache server. No Word is installed on it. Can’t call it with COM. Sorry.

If you find out differently, please let me know. I researched it over and over and I can not locate any type of wrapper for Word on Apache servers. You must use a PHP written library that can read/write Word docs. PHPWord

Office IS installed on all of these platforms and has been confirmed as installed and working as expected.

Sponsor our Newsletter | Privacy Policy | Terms of Service