为什么 Perl 和 Word VBA 中的 Word 文档页数不同?

发布于 2024-07-19 01:52:35 字数 526 浏览 4 评论 0原文

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

极度宠爱 2024-07-26 01:52:35
#!/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;
}
#!/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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文