Code in BeforeClose is executing but having no effect

JeffK627

Active Member
Joined
Jun 22, 2005
Messages
313
I have the following code in an Excel 2010 workbook:

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)

    Application.EnableEvents = True

    Dim FileName As String
    
    FileName = ActiveWorkbook.Name

    If FileName = "False" Then Exit Sub
    
    If FileName <> "Shipping Manifest SaveAS Update.xlsm" Then
        Application.DisplayAlerts = False
        ActiveWorkbook.SaveAs FileName:=FileName, FileFormat:=xlOpenXMLWorkbookMacroEnabled, WriteResPassword:="abc123", ReadOnlyRecommended:=True
        Application.DisplayAlerts = True
    End If
    
End Sub

The code executes - I checked by inserting a breakpoint and stepping through it. But the password protection to open the file as Read Only unless the password is entered is not being applied - the file just opens as normal. What am I doing wrong?
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Your code worked fine for me. Just in case, this code should be into ThisWorkbook, not in module.
 
Upvote 0

Forum statistics

Threads
1,214,812
Messages
6,121,696
Members
449,048
Latest member
81jamesacct

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top