Delete Workbook Before Close

Darranimo

Board Regular
Joined
Jan 19, 2022
Messages
52
Office Version
  1. 365
Platform
  1. Windows
So I created a workaround so that I can make updates to a workbook so that the end user is always using the latest version. I did this by having the "Master" workbook save a copy to the user's desktop upon opening (Name for the desktop copy of the workbook ends in "Desktop"). The user is then working with the version on their desktop instead of the "Master". What I am trying to do now, is write some code so that the desktop copy is deleted upon exiting the workbook. Here is the code that executes upon opening:
VBA Code:
Private Sub Workbook_Open()
    Dim Path As String
    
    Path = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\" & "Journal Entry Automator Desktop.xls"
    
    If Dir(Path) <> "" Then Kill (Path)
    ActiveWorkbook.SaveAs Path
End Sub

Here is my attempt at the code for when the workbook is closed:
VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    
    If ThisWorkbook.Name = "Journal Entry Automator Desktop.xlsm" Then
        ThisWorkbook.ChangeFileAccess xlReadOnly
        Kill ThisWorkbook.FullName
    End If
End Sub

If someone could help me solve this I would really appreciate it!

Bonus question: Is there a way to preface these codes with an if statement so that if the workbook is located in my files it disables the macros? lol. This code is difficult to work because it keeps executing when I am working with it. It's like working with a wet bar of soap. haha
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Perhaps a secondary workbook that contains the code to delete that Desktop copy. Have a code that opens this secondary workbook and closes the desktop copy, then deletes the desktop copy off your desktop? - 'that's a mouth full'
I could give it an attempt when I get home. I use a workbook that is all code that I have other workbooks reference to avoid repeating code entries. Opens in the background, executes the code, then closes.
 
Upvote 0

Forum statistics

Threads
1,215,034
Messages
6,122,782
Members
449,095
Latest member
m_smith_solihull

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