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

I'm running in to some problems in making a OLE application do some stuff in excel (181874)

I was getting unblessed reference errors, though I think I *may* be past that now. Now it says 'Unable to set LineStyle property of the Border class', then it says ERROR exception occurred (hex reference) in PROPERTYPUT "LineSTyle".

Also, the general page itself is not being laid out as I want (the names aren't there--for the worksheets, which themselves aren't all there). Here's the code stripped down.

#!/usr/bin/perl -w use strict; use Spreadsheet::WriteExcel::Utility; use Win32::OLE qw(in valof with); use Win32::OLE::Const 'Microsoft Excel'; use Win32::OLE::NLS qw(:DEFAULT :LANG :SUBLANG); $Win32::OLE::Warn = 3; my $file = 'test.txt'; open (TABFILE,$file) or die "$file: $!"; # Create a new Excel workbook my $file_name = 'test.xls'; my $xl = Win32::OLE->new('Excel.Application', sub {$_[0]->Quit;}) or die "error message"; my $book = $xl->Workbooks->Add(); my $sheet1 = $xl->Worksheets->Add(); my $sheet2 = $xl->Worksheets->Add(); $sheet1->{Name} = 'all_avg'; $sheet2->{Name} = 'grp_avg'; #$sheet2->Activate(); $book->SaveAs($file_name); my @edges = qw (xlEdgeBottom xlEdgeLeft xlEdgeRight xlEdgeTop xlInsideHorizontal xlInsideVertical); my @ranges = qw (B1 A2:G2 B16 A17:E17 B31 A32:E32 B46 A47:E47); for my $range (@ranges) { for my $edge (@edges) { with (my $Borders = $sheet2->Range($range)->Borders(eval($edge)), LineStyle =>xlContinuous, Weight => xlThin , ColorIndex => 1); } } for my $edge (@edges) { with (my $Borders = $sheet1->Range('A1:G1')->Borders(eval($edge)), LineStyle =>xlContinuous, Weight => xlThin , ColorIndex => 1); } $book->SaveAs($file_name); for (<TABFILE>) { $sheet1->Range(x1_rowcol_to_cell($row,$col))->{Value} = $_; } $book->SaveAs($file_name); $sheet1->Range('A1:G1')->{Value} = @$headers1; for(2,17,32,47) { $sheet2->Range(x1_rowcol_to_cell($_,0))->{Value} = @cats } for(0,15,30,45) { $sheet2->Range(x1_rowcol_to_cell($_,1))->{Value} = @$headers3; } for(1,16,31,46) { $sheet2->Range(x1_rowcol_to_cell($_,0))->{Value} = @$headers2; } $sheet2->Range(x1_rowcol_to_cell(1,5))->{Value} = ('Rest EMG', 'IMC EMG'); ## inside several for loops $sheet2->Range($cells->[$gr][$avg][$cond])->{Value} = $form; $book->SaveAs($file_name);

Replies are listed 'Best First'.
Re: Win32::OLE Woes
by jsprat (Curate) on Jul 16, 2002 at 15:01 UTC
    The reason the error happens is Excel won't set the border for xlInsideHorizontal or xlInsideVertical on a single cell selection. If there are no inside edges to set, Excel gives you an error, and not a very helpful one. You'll need to check which ranges span multiple columns or rows, then only set those properties as needed.

    If you want to see this for yourself, put $xl->{Visible} = True; right after you create $xl, then take B1 out of @ranges. You'll see that it works until the range doesn't support one of xlInsideHorizontal or xlInsideVertical.

      Once I figured out what a macro was (did I say I was new to excel? :) I got the problem straihgtened out.. Thanks. Also, as I said in the chatterbox, the problem wasn't excel or perl or anything. IT WAS ME!

      I had a test.xls in my cwd. Excel, as I have been told (and found out :( has its own cwd, which it created test.xls in. Unbenownst to me, I looked at the old test.xls (which was blank because I had been using writeExcel, and well, long story).