Solved

Unprotect Excel  Worksbooks with Access VBA

Posted on 2016-07-14
6
29 Views
Last Modified: 2016-07-15
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
    wb.Save
    wb.Close
    strFile = Dir()
Loop

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


'xls.Quit

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

tryagain:
'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
            Else
                vrtSelectedItem = .SelectedItems(1)
            End If
        Else
            Exit Function
        End If
    Else
        Exit Function
    End If

End With

BrowseFolder = CStr(vrtSelectedItem)
End Function
0
Comment
Question by:shieldsco
6 Comments
 
LVL 33

Expert Comment

by:Norie
ID: 41711613
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

0
 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41711679
Other than what Norie suggested, change the line .Protect Password:="123" to .UnProtect Password:="123"
0
 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 41712613
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.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 26

Expert Comment

by:Nick67
ID: 41713031
@fanpages.
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.

Nick67
0
 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 41713052
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).
0
 
LVL 26

Accepted Solution

by:
Nick67 earned 500 total points
ID: 41713081
Ok,

Try thisshieldsco.mdb
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now