lostcause has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I'm trying to generate a report with MSWord and have a few small questions.
I've checked the super search feature and the internet in general both of which
have helped me write the following code but not answered my questions.

I wish to set the heading parameters of sections of text so that I can format the document.
The visual Basic script for this command looks like this:

Selection.Style = ActiveDocument.Styles "Heading 1" Selection.TypeText Text:="The Title"

I've tired many variations on the theme hoping to find the right combination of commands.
For example:

$Word->Selection->TypeText ({ Text => "The Title", Style => 'Heading 1', });

Anyone know how to do this?

My second question is with table generation. I can make a table and populate it with data just fine.
But if I sort the data before writing it to the table the script gives an error:

Win32::OLE(0.1501) error 0x80020005: "Type mismatch"
in METHOD/PROPERTYGET "TypeText" argument "Text" at C:\Dev\ResultProcessor\w
ordtabelbasic.pl line 69

Although Perl does not care if the data are text or numbers Word seems to.
I can work around this problem but would like to do it properly if I can.

Here is some sample code that illustrates the table problem It also places a picture in the document,
you will need to change the path to your 'My Pictures' folder for this to work or comment the code out.

#!/usr/bin/perl -w #Check that the path to the picture is correct on line 53 use strict; use Win32::OLE qw(in with); use Win32::OLE::Const; $Win32::OLE::Warn = 2; # Throw Errors, I'll catch them my $Word = Win32::OLE->new('Word.Application', 'Quit'); $Word->{'Visible'} = 1; # if you want to see what's going on $Word->Documents->Add || die("Unable to create document ", Win32::OLE->LastError()); # $Word->Selection->TypeText ({ Text => "The Title"}); #Generate and sort tabel information my %mass_info_hash= ("5", "25","10", "100","15", "225","20", "400","25 +", "625", "35", "1225","40", "1600","45", "2025","50", "2500","55", "3025"," +60", "3600","65", "4225","70", "4900","75", "5625","80", "6400","85", " +7225", "90", "8100","95", "9025"); my @mass_list = keys %mass_info_hash; #copy mass list over to new variabel before sorting my @mass_list_sorted = @mass_list; @mass_list_sorted = sort{$a<=>$b}@mass_list_sorted; my $rows = ($#mass_list) +2; # create table... $Word->ActiveDocument->Tables->Add({ Range => $Word->Selection->Range, NumRows => $rows, NumColumns => 2, }); # crude way of inserting text into table... #print tabel headers $Word->Selection->TypeText ({ Text => "Mass"}); #zero $Word->Selection->MoveRight({Count => 1}); $Word->Selection->TypeText ({ Text => "Intensity" }); #print hash info foreach my $mass( @mass_list){ $Word->Selection->MoveRight({Count => 1}); $Word->Selection->TypeText ({ Text => $mass }); $Word->Selection->MoveRight({Count => 1}); $Word->Selection->TypeText ({ Text => $mass_info_hash{$mass} }); } $Word->Selection->MoveRight({Count => 1}); # Next space is outside table #insert picture $Word->Selection->TypeText ({ Text => "\nPicture\n" }); #change the path to point to your user folder. $Word->Selection->InlineShapes->AddPicture ({ FileName => 'C:\Documents and Settings\Richard\My Documents\My Pic +tures\sample.jpg', LinkToFile=>'False', SaveWithDocument=>'True', }); #Make second tabel with sorted mass list $Word->Selection->TypeText ({ Text => "\n\n" }); # create table... $Word->ActiveDocument->Tables->Add({ Range => $Word->Selection->Range, NumRows => $rows, NumColumns => 2, }); $Word->Selection->TypeText ({ Text => "Mass"}); #zero $Word->Selection->MoveRight({Count => 1}); $Word->Selection->TypeText ({ Text => "Intensity" }); foreach my $mass( @mass_list_sorted){ $Word->Selection->MoveRight({Count => 1}); $Word->Selection->TypeText ({ Text => $mass }); $Word->Selection->MoveRight({Count => 1}); $Word->Selection->TypeText ({ Text => $mass_info_hash{$mass} }); } $Word->Selection->MoveRight({Count => 1});

Replies are listed 'Best First'.
Re: Scripting MSWord - headers and tabels
by cacharbe (Curate) on Oct 25, 2001 at 00:09 UTC
    Instead of:
    $Word->Selection->TypeText ({ Text => "The Title", Style => 'Heading 1', });
    Try (assumes:use Win32::OLE::Const 'Microsoft Word';)
    $Word->Selection->{Style} = wdStyleHeading1; $Word->Selection->TypeText ({ Text => "The Title"});
    Update: I thought an explanation was in order:

    TypeText only takes one arg, and that's Text. You want to set the style of the current selection, which you can do using one of the Style Constants through the Style property, although it is an object in it's own right.

    C-.

      hmmm.

      Well, I used the following code as a test table (pardon the pun). It was written a while ago for someone on one of the AS Win32 Perl Mailing lists, and I didn't receive any errors.

      Machine is Win2K Advanced Server with AS Perl 5.6.1.629.

      use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Word'; $Win32::OLE::Warn = 3; # Fail on errors my $Word = Win32::OLE->GetActiveObject('Word.Application') || Win32::OLE->new('Word.Application', 'Quit'); $Word->{'Visible'} = 1; $Word->Documents->Add || die("Unable to create document ", Win32::OLE- +>LastError()); $MyRange = $Word->ActiveDocument->Content; $country_name = "Unknown"; # I'll fill this in later # add a table that is the header portion, 1 column by 1 row $Word->ActiveDocument->Tables->Add({ Range => $MyRange, NumRows => 1, NumColumns => 1, }); $Word->Selection->{Style} = wdStyleHeading1; $Word->Selection->TypeText ({ Text => "Hosts found in $country_name"}) +; $Word->Selection->MoveRight({Count => 2}); $Word->Selection->TypeText ({ Text => "\n\n" }); #add another table +that is two lines below the first $Word->Selection->MoveDown({Count => 2}); # add another table that contains the data # collapse the range so the starting and ending points are equal $MyRange->Collapse({Direction => wdCollapseEnd}); $Word->ActiveDocument->Tables->Add({ Range => $MyRange, NumRows => 1, NumColumns => 4, }); $Word->Selection->TypeText ({ Text => "end" }); print Win32::OLE->LastError(); my $Cell = $Word->ActiveDocument->Tables(2)->Cell(1,2)->Select(); $Word->Selection->TypeText ({ Text => "This is the new text"});

      Update: Clean up - I removed a few lines that were unnecessary, and changed the ::Warn line to fail on errors.

      C-.

      Thanks for that cacharbe,
      And the explanation too.

      When I use: use Win32::OLE::Const 'Microsoft Word';
      the script generates 15

      Constant subroutine emptyenum redefined at C:/Perl/site/lib/Win32/OLE/ +Const.pm line 65535.

      errors. I've seen this reported elsewhere and it doesn't seem to have an affect on the execution of the code.

      Cheers
      Richard