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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |