问题描述
我有一个(一组)Word文档,我正在尝试使用Perl中的Win32 :: OLE获得各种属性(页数,作者等):
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";
这将返回4页.但是文档中的实际页数是9.第一节中的页数是4.我想要文档中的总页数.
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.
如果在Word VBA中,我执行以下操作:
If, within Word VBA, I do the following:
MsgBox ActiveDocument.BuiltInDocumentProperties("Number of pages")
显示9.属性/统计"页面中显示的页面数为9.
This displays 9. The number of pages displayed in the Properties/Statistics page is 9.
我必须强制重新计算吗?有什么方法可以要求OLE库强制重新计算,还是我必须分别对待每个部分?
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?
我使用的是XP,Word 2007,ActivePerl v5.10.0.
I'm on 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;
}
这篇关于为什么在Perl和Word VBA中,Word文档中的页数不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!