cormanaz has asked for the wisdom of the Perl Monks concerning the following question:

Good day Monks. I am trying to locate highlighted words in a ms-word document and the page numbers on which they reside. I am having trouble getting the page number. I am trying it like this:
use Win32::OLE::Const 'Microsoft Word'; $filename = "c:\\work\\test.doc"; $word = Win32::OLE->new('Word.Application', 'Quit'); $doc = $word->Documents->Open($filename) or die "Can't open: $!\n"; for(my $i = 1; $i <= $doc->Words->Count; $i++) { if($doc->Words->Item($i)->HighlightColorIndex > 0) { $page = $doc->Words->Item($i)->Application->Selection + ->Information(wdActiveEndPageNumber); print "$page\n"; } }
but when I run it on a file with one word highlighted on each of three pages, it returns 1 for the page number three times. Anyone know what I'm doing wrong?

TIA...Steve

Replies are listed 'Best First'.
Re: Win32::OLE: How to get the page number from ms-word?
by NetWallah (Canon) on Mar 25, 2006 at 06:10 UTC
    I have written a small amount of code using the msWord Object automation - based on that experience, I think you need to select the word, then get the Information you need.

    Something like this (Not syntax checked, object model not verified, and not tested:

    ## Within the for loop -- my $selection = $doc->Words->Item($i)->Select(); $page = $selection->Information(wdActiveEndPageNumber);

         "For every complex problem, there is a simple answer ... and it is wrong." --H.L. Mencken

      Very close - I was curious so I verified this version (Select doesn't appear to return anything, so you have to create the selection first, then ask the document for information about it later):
      $doc->Words->Item($i)->Select; my $page = $word->Selection->Information(wdActiveEndPageNumber);

      --
      I'd like to be able to assign to an luser

Re: Win32::OLE: How to get the page number from ms-word?
by Madams (Pilgrim) on Mar 26, 2006 at 03:47 UTC
    Please see my related node here. This is nearly a frequent question from an OLE view point.


    "All too often people confuse their being able to think with their actually having done so. A more pernicious mistake does not exist."

    --Moraven Tollo in Michael A. Stackpole's A Secret Atlas

    My Unitarian Jihad Name is: Sibling Pepper Spray of Loving Kindness. Get yours.