?
Solved

Detecting password excel files

Posted on 2009-02-20
11
Medium Priority
?
457 Views
Last Modified: 2012-05-06
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
0
Comment
Question by:triharish
  • 6
  • 4
10 Comments
 
LVL 10

Expert Comment

by:Tobias
ID: 23699545
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

0
 
LVL 10

Expert Comment

by:Tobias
ID: 23699554
Or the files it's faster than copy and paste.


test.pl.txt
ExcelFileIsProtected.pm.txt
0
 

Author Comment

by:triharish
ID: 23708682
Dear MadShiva

Will the above code work if the MS Excel software is not present in the system ?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 10

Accepted Solution

by:
Tobias earned 2000 total points
ID: 23708885
Dear triharish,


No, you could check if Excel "start" with something like this :

my $xlApp = Win32::OLE->new('Excel.Application') || die "Cannot start Excel, maybe not installed";


Best Regards
0
 

Author Comment

by:triharish
ID: 23771825
Will the below work if excel is not there :


my $xlApp = Win32::OLE->GetActiveObject('Excel.Application');
0
 
LVL 10

Expert Comment

by:Tobias
ID: 23793333
Dear,

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

Best Regards
0
 

Author Comment

by:triharish
ID: 23907270
Dear

I meant whether the code will work if the excel software is not present.
0
 
LVL 10

Expert Comment

by:Tobias
ID: 23907307
Dear,

No it will not work.

Best Regards
0
 

Author Comment

by:triharish
ID: 23926767
Dear

Then what is the other way ?
0
 
LVL 10

Expert Comment

by:Tobias
ID: 23927118
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.

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question