【问题标题】:Perl Win32::OLE word merging two doc filesPerl Win32::OLE word 合并两个 doc 文件
【发布时间】:2015-02-24 18:13:17
【问题描述】:

我正在尝试将两个文档合并到新的 doc 文件中,但在以下代码中出现错误。

    use strict;
    use Win32::OLE;
    use Win32::OLE::Const 'Microsoft Word';

    $meta_file_path = "D:\\copyfrom.doc";
    $main_file_path = "D:\\copyto.doc";

    my $x = Win32::OLE->GetActiveObject('Word.Application') ;
    my $word = Win32::OLE->new('Word.Application', sub { $_[0]->Quit; } );
    my $doc = $word->Documents->open($meta_file_path) or die $!;

        my $doc = $word->Documents->Open($meta_file_path) or die $!;
$word->ActiveDocument->Content;
$word->ActiveDocument->Select();
$word->Selection->Copy();
$doc->Close;
my $doc2 = $word->Documents->Open($main_file_path) or die $!;       
$word->ActiveDocument->Content->Paste();
$word->ActiveDocument->SaveAs('D:\\outdoc.doc');
$doc2->Close;       
exit;

错误是:

Win32::OLE(0.1709) error 0x80020011: "Does not support a collection"
    in METHOD/PROPERTYGET "" at D:/merge.pl line 12.
Can't call method "Content" on an undefined value at D:/merge.pl line 12.

我从perlmonks 获得了一个代码,它给了我想要的输出,但它们的格式存在一个问题,copyfrom.doc 的表格和文本与 copyto.doc 具有不同的对齐方式和字体系列。

【问题讨论】:

  • same question你已经问过了。
  • @serenesat 是的,但以前的问题没有解释问题,这就是我添加另一个问题的原因请指导我

标签: perl


【解决方案1】:

您可能试图将当前文档用作 Documents 集合的一部分,而它实际上是 Word 应用程序对象的一部分。 我没有办法在这里测试代码,但尝试更改

$doc->ActiveDocument

$word->ActiveDocument

【讨论】:

  • 我进行了更改,但仍然没有得到正确的输出。更改 $word 使其没有错误,但它不会合并两个文档,它只是保存为第一个文档...请指导
  • 如果解决方案解决了您的错误,您应该将其标记为答案。至于没有将任何内容复制到输出文件的事实,请尝试使用 SelectAll 而不是 Select。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-07
  • 1970-01-01
  • 1970-01-01
  • 2012-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多