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});
In reply to Scripting MSWord - headers and tabels by lostcause
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |