2

PerlでWin32::OLEを使用してさまざまなプロパティ(ページ数、作成者など)を取得しようとしている(一連の)Wordドキュメントがあります。

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、ActivePerlv5.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 に答える