问题
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.
回答1:
#!/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;
}
来源:https://stackoverflow.com/questions/820348/why-are-the-number-of-pages-in-a-word-document-different-in-perl-and-word-vba