VB Quicktakes - Recent Files

This module can be used to save a recent files list to the registry and to retrieve the list into a menu. To save a file to the Recent Files registry entry use the SaveRecent Function passing it the file path. To Load the menu, call the LoadMenu function passing it a menu array and optionally a menu separator bar.

Option Explicit

Private Const MAX_FILES = 5    	'specifies max number of file entries

Public Sub SaveRecent(NewEntry As String) Dim fileName(MAX_FILES - 2) As String Dim i As Integer If DuplicateEntry(NewEntry) Then Exit Sub End If For i = 0 To (MAX_FILES - 2) fileName(i) = GetSetting(App.Title, "Recent Files", "File" & i + 1) Next SaveSetting App.Title, "Recent Files", "File1", NewEntry For i = 0 To (MAX_FILES - 2) SaveSetting App.Title, "Recent Files", "File" & i + 2, fileName(i) Next End Sub
Public Sub LoadMenu(Menu As Variant, Optional sepMenu As Menu) Dim i As Integer Dim fileName(MAX_FILES) As String On Error Resume Next For i = 1 To MAX_FILES Menu(i).Visible = False If Not IsNull(sepMenu) Then sepMenu.Visible = False End If Next For i = 1 To MAX_FILES fileName(i) = GetSetting(App.Title, "Recent Files", "File" & i) If Len(fileName(i)) > 0 Then Load Menu(i) Menu(i).Caption = fileName(i) sepMenu.Visible = True Menu(i).Visible = True End If Next End Sub
Private Function DuplicateEntry(EntryName As String) As Boolean Dim fileName(MAX_FILES - 1) As String Dim i As Integer For i = 0 To (MAX_FILES - 1) fileName(i) = GetSetting(App.Title, "Recent Files", "File" & i + 1) If fileName(i) = EntryName Then DuplicateEntry = True Exit Function End If Next End Function
About this page: