Solved

Unprotect Excel  Worksbooks with Access VBA

Posted on 2016-07-14
6
35 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

943 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