views:

509

answers:

2

I have a (set of) Word document(s) for which I'm trying to get various properties (number of pages, author, etc) using Win32::OLE in Perl:

print $MSWord->Documents->Open($name)->
BuiltInDocumentProperties->{"Number of pages"}->value . " \n";

This returns 4 pages. But the actual number of pages in the document is 9. The number of pages in the first section is 4. I want the total number of pages in the document.

If, within Word VBA, I do the following:

MsgBox ActiveDocument.BuiltInDocumentProperties("Number of pages")

This displays 9. The number of pages displayed in the Properties/Statistics page is 9.

Do I have to force a recalculate? Is there some way to ask the OLE library to force a recalculate or do I have to treat every section separately?

I'm on XP, Word 2007, ActivePerl v5.10.0.

A: 

Is the Perl response always consistent with the page count of the first section? I'm wondering if page-count is dependent upon the currently selected printer/setup, and the "Perl printer" (whatever that is) is setup differently than the actual one used by Word itself.

jimtut
+3  A: 
#!/usr/bin/perl

use strict;
use warnings;

use File::Spec::Functions qw( catfile );

use Win32::OLE;
use Win32::OLE::Const 'Microsoft Word';
$Win32::OLE::Warn = 3;

my $word = get_word();
$word->{Visible} = 1;

my $doc = $word->{Documents}->Open(catfile $ENV{TEMP}, 'test.doc');
$doc->Repaginate;

my $props = $doc->BuiltInDocumentProperties;
my $x = $props->Item(wdPropertyPages)->valof;
print "$x\n";

$doc->Close(0);

sub get_word {
    my $word;
    eval {
        $word = Win32::OLE->GetActiveObject('Word.Application');
    };

    die "$@\n" if $@;

    unless(defined $word) {
        $word = Win32::OLE->new('Word.Application', sub { $_[0]->Quit })
            or die "Oops, cannot start Word: ", 
                   Win32::OLE->LastError, "\n";
    }
    return $word;
}
Sinan Ünür
This works. The $doc->Repaginate was what I was after. Thanks.What is the difference between ->valof and ->value?
MatthieuF
I hate to admit it. I am not too sure about the difference between valof and value or even {Value}. The script I posted is adopted from something else I had written.Now that you mentioned it, $doc->BuiltInDocumentProperties->Item(wdPropertyPages)->{Value}; $doc->BuiltInDocumentProperties->Item(wdPropertyPages)->Value; $doc->BuiltInDocumentProperties->Item(wdPropertyPages)->valof;all give the same result. This is something to look into but, just for aesthetic reasons, I would stick with ->Value.
Sinan Ünür