Solved

Unprotect Excel  Worksbooks with Access VBA

Posted on 2016-07-14
6
42 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 29

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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

777 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