views:

871

answers:

4

Please tell me a solution to suppress passsword prompting of an excel file.

    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\\testfile.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;
+1  A: 

You may convert the following vb code to perl and give a try, Please note that this code is for vbproject, similary you can check for the worksheets, cells, or entire sheet, the same way.

' returns TRUE if the VB project in the active document is protected ' Please not

Function ProtectedVBProject(ByVal wb As Workbook) As Boolean

Dim VBC As Integer

  VBC = -1

  On Error Resume Next

  VBC = wb.VBProject.VBComponents.Count

  On Error GoTo 0

  If VBC = -1 Then

    ProtectedVBProject = True

  Else

    ProtectedVBProject = False

  End If

End Function

Example:

If ProtectedVBProject(ActiveWorkbook) Then Exit Sub

For Worksheet

If ActiveWorkbook.ProtectStructure=True  Then Exit Sub

For active work book windows

If ActiveWorkbook.ProtectWindows= True Then Exit sub

and so on..

Or You can open excel sheet with password

The Open method for the Workbook object, takes 12 arguments. To open a workbook with password protection, you would need to write the following code:

 Workbooks.Open "Book1.xls", , , ,"pswd"

You can also check with perl the same with empty arguments. I am not sure how to give...

lakshmanaraj
How to convert the code to Perl ?Is there any help document ?
Pankaj
May be $xlBook->Worksheets(1)->Protectstructure . And also I am doing the same in VB
lakshmanaraj
+4  A: 

If you know the passwords, you can supply them in the password and/or writerespassword arguments of the open command. Excel will not prompt for the passwords if they are supplied this way.

If you don't know the passwords but want to prevent the dialog box from appearing, you can supply dummy passwords in these parameters ("ThisIsNotAPassword", for instance). I have not found this in the documentation, but tested it in Excel 2003:

  • If the Excel file does not have passwords, it is opened.
  • If it does have passwords (other than those supplied), it will not ask the user for a password, but fail with an error you can detect.
Renze de Waal
So is the following correct ?my $xlBook = $xlApp->Workbooks->Open("D:\\Perl\\bin\\search tool\\test.xls","ThisIsNotAPassword");
Pankaj
Sorry, that won't work. The open method signature is Open(FileName, UpdateLinks, ReadOnly, Format, Password, WriteResPassword, /* others deleted */), you can find this in the help of Excel vba ide. You need to specify parameters by name or supply eveything up to and including writerespassword.
Renze de Waal
You mean like this :Open("D:\\Perl\\bin\\search tool\\test.xls",Password ="ThisIsNotAPassword");
Pankaj
I'm not sure whether Perl allows this. If so, I do mean that:-) You also need to supply a WiteResPassword, because Excel allows two different passwords, for reading and for editing.
Renze de Waal
The vba syntax would be open(Filename:=("D:\\Perl\\bin\\search tool\\test.xls",Password:="ThisIsNotAPassword",WriteResPassword:="ThisIsNotAPassword")
Renze de Waal
Can you write the correct code here ?
Pankaj
I don't know Perl, but am hoping thatmy $xlBook = $xlApp->Workbooks->Open("D:\\Perl\\bin\\search tool\\test.xls",0,0,1,"ThisIsNotAPassword","ThisIsNotAPassword"); will work. I have selected parameter values for UpdateLinks, ReadOnly and Format that I hope will not hurt.
Renze de Waal
No its not working.
Pankaj
Perl uses a hashref to pass named parameters. Try Open("D:\\Perl\\bin\\search tool\\test.xls", {Password=>"ThisIsNotAPassword",WriteResPassword=>"ThisIsNotAPassword"})
cjm
It doesn't work.
Pankaj
Could you indicate what goes wrong. Do you get an error, does Excel start but you still get a dialog box? What does happen?
Renze de Waal
If you dont use WriteResPassword then it works.The new problem is that the Excel files get opened in the background(invisible).We come to know about this when we open the Excel Software.I tried to destroy the object but still it opens in the background.
Pankaj
Destroying the object solves the problem.
Pankaj
+1  A: 

You may be better off using Spreadsheet::ParseExcel and Spreadsheet::WriteExcel than messing around starting up a copy of Excel and mucking around with OLE.

It'd be a lot more portable too.

David Precious
I have used these packages to parse but some excel files are showing Out Of Memory Error. So I used Win32::OLE to skip these type of files.
Pankaj
Fair enough, just wanted to ensure you were aware of those options.If you have time, it would be well worth contacting the authors and providing a test case which demonstrates this behaviour; if it's a bug in those modules, hopefully the authors will be interested in fixing it :)
David Precious
I had already reported that error.
Pankaj
A: 

Working off of lakshmanaraj's idea, and unknown's response:

use Win32::OLE;

sub is_protected_vb_project { 
    my $work_book = shift;
    eval { 
        my $count = $work_book->{VBProject}{VBComponents}{Count};
    };
    Carp::carp $@ if $@;
    return $@ ? 1 : 0;
}

my $work_book = Win32::OLE->GetObject( 'd:/some/path/somewhere.xls' );
printf "is_protected_vb_project( \$work_book )=%s\n"
     , is_protected_vb_project( $work_book )
     ;
Axeman