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

I am trying to use the following code to grab the number of pages from a word document. I get the following error:
Unable to open property Win32::OLE(0.1501) error 0x80020011: "Does not + support a collection" in METHOD/PROPERTYGET "" at pdf_testing.pl line 31.
in the MS doc I found that I should be able to use
object.Information(wdNumberOfPagesInDocument)
I translated as below and several other permutations for Perl. If anyone can help me on this, I would appreciate it.
use strict; use Win32::OLE; use Win32::OLE::Const 'Microsoft Word'; my $wd = Win32::OLE::Const->Load("Microsoft Word 10.0 Object Library") +; my ($pages,$doc); my $Word = Win32::OLE->new('Word.Application', 'Quit'); # $Word->{'Visible'} = 1; # if you want to see what's going on my $doc = $Word->Documents->Open("P:\\My DOCUMENTS\\test.doc") || die("Unable to open document ", Win32::OLE->LastError()); $pages = $doc->Information($$wd{'wdNumberOfPagesInDocument'}) || die(" +Unable to open property ", Win32::OLE->LastError()); print "The number of pages in test.doc are: $pages"; exit;

g_White

Replies are listed 'Best First'.
Re: Win32::OLE + word page count
by Rudif (Hermit) on Aug 15, 2001 at 01:51 UTC
    Hi gwhite

    The script below works for me.
    If you are tempted by VB/Word programming :-(, this site offers some guidance.
    #!perl -w use strict; use Win32::OLE; use Win32::OLE::Const 'Microsoft Word'; #my $wd = Win32::OLE::Const->Load("Microsoft Word 10.0 Object Library" +); my $wd = Win32::OLE::Const->Load("Microsoft Word 9.0 Object Library"); my $Word = Win32::OLE->new('Word.Application', 'Quit'); #$Word->{'Visible'} = 1; # if you want to see what's going on my $doc = $Word->Documents->Open("C:\\temp\\test.doc") || die("Unable to open document ", Win32::OLE->LastError()); my $sel = $Word->Selection; $sel->WholeStory; my $pages = $sel->Information($wd->{'wdNumberOfPagesInDocument'}) || d +ie("Unable to open property ", Win32::OLE->LastError()); print "The number of pages in test.doc are: $pages\n";
    HTH
    Rudif
      HEY THAT IS SLICK!!
Re: Win32::OLE + word page count
by John M. Dlugosz (Monsignor) on Aug 15, 2001 at 01:56 UTC
    You get that strange error in many cases. Usually means that the member doesn't exist in that object. Sometimes it's just cranky, it seems.

    In VB, intermediate hops are sometimes omitted, so check for "default property" when converting to another language.

    BTW, you don't have to explicitly fire up a word Application. You can just load the filename as an OLE object.

Re: Win32::OLE + word page count
by rchiav (Deacon) on Aug 15, 2001 at 21:01 UTC
    Just so you're aware of why your original script doesn't work, and the one posted by Rudif does, it's because there's no "Information" property available for a 'Document'. It's only available for selections.

    Rich