• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 89
  • Last Modified:

Unprotect Excel Worksbooks with Access VBA

I' trying to modify the Excel Protection code below to Unprotect multiple workbooks in a folder.

Private Sub NavigationButton18_Click()
Dim xls As Object
Dim wb As Object
Dim wks As Object

Dim strPathFile As String
Dim strFile As String
Dim strPath As String
Dim strTable As String
Dim strBrowseMsg As String
Dim x As Integer

  On Error GoTo Err_Handler
strBrowseMsg = "Select the folder that contains the Data Call EXCEL files:"
strPath = BrowseFolder(strBrowseMsg)

If Nz(strPath, "") = "" Then
    MsgBox "No folder was selected.", vbOK, "No Selection"
    Exit Sub
End If

strFile = Dir(strPath & "\*.xls*")
Do While Len(strFile) > 0
    strPathFile = strPath & "\" & strFile
DoCmd.Hourglass True
    Set xls = CreateObject("Excel.Application")
    xls.Visible = False
    'xls.UserControl = True
    Set wb = xls.Workbooks.Open(strPathFile)
    For x = 1 To wb.Worksheets.Count
        'MsgBox x & " " & wb.Worksheets(x).Name
        If wb.Worksheets(x).Name = "Data Call" Then
            Set wks = wb.Worksheets(x)
        End If
    Next x
    'Cells Not To Lock
    With wks
        With .Range("A:A,H:H,I:I,J:J")
            If .Locked = True Then
                .Locked = False
            End If
        End With
        .Protect Password:="123"
    End With
    strFile = Dir()

DoCmd.Hourglass False
MsgBox "done!"
Exit Sub
   MsgBox Err.Description, vbExclamation, Err.Number


End Sub

Function BrowseFolder(Prompt As String) As String

Dim theFile As String
Dim response As Integer

Dim fs As Object
Set fs = CreateObject("Scripting.FileSystemObject")

'first we have to select the desired drive
'Declare a variable as a FileDialog object.
Dim fd As Object

'Create a FileDialog object as a File Picker dialog box.
Set fd = Application.FileDialog(4)
'Declare a variable to contain the path
'of each selected item. Even though the path is a String,
'the variable must be a Variant because For Each...Next
'routines only work with Variants and Objects.
Dim vrtSelectedItem As Variant

'Use a With...End With block to reference the FileDialog object.
With fd
    .InitialView = 2 'msoFileDialogViewDetails
    .Title = Prompt
    '.InitialFileName = theFile
    .ButtonName = "Use this folder"
    .AllowMultiSelect = False

    'The user pressed the action button.
    If .Show = True Then
        If .SelectedItems.Count = 0 Then
            MsgBox "You didn't make a valid selection.  Try again!"
            GoTo tryagain
        End If
        response = MsgBox("Use this Folder?", vbYesNo + vbCritical, "Last Chance")
        If response = vbYes Then
            'check that only one file was picked
            If .SelectedItems.Count > 1 Then
                MsgBox "You may only select one item.  Try again!"
                Exit Function
                vrtSelectedItem = .SelectedItems(1)
            End If
            Exit Function
        End If
        Exit Function
    End If

End With

BrowseFolder = CStr(vrtSelectedItem)
End Function
1 Solution
NorieVBA ExpertCommented:
The code looks fine to me, though I would recommend you put this outside the loop unless you want to end up with multiple instances of Excel.
    Set xls = CreateObject("Excel.Application")
    xls.Visible = False
    'xls.UserControl = True

Open in new window

Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
Other than what Norie suggested, change the line .Protect Password:="123" to .UnProtect Password:="123"
[ fanpages ]IT Services ConsultantCommented:
Norie: Regarding creating the "Excel.Application" object multiple times...

It is as if the comments in the previous question just didn't exist:

[ https://www.experts-exchange.com/questions/28956817/Access-variable-not-defined-error.html#a41706586 ]

...I am confused why the "Excel.Application" object is (re)created within the loop that sequentially returns each file in the folder selected.

Given the lack of response to all but one of my queries, I didn't bother contributing further within that question.
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

That may have been my bad.
Although Excel automation can be a bit funny with regard to whether Visible = True and UserControl = true.

In the loop, the workbook gets closed at the end of each iteration.
If .Visible = true and .usercontrol= true, Excel survives this.
I don't know for certain that it does when those conditions are NOT true.
But I built a working sample with the CreateObject in that position -- so that's on me.

[ fanpages ]IT Services ConsultantCommented:
No need to apologise, Nick'.

The creation of the object was inside the loop at the beginning of the thread, so there may have been a valid reason for keeping it there (that neither of us were aware of).

Try thisshieldsco.mdb
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now