我有一组(一组)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。

有帮助吗?

解决方案

#!/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;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top