2

我有一个(一组)Word 文档,我试图在 Perl 中使用 Win32::OLE 获取各种属性(页数、作者等):

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

这将返回 4 页。但是文档的实际页数是9。第一节的页数是4。我想要文档的总页数。

如果在 Word VBA 中,我执行以下操作:

MsgBox ActiveDocument.BuiltInDocumentProperties("Number of pages")

这将显示 9。“属性/统计”页面中显示的页面数为 9。

我必须强制重新计算吗?有什么方法可以让 OLE 库强制重新计算,还是我必须分别处理每个部分?

我在 XP、Word 2007、ActivePerl v5.10.0 上。

4

1 回答 1

4
#!/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;
}
于 2009-05-04T19:26:30.267 回答