Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

karlkras's avatar

Issue accessing COM (microsoft) service on host server from lumen web service.

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 built 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.,

[my-service]/uploads/{guid}/files

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;
        $word->Documents->Open($fileName);
        $pageCount = $word->ActiveDocument->ComputeStatistics(self::WORD_STATISTIC_PAGES);
        $word->ActiveDocument->Close();
        $word->Quit();
        $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 is 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:

<?php
       $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;
        $word->Documents->Open($fileName);
        $pageCount = $word->ActiveDocument->ComputeStatistics(self::WORD_STATISTIC_PAGES);
        $word->ActiveDocument->Close();
        $word->Quit();
        $word = null;
        echo $pageCount;

and when 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.

I'm not allowed access to 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 server (running as a service) 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.

Any help on this would be greatly appreciated!

0 likes
0 replies

Please or to participate in this conversation.