FAQ
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.

Search Discussions

  • Lesley Binks at Nov 9, 2009 at 4:53 pm

    On Mon, Nov 09, 2009 at 03:46:19PM -0000, Andy Maginnis wrote:
    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.

    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 at Nov 9, 2009 at 5:15 pm
    apologies, I've sent this to the wrong list.

    -----Original Message-----
    From: Lesley Binks
    Sent: 09 November 2009 16:53
    To: Andy Maginnis
    Cc: dbi-users@perl.org
    Subject: Re: Automating Excel & Word - Table of contents!
    On Mon, Nov 09, 2009 at 03:46:19PM -0000, Andy Maginnis wrote:
    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.

    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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupdbi-users @
categoriesperl
postedNov 9, '09 at 3:49p
activeNov 9, '09 at 5:15p
posts3
users2
websitedbi.perl.org

2 users in discussion

Andy Maginnis: 2 posts Lesley Binks: 1 post

People

Translate

site design / logo © 2022 Grokbase