I guess there a few different ways you can attack this. First let's get all the basic stuff out of the way.
use strict; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Word'; $Win32::OLE::Warn = 3; # Die on Errors my $Word = Win32::OLE->GetActiveObject('Word.Application') || Win32::OLE->new('Word.Application', 'Quit'); $Word->Documents->Add || die("Unable to create document ", Win32::OLE- +>LastError()); my @Data = ("Data ONE", "Data TWO", "Data THREE" , "Data FOUR");
Those are some necessary Evils, but always important to include.

If you just want to add plain header text, and then add a table below it, I would do this:

$Word->Selection->TypeText ({ Text => "This is plain header text"}); $MyRange->Collapse({Direction=>wdCollapseEnd}); $Word->Selection->TypeText ({ Text => "\n\n" }); #add the table 2 li +nes below $Word->Selection->MoveDown({Count => 2});
If you want TABLE headers, you might want to try something like what follows. If not, just use the text above, and the table creation method from below and add your data:
my @headers = ("Header ONE", "Header TWO", "Header THREE" , "Header FOUR"); my $MyRange = $Word->ActiveDocument->Content; # Add a table at the end of the content. $Word->ActiveDocument->Tables->Add({ Range => $MyRange, NumRows => 1, NumColumns => ($#headers+1), }); #put headers In foreach my $str (0..$#headers){ my $Cell = $Word->ActiveDocument->Tables(1)->Cell(1,($str+1))->Sel +ect; $Word->Selection->TypeText ({ Text => $headers[$str]}); } #Do some simple styling with the header row. my $Row = $Word->ActiveDocument->Tables(1)->Rows(1); $Row->Shading->{BackgroundPatternColor} = wdColorGray40; my @borders = (wdBorderBottom, wdBorderTop, wdBorderLeft, wdBorderRigh +t); foreach my $bord (@borders){ with (my $Borders = $Row->Borders->Item($bord), LineStyle=>wdLineStyleSingle, LineWidth=>wdLineWidth300pt);} my $table = $Word->ActiveDocument->Tables(1); my $lastrow = $table->Rows->{Count}; # Put the data in, based on the number of header columns while ($lastrow < 15){ my $color; $table->Rows($lastrow)->Select(); $Word->Selection->InsertRowsBelow(1); $lastrow++; my $test = $lastrow%2; if (!$test){$color = wdColorWhite;}else{$color = wdColorGray40;} $Word->ActiveDocument->Tables(1)->Rows($lastrow)->Shading->{Backgr +oundPatternColor} = $color; foreach my $str (0..$#headers){#put Headers In my $Cell = $Word->ActiveDocument->Tables(1)->Cell($lastrow,($s +tr+1))->Select; $Word->Selection->TypeText ({ Text => $Data [$str]}); } } with (my $Borders = $Row->Borders->Item(wdBorderBottom), LineStyle=>wdLineStyleSingle, LineWidth=>wdLineWidth300pt); my $Doc= $Word->{ActiveDocument}; $Doc->SaveAs('c:\perl\projects\testtable.doc');

C-.

---
Flex the Geek


In reply to Re: MSWord and OLE by cacharbe
in thread MSWord and OLE by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.