Hello Fellows,

I've been painfully trying to work with Visual Basic's odd coding idiosyncracies in order to output Word documents using Win32::OLE. I have infact been able to create a Perl script to output text by making calls to a Mysql database. It seems to work, however, I've been unable to output word tables. Any suggestion or pointers would be appreciated. I've included the "crude coding" done thus far below.

use Win32::OLE; use DBI; #//set up the spec for DBI $db_type = 'mysql'; $db_name = "jcu2"; $db_host_name = '127.0.0.1'; #$db_port = '3306'; $db_spec = "DBI:$db_type:$db_name:$db_host_name"; #//connect to the database my $dbh = DBI->connect($db_spec, undef, undef) or (print "Couldn't + connect to $db_spec: $DBI::errstr\n" and die()); my $Class = "Word.Application"; my $File = "c:\\Windows\\Desktop\\text.doc"; my $Word = Win32::OLE->GetActiveObject( $Class ); if( ! $Word ) { $Word = new Win32::OLE( $Class, \&Quit ) || die "Can not create a '$Class' object.\n"; } # By default a Word COM object is invisible (not # displayed on the screen). # Let's make it visible so that we can see what we # are doing… $Word->{Visible} = 1; my $Doc = $Word->Documents->Add(); #Prepare the DBI for fetching and execution my $sth=$dbh->prepare("SELECT strengths,weaknesses,recommendations FRO +M RawText"); $sth->execute or die "Can't prepare SELECT sql statement:$DBI::errstr\ +n"; #Loop through the fetched data while (($s,$w,$r) =$sth2->fetchrow_array) { #Assign the fetched data to a series of VB calls #This in fact outputs the entire contents of the call made #to the mys +ql table my($range) = $Doc -> {Content}; $range -> InsertAfter($s); $range -> InsertAfter($w); $range -> InsertAfter($r); #I would like to now output the data in a series of #tables if possible $Word->$range->Tables->Add(2,2); #Doesn't work for starters } $Doc->SaveAs( $File ); $Doc->Save(); print "Hit enter to continue...\n"; <STDIN>; $Doc->Close(); $sth->finish; $dbh->disconnect; sub Quit { my( $Obj ) = @_; $Obj->Quit(); }

In reply to MS WORD TABLES WITH WIN32::OLE by camel's_back

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.