Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Please can anyone explain why the following code executes with no errors although fails to print anything?
use strict; use Win32::OLE; use Win32::OLE::Const 'Microsoft Word'; my $Word = Win32::OLE->new('Word.Application', 'Quit'); $Word->Documents->Open("C:\\ems\\ems1.rtf"); $Word->ActiveDocument->PrintOut({ Range => wdPrintFromTo, From => 1, To => 2, });
If I simply use $Word->ActiveDocument->PrintOut then I get a full printout. My problem is I need the first 2 pages printed only.

Replies are listed 'Best First'.
Re: Printing specific pages of a word document
by holli (Abbot) on Mar 21, 2005 at 15:49 UTC
    the following works fine for me (Word 2002)
    use strict; use Win32::OLE; use Win32::OLE::Const 'Microsoft Word'; my $Word = Win32::OLE->new('Word.Application'); $Word->Documents->Open("C:\\temp\\m.rtf"); $Word->ActiveDocument->PrintOut ({ Range => wdPrintRangeOfPages, Item => wdPrintDocumentContent, Pages => "1-2", }); $Word->Quit(0);
    The VB-Code I got that from is
    Application.PrintOut FileName:="", Range:=wdPrintRangeOfPages, Ite +m:= _ wdPrintDocumentContent, Copies:=1, Pages:="1-2", PageType:= _ wdPrintAllPages, ManualDuplexPrint:=False, Collate:=True, Back +ground:= _ False, PrintToFile:=False, PrintZoomColumn:=0, PrintZoomRow:=0 +, _ PrintZoomPaperWidth:=0, PrintZoomPaperHeight:=0
    Generally it's a good idea to use the makro recorder and translate it's code.


    holli, /regexed monk/
Re: Printing specific pages of a word document
by traveler (Parson) on Mar 21, 2005 at 15:54 UTC
    I beat myself over the head with OLE-Word for several days before I discovered that some older versions of Word do not seem to work as documented... I upgraded to Word2002 and all was well.
      Thanks Holli that worked perfectly! I took your advice and used word to create a macro then converted to this
      $Word->ActiveDocument->PageSetup({ Orientation => wdOrientPortrait, TopMargin => 14, BottomMargin => 14, LeftMargin => 42, RightMargin => 28, FirstPageTray => 260, OtherPagesTray => 260, });
      but this does not work at all. I am using word 2003 and just wondering if you have managed to so this one before?
        Can you define "not work"?


        holli, /regexed monk/