Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Unprotect Excel  Worksbooks with Access VBA

Posted on 2016-07-14
6
Medium Priority
?
75 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 35

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 33

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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
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 2000 total points
ID: 41713081
Ok,

Try thisshieldsco.mdb
0

Featured Post

Industry Leaders: 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

Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
In a use case, a user needs to close an opened report by simply pressing the Escape (Esc) key. This can be done by adding macro code in Report_KeyPress or Report_KeyDown event.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

963 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