Hello,
I would like some help if anyone can. I have researched, research and I just cant do it anymore - I have to ask. Before I do, I know this is probably a simple answer, and your fed up of explainng it but from my research (lots) the example code seems to be a simple situation, and I have tried to undertake what has been said...I always assign a value to a variable. As you will see below I have but I still get the error (below) I have spent hours trial and error and I havnt even 'randomly' come upon the correct answer!
Use of uninitialized value $VarComparison
acro_Files/NO_DEL/AccessExcel.pl line 84.
Use of uninitialized value $VarComparison
acro_Files/NO_DEL/AccessExcel.pl line 84.
Use of uninitialized value $VarComparison
acro_Files/NO_DEL/AccessExcel.pl line 84.
My modified code:
#!/usr/bin/perl -w
# Declare the subroutines
sub trim($);
sub ltrim($);
sub rtrim($);
# Right trim function to remove trailing whitespace
sub rtrim($)
{
my $string = shift;
$string =~ s/\s+$//;
return $string;
}
use v5.12.1;
use strict;
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';
$Win32::OLE::Warn = 3; # die on errors...
# get already active Excel application or open new
my $Excel = Win32::OLE->GetActiveObject('Excel.Application')
|| Win32::OLE->new('Excel.Application', 'Quit');
# open Excel file
my $Book = $Excel->Workbooks->Open("C:/CH-53_Electronics/Macro_Files/NO_DEL/WDMASTER.xls");
# You can dynamically obtain the number of worksheets, rows, and columns
# through the Excel OLE interface. Excel's Visual Basic Editor has more
# information on the Excel OLE interface. Here we just use the first
#20 worksheet, rows 1 through 4 and columns 1 through 3.
# select worksheet number 1 (you can also select a worksheet by name)
my $Sheet = $Book->Worksheets("GA");
unlink("C:/boo/boo1.txt");
my $record;
our $ansi;
my $VarFileName = 0;
my $VarISS = "VarISS_TestValue";
my $VarDWGDO = "VarDWGDO_TestValue";
#our $varComparison = 0;
############## READ FILE FROM ISODRAW ##################
open(INFILE, "<:encoding(UTF-16)", "C:/CH-53_Electronics/Macro_Files/Tmp_Iso_FileName.txt");
while(<INFILE>)
{
$record=$_;
print "$record \n";
$VarFileName = $record;
}
close(INFILE);
######## Trim the trailing whitespace ########
$VarFileName = rtrim($VarFileName);
#################################
# varDWGDO_Col is the column DWGDO, check that it is the 6th column in the excel.
my $VarDWGDO_Col = 6;
# VarISS_Col is the column ISS, check that it is the 8th column in the excel.
my $VarISS_Col = 8;
# VarICN_Col is the column ICN, check that it is the 16th column in the excel.
my $VarICN_Col = 16;
my $varFlag = 0;
foreach my $row (1..256)
{
foreach my $col ($VarICN_Col)
{
my $VarComparison = $Sheet->Cells($row,$VarICN_Col)->{'Value'};
if ($VarComparison eq $VarFileName)
{
$varFlag = 1;
$VarISS = $Sheet->Cells($row,$VarISS_Col)->{'Value'}."\n";
$VarDWGDO = $Sheet->Cells($row,$VarDWGDO_Col)->{'Value'}."\n";
open WriteFILEISS, ">C:/boo/boo_ISS.txt" or die $!;
print WriteFILEISS $VarISS;
close WriteFILEISS;
open WriteFILEDWGDO, ">C:/boo/boo_DWG.txt" or die $!;
print WriteFILEDWGDO $VarDWGDO;
close WriteFILEDWGDO;
last;
} else {
}
}
}
if ($varFlag == 0)
{
open WriteFILEFAILED, ">>C:/boo/boo.txt1" or die $!;
print WriteFILEFAILED $VarFileName."\n";
close WriteFILEFAILED;
open WriteFILEFAILEDERROR, ">C:/boo/boo.txt2" or die $!;
print WriteFILEFAILEDERROR 1;
close WriteFILEFAILEDERROR;
} else {
open WriteFILEFAILEDERROR, ">C:/boo/boo.txt3" or die $!;
print WriteFILEFAILEDERROR 0;
close WriteFILEFAILEDERROR;
}
# clean up after ourselves
$Book->Close;
I have tried declaring and assigning $VarComparison next to my $VarICN_Col = 16;
and making it global(our) ands it just doesnt want to work. I have added in everything to assist with the line # but I think the issue is around the for each loops everywhere else seems fine. I had this working originally but needed to change a few things around, the main things I changed was ($VarICN_Col) which used to be ($VarDWGDO_Col) - thats all. I havnt touched $varComparison.
Can someone please help me out here and explain why it doesnt work.
Many thanks
Alan