aflatooni Posted July 16, 2020 Share Posted July 16, 2020 I have this php code to load a word document : sample.docx //define a variable define('wdPropertyTitle', 1); //instantiate the object OK $word = new COM("word.application") or die ("Could not initialise MS Word object."); // check Class print_r(get_class($word)); // OK dispalys Class_name COM // load a document //$word->Documents->Open(realpath("Sample.docx")); $word->Documents->Open("sample.docx"); // No Exception up to here //check the object echo "<pre>"; print_r($word); // respose: object ( ) echo "</pre>"; // get properties $Print = $word->ActiveDocument->BuiltInDocumentProperties(wdFieldPrint); ?> Question: When I run the script, I get the following exception: PHP Fatal error: Uncaught com_exception: <b>Source:</b> Microsoft Word<br/><b>Description:</b> This command is not available because no document is open. in C:\phpFolder\php_codes\class_COM.php:75 Stack trace: #0 {main} thrown in C:\phpFolder\php_codes\class_COM.php on line 75 Do you have any solution to get this thing done?, please PS: I have : win 10, php 7.4.1, IIS 10 , php_com_dotnet is active in php.ini, Dcom Configured in windows snap-in Appreciated Mohamad Aflatooni Quote Link to comment https://forums.phpfreaks.com/topic/311099-php-error-message/ Share on other sites More sharing options...
gw1500se Posted July 16, 2020 Share Posted July 16, 2020 You need to be using try/catch with these calls to handle and identify errors. I'm guessing the open is failing because the path to the file is wrong. Quote Link to comment https://forums.phpfreaks.com/topic/311099-php-error-message/#findComment-1579680 Share on other sites More sharing options...
aflatooni Posted July 16, 2020 Author Share Posted July 16, 2020 7 hours ago, aflatooni said: I have this php code to load a word document : sample.docx //define a variable define('wdPropertyTitle', 1); //instantiate the object OK $word = new COM("word.application") or die ("Could not initialise MS Word object."); // check Class print_r(get_class($word)); // OK dispalys Class_name COM // load a document //$word->Documents->Open(realpath("Sample.docx")); $word->Documents->Open("sample.docx"); // No Exception up to here //check the object echo "<pre>"; print_r($word); // respose: object ( ) echo "</pre>"; // get properties $Print = $word->ActiveDocument->BuiltInDocumentProperties(wdFieldPrint); ?> Question: When I run the script, I get the following exception: PHP Fatal error: Uncaught com_exception: <b>Source:</b> Microsoft Word<br/><b>Description:</b> This command is not available because no document is open. in C:\phpFolder\php_codes\class_COM.php:75 Stack trace: #0 {main} thrown in C:\phpFolder\php_codes\class_COM.php on line 75 Do you have any solution to get this thing done?, please PS: I have : win 10, php 7.4.1, IIS 10 , php_com_dotnet is active in php.ini, Dcom Configured in windows snap-in Appreciated Mohamad Aflatooni 1-I tried "try an catch" as follows, ..didn't get any error!. //-----------------------------------------------try exception try { $word = new COM("word.application"); $filename="C:/phpFolder/php_codes/Sample.docx"; $word->Documents->Open($filename); } catch (com_exception $e) {print $e;} // displays no error //------------------------------------------------end try exception 2-The error is solely due to these statements that I tried again: $Print = $word->ActiveDocument->BuiltInDocumentProperties(wdFieldPrint); $word->Selection->TypeText("This is a test..."); // function does not exist $word->Documents[1]->SaveAs("Useless test.docx"); // function does not exist $Title = $word->ActiveDocument->BuiltInDocumentProperties(wdPropertyTitle); $content = (string) $word->ActiveDocument->Content; print $content; any idea?, appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/311099-php-error-message/#findComment-1579708 Share on other sites More sharing options...
kicken Posted July 16, 2020 Share Posted July 16, 2020 You need to make sure to exit word when you're done, otherwise it will hang around in the background causing issues. wdFieldPrint / wdPropertyTitle don't mean anything. These are constants that have a particular value so you either need to define them or use the real value in their place. wdFieldPrint isn't something you'd look for in BuiltInDocumentProperties either Make sure you use a full path to the document. When I tried a relative path it looked in C:\Windows\System32 rather than the current directory. Rather than using ActiveDocument, you could just use the value returned from your open call. <?php const wdPropertyPages = 14; const wdPropertyWords = 15; const wdPropertyTitle = 1; $word =new COM('Word.Application'); try { $document = $word->Documents->Open('W:\\test.docx'); $value = $document->BuiltInDocumentProperties[wdPropertyPages]; var_dump((string)$value); $value = $document->BuiltInDocumentProperties[wdPropertyWords]; var_dump((string)$value); $value = $word->ActiveDocument->BuiltInDocumentProperties[wdPropertyTitle]; var_dump((string)$value); } finally { $word->Quit(0); } Quote Link to comment https://forums.phpfreaks.com/topic/311099-php-error-message/#findComment-1579734 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.