Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

How can I copy and paste a range of tables in Word with Win32::OLE?

by romandas (Pilgrim)
on Sep 16, 2009 at 19:28 UTC ( #795700=perlquestion: print w/replies, xml ) Need Help??
romandas has asked for the wisdom of the Perl Monks concerning the following question:

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->Ta +bles(1)->{Range}->{Start}, End => $originalDoc->Ta +bles($count)->{Range}->{End} } ); $range->Copy(); $newDoc->Range()->Paste();

I'm getting a rather strange 'not enough storage to complete this action' error. How can I properly copy a range of tables from $originalDoc to $newDoc?

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).

  • Comment on How can I copy and paste a range of tables in Word with Win32::OLE?
  • Download Code

Replies are listed 'Best First'.
Re: How can I copy and paste a range of tables in Word with Win32::OLE?
by jrsimmon (Hermit) on Sep 16, 2009 at 20:18 UTC
    Record a macro of the actions you want performed in Word, then convert the resulting vbscript to perl and Win32::OLE.
      ... achieving the conversion using this guide, or possibly the VBScript utility distributed with the ActiveState PDK. ... depending on your Windoze distribution (of choice).

      A user level that continues to overstate my experience :-))

      Using this method (via the Macro recorder) just shows me how to use Selection to obtain one table, whereas if I try to select multiple tables, the macro is written to grab the text between them as well, which is not what I'm after. Do you have any insight into how to use a range of multiple tables with the Tables collection?

      Put another way, how can I specify a range of 1..lastTable using the Tables collection instead of looping? Dave Roth has an excellent example using Paragraphs, but I don't seem to be able to use Tables in the same way, likely a result of my own misunderstanding.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://795700]
Approved by ikegami
Front-paged by tye
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2017-04-28 02:37 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (517 votes). Check out past polls.