Automatically or Manually Backup Multiple Versions of VbaProject.OTM

Status
Not open for further replies.

Hornblower409

Member
Outlook version
Outlook 2010 32 bit
Email Account
IMAP
After reading the Slipstick article How to backup and save your Outlook VBA macros (and after losing a day's work because of a stupid mistake) I wrote an expanded version of wolfgang's macro from the article.

You can use it to automatically backup the VbaProject.OTM file at Application_Startup, Application_Quit, or manually with a Ribbon button. It can keep as many previous versions of the file as you want, so you can go back and look at your old code (this has saved my ass more than once.)

The modifications required (directory paths, number of versions to keep, etc.) are pretty obvious. You will need a Reference to the "Microsoft Scripting Runtime" (scrrun.dll) in your VBE Tools -> References to resolve the Scripting. names.

Code:
'   Make a backup of the VbaProject.OTM file
'
'   Called by App Startup and Shutdown or from a Ribbon Button
'
Public Sub File_BackupVbaProject()
Const ThisProc = "File_BackupVbaProject"

    Const Source = "C:\Users\AGlassman\AppData\Roaming\Microsoft\Outlook\VbaProject.OTM"
    Const DestPath = "C:\Data\Backups\Outlook\VbaProject\"

    Dim FSO As Scripting.FileSystemObject
    Set FSO = CreateObject("Scripting.FileSystemObject")
    
    Dim Destination As String
    Destination = DestPath & Format(Now, "yyyy-mm-dd") & "_" & Format(Now, "hh-mm-ss") & "-" & Right(Format(Timer, "0.00"), 2) & "_VbaProject.OTM"
    
    FSO.CopyFile Source, Destination
    
    If Not Files_DeleteOlder(DestPath, 128) Then Exit Sub

End Sub

'   Delete older files in a folder based on DateCreated (NOT DateLastModified)
'
'   Called by File_BackupVbaProject
'
'   SPOS - Because VbaProject.OTM does not change it's DateLastModified or size on a reliable basis
'   we have to look for older files based on the DateCreated (when I made the copy).
'
'   See: https://stackoverflow.com/questions/24816147/outlook-vbaproject-otm-timestamp-is-not-updated-upon-changing
'
Public Function Files_DeleteOlder(ByVal FolderPath As String, ByVal NumberToKeep As Long) As Boolean
Const ThisProc = "Files_DeleteOlder"

    Dim FSO As Scripting.FileSystemObject
    Set FSO = CreateObject("Scripting.FileSystemObject")

    Files_DeleteOlder = False

    If Not FSO.FolderExists(FolderPath) Then
        MsgBox ("Folder '" & FolderPath & "' does not exist.")
        Exit Function
    End If
    
    Dim oFolder As Scripting.Folder
    Set oFolder = FSO.GetFolder(FolderPath)
    
    Dim oFiles As Scripting.Files
    Set oFiles = oFolder.Files
    
    Do While oFiles.Count > NumberToKeep
    
        '   Get the DT Created of the first file in the Collection
        '
        Dim oOldest As Scripting.File

            ' SPOS - You can't get a member of the Files collection by index number.
            '
            '   https://stackoverflow.com/questions/848851/asp-filesystemobject-collection-cannot-be-accessed-by-index
            '
            '   "In general, collections can be accessed via index numbering, but the Files Collection is not a normal collection.
            '   It does have an item property, but it appears that the key that it uses is filename"
            '
            ' Set oOldest = oFiles.Item(1)  --> BOOM
            '
            ' So we do a For Each, get the first file, and then immediatley exit the For.
            '
            Dim oFile As Scripting.File
            For Each oFile In oFiles
                Set oOldest = oFile
                Exit For
            Next oFile
        
        '   Find the oldest file in the collection and Delete it
        '
        For Each oFile In oFiles
            If oFile.DateCreated < oOldest.DateCreated Then
                Set oOldest = oFile
            End If
        Next oFile
        FSO.DeleteFile oOldest.Path
    
    Loop
    
    Files_DeleteOlder = True

End Function
 
If you only want to look at an older version of the VbaProject.OTM file, use the Outlook "/altvba" command line switch and point it to a saved version. e.g.:

Code:
"C:\Program Files (x86)\Microsoft Office\Office14\OUTLOOK.EXE" /altvba C:\Data\Backups\Outlook\VbaProject\2023-07-02_01-41-56-07_VbaProject.OTM
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
T Rules to run manually and NOT automatically ? Using Outlook 0
A Opening a link from an email automatically Outlook VBA and Custom Forms 0
A Opening a link from a specific sender automatically Outlook VBA and Custom Forms 4
X Run macro automatically when a mail appears in the sent folder Using Outlook 5
J Calendar events created on iPhone automatically changing default alert from 'None' to 'Time of Event' Using Outlook.com accounts in Outlook 0
Z Automatically adjust Outlook Reading Pane from bottom to right depending on portrait or landscape window Using Outlook 1
G Automatically delete email when a condition is met Outlook VBA and Custom Forms 1
B Outlook 2019 Automatically move email after assigning category Using Outlook 4
G automatically choosing "add to autocorrect" option Using Outlook 0
L Why are some email automatically going to "archive" Using Outlook 2
Z Outlook 365 Automatically assign categories to incoming mail in a shared folder Round Robin Outlook VBA and Custom Forms 1
G Automatically delete messages in the synchronization folder Outlook VBA and Custom Forms 3
C Automatically Insert Recipient Name from To Field Outlook VBA and Custom Forms 4
E Remove flag automatically Using Outlook 4
T Outlook 365 Move newly created tasks automatically on save. Outlook VBA and Custom Forms 1
M Outlook 365 Switching from AOL to Yahoo automatically Using Outlook 5
P Print attachments automatically and move the mail to an existing folder called "Ted" Outlook VBA and Custom Forms 4
B Zoom automatically next email item (VBA) Outlook VBA and Custom Forms 2
Paul Hobbs Automatically accept "Empty Folders" prompt Outlook VBA and Custom Forms 6
diver864 vba for a rule to automatically accept meeting requests with 'vacation' in subject, change to all-day event, change to free, don't send reply Outlook VBA and Custom Forms 1
D Custom Search Folders not refreshing/updating automatically Using Outlook 0
M Automatically add senders first name to a greeting Outlook VBA and Custom Forms 1
C Add Form to Appointments Received, Automatically Outlook VBA and Custom Forms 6
J Automatically forward email and apply template Outlook VBA and Custom Forms 0
Y Outlook 2013 Stop Outlook from automatically assigning categories to Tasks Using Outlook 0
O Forward a email with modified body Automatically. Outlook VBA and Custom Forms 0
A How to open a specific link automatically with outlook 2016 Outlook VBA and Custom Forms 6
P Automatically Categorize Meetings once they are accepted Outlook VBA and Custom Forms 5
W Automatically open attachments without automatically printing them Using Outlook 0
N How to set automatically the default or user defined Quickstyle Templates by Answer in Outlook Using Outlook 1
O Run macro automatically at sending an email Using Outlook 11
D Outlook 2016 automatically increment anniversaries Using Outlook 1
T Office 2013 no longer updating automatically Using Outlook 2
B Automatically Forward Emails and Remove/Replace All or Part of Body Outlook VBA and Custom Forms 8
D Print attachments automatically and moves the mail to a new folder Outlook VBA and Custom Forms 9
A How to open a specific link automatically with outlook Outlook VBA and Custom Forms 13
L Automatically Insert Recipient Name from To Field Outlook VBA and Custom Forms 33
N how to sync automatically when outlook opens Using Outlook 10
A Sort emails into subfolders based on sender and deleting emails automatically Outlook VBA and Custom Forms 3
undercover_smother Automatically Forward All Sent Mail and Delete After Send Outlook VBA and Custom Forms 10
C Need VBA code to automatically save message outside outlook and add date Outlook VBA and Custom Forms 1
stephen li VBA Outlook send mail automatically by specified outlook mail box Outlook VBA and Custom Forms 1
R Make Enter Network Password Prompt Go Away Automatically Using Outlook 0
I Print Automatically Attachments Outlook VBA and Custom Forms 3
S Automatically selecting folders and deleting messages in Outlook VBA Outlook VBA and Custom Forms 7
M Outlook 2016 Rules Not Working Automatically Using Outlook 5
Diane Poremsky Automatically create a task when sending a message Using Outlook 0
D Is it possible to automatically send an email when it is moved to a folder? Exchange Server Administration 1
A Automatically send email based on drop-down field? Outlook VBA and Custom Forms 2
M Automatically create event in calendar when task is created Outlook VBA and Custom Forms 1

Similar threads

Back
Top