0
votes

I am using a PHP COM Object to Open a Word 2013 file and count pages. My code works great for .docx files but not for .docm files. I am using php 5.4 and IIS 7.5 in a Windows 2008 R2 environment. Any recommendations?

// Create an instance of the Word application
$word = new COM("word.application");
if ($word) {
    // Open the document
    $word->Documents->Open($file_dir . $filename);

    // Get the page count
    $pagecount = $word->Documents[1]->ComputeStatistics(2);
} 
2

2 Answers

0
votes

You could try to turn off all macros like this before opening a file, this also disable security prompts:

 $word->AutomationSecurity = 3;   // disable all macros in the documents being opened
0
votes

Well, doesn't .docm signify that there are macros or event macros present in the file?

Usually when the user opens a .docm file, the user is given a security prompt. Being that you are running under PHP, you might be running under a service and the prompt is not being seen in the hidden desktop, or security it preventing it from running at all. I would suggest you write some VBS code to try and open the same file under a console and see what the results are. If you get prompts or its being prevented, hopefully you will get better error messages.

var word
set word = CreateObject("Word.Application")

word.documents.open("path to my file")

var pageCount
pageCount = word.Documents(1).ComputeStatistics(s)