Perl
Home
Perl
  Regular Expressions
  Escape Sequences
  OLE - Outlook
  OLE - Excel
  OLE - Word
  Links
SQL
Services
Links
OLE - Word

use Win32::OLE;
use Win32::OLE qw(in with);
use Win32::OLE::Variant;
use Win32::OLE::Const 'Microsoft Word';

Word
$Word = Win32::OLE->GetActiveObject('Word.Application') ||
	   Win32::OLE->new('Word.Application');
$Word->{'Visible'}     = 0;
$Word->{DisplayAlerts} = 0;

# Open File
my $File = $Word->Documents->Open('C:\programs\tmp.txt');

# Replace Content of Document
$rng = $File->{Content};
$rng->{Text} = 'test';

# Change Page Margins
with (my $test = $File->PageSetup,
    TopMargin      => 40,
    BottomMargin => 40,
    LeftMargin      => 40,
    RightMargin    => 40);

# Print File
$File->PrintOut();
# or 
$Word->ActiveDocument->PrintOut({
  Background => 0,
  Range          => wdPrintAllDocument,
  Item             => wdPrintDocumentContent,
  PageType    => wdPrintAllPages})
sleep 5;   # sleep to let Word print before closing

# Save As
$Word->ActiveDocument->SaveAs({
             FileName   => $filename,
             FileFormat => wdFormatDocument});
# Quit/Close
$File->Close(); 
$Word->Quit();