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?
 

Some videos you may like

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

kasan

New Member
Joined
Dec 30, 2016
Messages
14
Your code worked fine for me. Just in case, this code should be into ThisWorkbook, not in module.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,232
Messages
5,594,956
Members
413,954
Latest member
mrsandy

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
Top