Link to home
Start Free TrialLog in
Avatar of triharish
triharish

asked on

Detecting password excel files

Please tell me a solution to detect excel file with passwords.

The code only detects the excel files whose sheets are protected.
use Win32::OLE;
 
      my $xlApp = Win32::OLE->new('Excel.Application');
 
      $xlApp->{Visible} = 0;
 
      $xlApp->{DisplayAlerts} = 0;
 
      # Open excel file.
      my $xlBook = $xlApp->Workbooks->Open("C:\\Documents and Settings
+\\username\\Desktop\\test.xls");
      my $Sheet1 = $xlBook->Worksheets(1);
 
        my $row = 1;
        my $col = 1;
 
       $Sheet1->Cells($row,$col)->{'Value'} = 5;
 
        if (Win32::OLE->LastError) 
        {
                print "File protected";
        }
 
      $xlBook ->Close();
 
  undef $xlBook;

Open in new window

test.xls
Avatar of Tobias
Tobias
Flag of Switzerland image

Dear triharish,


I see that you want  this function, then ok now it's working :)

Best Regards
package ExcelFileIsProtected;
 
sub ExcelFileIsProtected
{
my ($path_to_file) = @_;       # One argument wait
use Win32::OLE;
#$Win32::OLE::Warn = 3; # die on errors...
#  
# Start Excel and make it invisible
      my $xlApp = Win32::OLE->new('Excel.Application');
      
      $xlApp->{DisplayAlerts} = 0;
	  
	  $xlApp->{Display} = 0;
       
	  $Password="";
	   
      # Open excel file.
      my $xlBook = $xlApp->Workbooks->Open($path_to_file, $UpdateLinks, $ReadOnly, $Format,$Password);   
	  
		my $var = Win32::OLE->LastError() ;
	  	
		  
	     if ($var == 0 ) 
        {
        return 0;
        }
		else
		{
			return 1;
			}
		
 #$xlBook ->Close();
 
  undef $xlBook;
}
 
 
 
1;
 
 
 
 
 
 
 
 
 
#!/usr/bin/perl
 
#use strict;
#use warnings;
 
 
#My Modules
use ExcelFileIsProtected;						
 
$path_to_file = "C:\\Documents and Settings\\username\\Desktop\\test.xls";
 
$var = ExcelFileIsProtected::ExcelFileIsProtected($path_to_file);
if($var==1)
{
print "File is protected\n";
}
else
	{
	#Do something
	print "File is not protected...\n";
	}

Open in new window

Or the files it's faster than copy and paste.


test.pl.txt
ExcelFileIsProtected.pm.txt
Avatar of triharish
triharish

ASKER

Dear MadShiva

Will the above code work if the MS Excel software is not present in the system ?
ASKER CERTIFIED SOLUTION
Avatar of Tobias
Tobias
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Will the below work if excel is not there :


my $xlApp = Win32::OLE->GetActiveObject('Excel.Application');
Dear,

It will return one error in Win32::OLE->LastError().

Best Regards
Dear

I meant whether the code will work if the excel software is not present.
Dear,

No it will not work.

Best Regards
Dear

Then what is the other way ?
Then what is the other way ?

For what ?

If you want use Win32::OLE; with excel you need to have excel...

Anyway I answered the question, and I will not more answer most any other questions on this issue, because this issue for me it is closed.