如何在 Word 中复制并粘贴一系列表格?
编辑:如果您有 VBA 示例,我会采纳。我只是想了解如何将 Range 对象与 Tables 集合一起使用来复制和粘贴多个表而不循环。换句话说,如何使用 Tables 集合指定 1..lastTable 的范围?如果我能看到一个有效的 VBA 示例,我将使用 VBA --> Perl 转换。
我正在尝试使用 Perl 的 Win32::OLE 模块(通过 Dave Roth 的优秀书)来自动执行一些我需要在某些 Word 文档上重复执行的任务。然而,本书(以及大多数网络示例)倾向于使用 Excel 作为示例,因此我不确定如何使用 Tables 集合对象有效地复制和粘贴。
这是我的代码片段:
my $originalDoc = $MSWord->Documents->Open('C:\Perl\testDocument.doc');
my $newDoc = $MSWord->Documents->Add;
my $selection = $MSWord->Selection(); # this may be spurious
my $Count = int( $originalDoc->Tables()->{Count} );
my $range = $originalDoc->Tables()->Range( { Start => $originalDoc->Tables(1)->{Range}->{Start},
End => $originalDoc->Tables($Count)->{Range}->{End}
} );
$range->Copy();
$newDoc->Range()->Paste();
原始代码使用段落,而不是表格,因此我假设一些错误是该代码的产物(或更可能是我对该代码的不理解)。
EDIT: If you have an example in VBA, I'll take it. I'm just trying to understand how to use the Range object with the Tables collection to copy and paste multiple tables without looping. Put another way, how can I specify a range of 1..lastTable using the Tables collection? If I can see a working VBA example of this, I'll work on the VBA --> Perl conversion.
I'm trying to use Perl's Win32::OLE module (via Dave Roth's excellent book) to automate a couple tasks I need to repeatedly perform on some Word documents. However, the book (and most web examples) tends to use Excel for examples, so I am not sure how to copy and paste effectively with the Tables collection object.
Here is a snippet of my code:
my $originalDoc = $MSWord->Documents->Open('C:\Perl\testDocument.doc');
my $newDoc = $MSWord->Documents->Add;
my $selection = $MSWord->Selection(); # this may be spurious
my $Count = int( $originalDoc->Tables()->{Count} );
my $range = $originalDoc->Tables()->Range( { Start => $originalDoc->Tables(1)->{Range}->{Start},
End => $originalDoc->Tables($Count)->{Range}->{End}
} );
$range->Copy();
$newDoc->Range()->Paste();
The original code used Paragraphs, not Tables, so I assume some of the bugs are artifacts from that code (or more likely my non-understanding of that code).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一次复制并粘贴一个表格可能会更好:
Copying and pasting tables one at a time might be preferable: