?
Solved

Unprotect excel files in a folder

Posted on 2011-05-03
11
Medium Priority
?
375 Views
Last Modified: 2012-05-11
I have a list of excel files(2010) in a folder that are protected. Instead of going to each file and unprotect it., Im looking for code to read the folder and unprotect each file. excel vba or vb.net is fine
0
Comment
Question by:zachvaldez
  • 5
  • 3
  • 3
11 Comments
 
LVL 33

Expert Comment

by:jppinto
ID: 35514389
Please try the attached code.

jppinto
Sub UnlockThemAll()
    Dim pw As String
    Dim ws As Worksheet
    pw = InputBox("Password please!")
    On Error GoTo ErrHandler
    For Each ws In ThisWorkbook.Worksheets
        ws.Unprotect pw
    Next
    MsgBox "All unlocked", vbOKOnly, "Yeah!"
    Exit Sub
ErrHandler:
    MsgBox "There was a password problem", vbCritical, "Unable to unprotect!"
End Sub


Sub subOpenFiles()
 
Dim strSearch As String
Dim strFile As String
Dim wb As Workbook
 
strSearch = "c:\My Documents\"  'Search string
 
strFile = Dir(strSearch & "*.xls")
 
Do While strFile <> ""    ' Start the loop.
   Set wb = Workbooks.Open(strSearch & strFile)     
    
'Run your Sub here
    UnlockThemAll()
    
    wb.Close True
    Set wb = Nothing
    strFile = Dir    ' Get next entry.
Loop
  
End Sub

Open in new window

0
 

Author Comment

by:zachvaldez
ID: 35690141
if you crate this in vb, a reference to teh Microsoft Excel Library may be needed and referred in the code?
0
 
LVL 33

Expert Comment

by:jppinto
ID: 35690624
This code is to be placed on an Excel macro, on Visual Basic Editor from Excel. Isn't this what you wanted?
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.

 

Author Comment

by:zachvaldez
ID: 35698927
i use vb.net. So I modified it a bit but  am missing a reference on the workbook here.

Dim pw As String
        Dim xlsWB As Excel._Workbook
        Dim ws As Excel.Worksheet
        pw = InputBox("Password please!")
        On Error GoTo ErrHandler
        For Each ws In xlsWB.Worksheets>>> this need to be fix??
            ws.Unprotect(pw)
        Next
        MsgBox("All unlocked", vbOKOnly, "Yeah!")
        Exit Sub
ErrHandler:
        MsgBox("There was a password problem", vbCritical, "Unable to unprotect!")
    End Sub
0
 
LVL 33

Expert Comment

by:jppinto
ID: 35699751
Can't help you with VB .Net.... sorry, it's not my area. Making it from Excel, would work like I showed you.
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 1000 total points
ID: 35699833
Try the below code. You would need to add reference to Excel Interop Assembly and add the

Imports Microsoft.Office.Interop.Excel

at the top


Sub UnlockThemAll(wb As WorkBook)
Try
    Dim pw As String
    Dim ws As Worksheet
    pw = InputBox("Password please!")
    For Each ws In wb.Worksheets
        ws.Unprotect pw
    Next
    MsgBox "All unlocked", vbOKOnly, "Yeah!"
Catch Ex As Exception
    MsgBox "There was a password problem", vbCritical, "Unable to unprotect!"
End Try
End Sub


Sub subOpenFiles()
 
Dim strSearch As String
Dim wb As Workbook
Dim app As New Excel.Application
 
strSearch = "c:\My Documents\"  'Search string
 
Dim strFiles As String() = IO.Directory.GetFiles(strSearch, "*.xls")
 
For Each File In strFiles
   wb = app.Workbooks.Open(File)     
   
   'Run your Sub here
   UnlockThemAll(wb)
    wb.Close True
    Set wb = Nothing
Next 
  
End Sub

Open in new window

0
 

Author Comment

by:zachvaldez
ID: 35700541


i tried the vb.net solution.
It's not looping to the list of excel files.
It only sees the first file in the folder. and ends with the message
Actually, it should only unprotect the workbook (sheet1-first sheet) not all sheets
 .even though there are 3 sheets in a file,

thanks
0
 

Author Comment

by:zachvaldez
ID: 35700643
a couple of things.
It should also pick ".xlsx" files
and save it after removing the protection...
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 35701929
You would have to reference and use different versions of Office Primary Interop Assemblies to be able to handle both xls and xlsx files.

>It only sees the first file in the folder. and ends with the message
Does it unprotect that file?
0
 

Author Comment

by:zachvaldez
ID: 35722817
Codecruser-It's working
at the same time Id like to unhide column 'A'
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 35727205
Try

ws.Range("A1").EntireColumn.Hidden = False
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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

749 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