Hi,
I'm having problems accessing the worksheet after creating the worksheet in an IF statement. For some reason, if I create the worksheet in the else statement, my $rxsheet address is gone once the else statement is exited causing me to not be able to access that worksheet. I noticed that I had to create the worksheet outside the if/else statement for me to continue accessing the $rxsheet. Any help would be great. Thanks.
Error Message-Can't call method "Range" on an undefined value
I'm having problems accessing the worksheet after creating the worksheet in an IF statement. For some reason, if I create the worksheet in the else statement, my $rxsheet address is gone once the else statement is exited causing me to not be able to access that worksheet. I noticed that I had to create the worksheet outside the if/else statement for me to continue accessing the $rxsheet. Any help would be great. Thanks.
Error Message-Can't call method "Range" on an undefined value
Code:
open (fh, '<',"./$ARGV[0]") or die $!;
$row=1;
#Use Module Spreadsheet
use Win32::OLE qw (with);
use Win32::OLE::Const 'Microsoft Excel';
#Start Excel and make it visible
$xlApp = Win32::OLE->new('Excel.Application');
$xlApp->{Visible}=1;
$xlApp->{DisplayAlerts}=0;
#(WORKS)
my $workbook = $xlApp->Workbooks->Add();
my $rxsheet = $workbook->Worksheets(1);
$rxsheet->{Name} = "RxSheet";
#If file exist, delete plots. If file does not exist, create new worksheet and header
if (-e "Result.xlsx") {
#CODE
#CODE
}
else {
#(DOES NOT WORK)
#my $workbook = $xlApp->Workbooks->Add();
#my $rxsheet = $workbook->Worksheets(1);
#$rxsheet->{Name} = "RxSheet";
$rxsheet->Range('A1')->{'Value'} = "Configuration";
#print "Sheet Address: $rxsheet \n";
}
$rxsheet->Range('G1')->{'Value'} = "Sensitivity";
#Problem point - I can't write to cell G1
#ONLY WORKS if worksheet is selected outside IF/Else statement
#print "Sheet Address: $rxsheet \n";
Comment