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

I seek knowledge! I am trying to insert a pagebreak in excel using Win32::OLE. I have tried many combination to no avail. The latest try is : $Excel->Worksheets(1)->HPageBreaks->Add({ Before => $Excel->Worksheets(1)->Range("A$row") }); Any suggestions would be appreciated? Thanks for your time.

Replies are listed 'Best First'.
Re: Inserting pagebreak in excel
by davidrw (Prior) on Nov 09, 2005 at 23:54 UTC
    Recording a macro for it yields this VB code:
    Rows("6:6").Select ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=ActiveCell
    wait .. your code actually worked for me (code borrowed mostly from Using Win32::OLE and Excel - Tips and Tricks).
    use strict; use warnings; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Excel'; use Win32::OLE::Variant; use Win32::OLE::NLS qw(:LOCALE :DATE); $Win32::OLE::Warn = 3; # Die on Errors. my $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit'); $Excel->{Visible} = 1; my $Book = $Excel->Workbooks->Add(); my $row = 6; $Excel->Worksheets(1)->HPageBreaks->Add({ Before => $Excel->Worksheets +(1)->Range("A$row") });
    Update: If i add a $Book->SaveAs('C:\Documents and Settings\David\temp\test.xls'); line in there, then open text.xls, then the page break is NOT there. Hmm.. just crappy Excel behavior??

    Update: Yup. !1 is right -- is really is there if you look hard enough ;) OP's and mine code should both work just fine ...

      It's there, just go to View > Page Break Preview then switch back to normal.