Echoes has asked for the wisdom of the Perl Monks concerning the following question:

Hello everybody, I'm using Win32::OLE and I'm working on Excel files. These files have protected VBA code. I don't know how to unprotect the files with perl. I know how to do it for sheets and workbook but not for the VBA code. I found this in VBA:
Sub UnprotectVBProject(WkBk As Workbook, ByVal Pwd As String) Dim vbProj As Object Set vbProj = WkBk.VBProject If vbProj.Protection <> 1 Then Exit Sub ' already unprotected Set Application.VBE.ActiveVBProject = vbProj SendKeys Pwd & "~~" Application.VBE.CommandBars(1).FindControl(ID:=2578, recursive:=True).Execute End Sub
So, I really need to translate it in perl. I tried a lot of things without success... Can anyone help me, please :( Thanks a lot, Regards.

Replies are listed 'Best First'.
Re: Unprotect source code from Excel files with perl
by marto (Cardinal) on Oct 13, 2008 at 11:16 UTC
    Since I have no idea what you have tried (actual Perl code) I can only suggest taking a look at How do I convert a VBA macro to Perl? which should be of interest. Failing that a super search or searching the web may be the way to go.

    Update: Out of curiosity, did you post this?

    Martin
      Hi, Yes, this is one of my post. They suggested me to post here :) I did a lot of search on the net before posting and it's always the same vba code. I know how to convert a VBA macro to Perl, and your link is very good. I did use it for a lot of things like unprotect my workbook, my sheets, retrieve values or write in the files. But I don't know how to convert this one. Any help will do :) Thanks, Regards.
        PS: The previous post was mine, I wasn't log in...
Re: Unprotect source code from Excel files with perl
by Anonymous Monk on Oct 13, 2008 at 21:26 UTC
      Hi, I finally found a solution to my problem. This is what I do:
      use Win32::OLE qw(in with); ... $Excel = Win32::OLE->new('Excel.Application', 'Quit'); $Excel->{Visible} = $vttrue; $oBook = $Excel->Workbooks->Open("wishyouwerehere.xls"); ... if ($oBook->VBProject->{Protection} == 1) { $Excel->VBE->CommandBars->FindControl({ID=>2578})->Execute; $Excel->SendKeys ($pass); $Excel->SendKeys ("{ENTER}"); $Excel->SendKeys ("{ESC}"); sleep(1); } ...
      What is very important here and make things work is sleep(1). It gives time to enter the password and unprotect the project which is the key for the rest of my program. Thanks for your help :) Regards.