Word 2007 COM - Can't directly access a page when word is set to invisible
Posted
by Robbie
on Stack Overflow
See other posts from Stack Overflow
or by Robbie
Published on 2010-06-14T14:28:36Z
Indexed on
2010/06/14
14:32 UTC
Read the original article
Hit count: 417
I'm using Word 2007 via COM from PHP 5.2 Apache 2.0 on a windows machine. The goal is to programmatically render jpeg thumbnails from each page in a Word document.
The following code works correctly if you set $word->Visible to 1:
try {
$word = new COM('word.application');
$word->Visible = 0;
$word->Documents->Open("C:\\test.doc");
echo "Number of pages: " . $word->ActiveDocument->ActiveWindow->ActivePane->Pages->Count() . "</br>";
$i = 1;
foreach ($word->ActiveDocument->ActiveWindow->ActivePane->Pages as $page) {
echo "Page number: $i </br>";
$i++;
}
//get the EMF image of the page
$data = $word->ActiveDocument->ActiveWindow->ActivePane->Pages->Item(3)->EnhMetaFileBits;
$word->ActiveDocument->Close();
$word->Quit();
} catch (Exception $e) {
echo "Exception: " .$e->getMessage();
}
The test document I'm using contains 35 pages. The code will display the correct number of pages but the for each loop only loops over 1 page. I can only directly access page 1 and 2 in the Pages->Item() collection. If I try to access another page I get the exception: "The requested member of the collection does not exist."
If I set the $word->Visible property to 1 I do get all the pages in the foreach loop and I can access any page directly. Everything is working as expected if Word is set to be visible.
Even stranger is the fact that if I set Word to be invisible and I don't have the foreach loop I can only access page 1 instead of page 1 and 2 if I do the for each loop.
Any pointers on how I can access all the pages in the document and keeping word invisible?
© Stack Overflow or respective owner