vba to kill workbook at open

cwchan220

New Member
Joined
Jul 30, 2014
Messages
23
Hi,

I want to expire a workbook on a specified date if the user opens it. I was trying to use the Workbook_open event but I am running into different errors. This file is in a shared drive and it is password protected. Could those be the reason for the permission denied error? I also got the "runtime error 91: object variable or with block variable not set" with the below code I was testing with.

Code:
dt = Format(Now + 2, "mmddyy")

On Error GoTo ErrorHandler
Select Case dt
Case Is > "101614"

With ActiveWorkbook
If .Path <> "" Then
.Saved = True
.ChangeFileAccess xlReadOnly
Kill ActiveWorkbook.FullName
MsgBox "File expired"
End If
End With
Exit Sub

ErrorHandler:
MsgBox "Fail to delete file: " & ActiveWorkbook.FullName
End Select


End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Thanks Andrew.

I tried using one of the codes in the link and I get an error: "file not found".Only difference with the link is I put it in Workbook_Open instead of a regular module. Looks like it converts it to a Read Only file before running into the error. Any idea how to fix this?

Code:
[/COLOR]Private Sub Workbook_Open()

With ThisWorkbook
        Application.DisplayAlerts = False
        If .Path <> vbNullString Then
            .ChangeFileAccess xlReadOnly
            Kill .FullName
        End If
        ThisWorkbook.Close SaveChanges:=False
    End With


End Sub


[COLOR=#333333]
 
Upvote 0
It's protected view and it says the file came from the internet even though it's from the shared drive. I've played with the protected view setting but I don't think I want to disable the protected view for files originating from the internet. Maybe my assumption for the "file not found" error is incorrect. Is there any reason the code isn't running?
 
Upvote 0
It's protected view and it says the file came from the internet even though it's from the shared drive. I've played with the protected view setting but I don't think I want to disable the protected view for files originating from the internet. Maybe my assumption for the "file not found" error is incorrect. Is there any reason the code isn't running?

You don't need to change/disable the protected view settings, when you open your file, the protected view warning will alert you to "enable editing", click on "enable editing", until you do so, your codes won't run properly because editing is disabled. If you don't "enable editing", you can view the file but can NOT make changes to it.
 
Last edited:
Upvote 0
I want to run the kill code when the workbook opens so I need something to override enable editing alert if that is the reason I am getting the error.
 
Upvote 0
I want to run the kill code when the workbook opens so I need something to override enable editing alert if that is the reason I am getting the error.

You can not overide it. Make a copy of your file in a safe location, enable editing, then run your kill code to see if that's the problem.
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,834
Members
452,947
Latest member
Gerry_F

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