Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/311099-php-error-message/
Share on other sites

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.

 

 

Link to comment
https://forums.phpfreaks.com/topic/311099-php-error-message/#findComment-1579708
Share on other sites

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);
}

 

Link to comment
https://forums.phpfreaks.com/topic/311099-php-error-message/#findComment-1579734
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.