in reply to Scripting MSWord - headers and tabels

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

Replies are listed 'Best First'.
Re: Re: Scripting MSWord - headers and tabels
by cacharbe (Curate) on Oct 25, 2001 at 03:23 UTC
    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-.

Re: Re: Scripting MSWord - headers and tabels
by lostcause (Monk) on Oct 25, 2001 at 03:03 UTC

    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