Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Automating Excel & Word - Table of contents!

0 views
Skip to first unread message

Andy Maginnis

unread,
Nov 9, 2009, 10:46:19 AM11/9/09
to dbi-...@perl.org
Apologies if this the wrong list for this question, if so could someone
point me in the right direction!

I am trying to generate an script to process multiple CSV's into a
single excel doc, create tables
and then create a shell report document template. I have managed all
this so far but cannot
seem to add a TOC. From what I can tell I have translated the VBA
command correctly.

Ideally I would like to add it at the start of the document, however I
also cannot move the
selector position & have been trying to just insert a TOC anywhere as a
first step.

$Word = Win32::OLE->GetActiveObject('Word.Application') ||
Win32::OLE->new('Word.Application');
my $document = $Word->Documents->Add;

foreach my $file (getFileListAsArray($basedir,"\.(?:gif|GIF)\$")){
print "Adding $file to document\n";
$name=$file;
$name=~s/$striphead//;
$name=~s/$striptail//;

$document->ActiveWindow->Selection -> {Style} = "Heading 1";
$document->ActiveWindow->Selection -> TypeText("$name\n");

$document->ActiveWindow->Selection -> {Style} = "Normal";
$document->ActiveWindow->Selection -> TypeText("Type Text here
1\n");

$Word->Selection->InlineShapes->AddPicture({
FileName => "$basedir\\$file",
LinkToFile => "False",
SaveWithDocument => "True"});

$document->ActiveWindow->Selection -> TypeText("Type Text here
2\n");
$Word->Selection->InsertBreak({Type=>wdSectionBreakNextPage});

}

$Range = $Word->Selection->{'Range'};
$Word->ActiveDocument->TablesOfContents->Add ({
Range=>$Range,
RightAlignPageNumbers=>"True",
UseHeadingStyles=>"True",
UpperHeadingLevel=>"1",
LowerHeadingLevel=>"4",
IncludePageNumbers=>"True",
AddedStyles=>"",
UseHyperlinks=>"True",
HidePageNumbersInWeb=>"True",
UseOutlineLevels=>"True"
});

$document ->SaveAs( $outfile );
$document ->Close;

Im using Word 2007

many thanks.

Lesley Binks

unread,
Nov 9, 2009, 11:52:58 AM11/9/09
to Andy Maginnis, dbi-...@perl.org


You would probably be better off discussing this topic on a more generalised perl list or forum.
This list is on using DBI - the independent database interface for Perl.

Andy Maginnis

unread,
Nov 9, 2009, 12:12:45 PM11/9/09
to Lesley Binks, dbi-...@perl.org
apologies, I've sent this to the wrong list.
0 new messages