Accessing worksheet outside If/Else statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tastingoo
    New Member
    • Sep 2011
    • 3

    Accessing worksheet outside If/Else statement

    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
    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";
    Last edited by numberwhun; Sep 27 '11, 05:05 AM. Reason: You need to please use the necessary code tags around code you post in the forums.
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    More than likely it goes to the scope of the object. If it is defined inside the if/else, then its probably only available inside of there. Try pre-defining it outside of the if/else first, then doing what you need to in the if/else and see if its available afterwards.

    Comment

    • tastingoo
      New Member
      • Sep 2011
      • 3

      #3
      Sorry about that. I'm new to perl and programming. Thanks for correcting.

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        Not a problem, that is why we are here.

        Comment

        • tastingoo
          New Member
          • Sep 2011
          • 3

          #5
          Thanks. I tried that and it's fine inside the if/else statement. Just can't get to it once the code leaves if/else statement. I found a workaround I guess. Similar to what you said, I predefined a pointer variable outside if/else. Once I created worksheet in my if/else, I pointed that memory address location to my global pointer. I was able to edit the same worksheet in my remaining code using that global pointer. Basically, I noticed during debugging that the memory address was always getting erased once the code went outside if/else statement. Thanks for input.

          Comment

          Working...