Join 137,228 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,898 people online right now. Registration is fast and FREE... Join Now!
Just wondering if there was an easier way of accomplishing what this site: MRU Menu, is providing? To be able to have a open recent section within my program, much like that of Adobe Illustrator and Photoshop and the likes. Any suggestions would be great. Thanks.
Can someone point me in the right direction with this functionality? I found this code, online, but it's confusing and is not accomplishing what I want to achieve, which is to store a list of the most recently opened files.
CODE
Option Explicit
Private Const MaxMRU = 4 'Maximum number of MRUs in list (-1 for no limit) Private Const NotFound = -1 'Indicates a duplicate entry was not found Private Const NoMRUs = -1 'Indicates no MRUs are currently defined
Private MRUCount As Long 'Maintains a count of MRUs defined
Private Sub Form_Load() ' Initialize the count of MRUs MRUCount = NoMRUs
' Call sub to retrieve the MRU filenames GetMRUFileList End Sub
Private Sub Form_Unload(Cancel As Integer) ' Call sub to save the MRU filenames SaveMRUFileList End Sub
Private Sub mnuMRU_Click(Index As Integer) ' Call sub to reorder the MRU list ReorderMRUList mnuMRU(Index).Caption, CLng(Index) End Sub
Private Sub mnuOpen_Click() ' Show the file open common dialog Me.CommonDialog1.ShowOpen
' Call sub to add this file as an MRU AddMRUItem Me.CommonDialog1.FileName End Sub
Private Sub AddMRUItem(NewItem As String) Dim result As Long
' Call sub to check for duplicates result = CheckForDuplicateMRU(NewItem)
' Handle case if duplicate found If result <> NotFound Then ' Call sub to reorder MRU list ReorderMRUList NewItem, result Else ' Call sub to add new item to MRU menu AddMenuElement NewItem End If End Sub
Private Function CheckForDuplicateMRU(ByVal NewItem As String) As Long Dim i As Long
' Uppercase newitem for string comparisons NewItem = UCase$(NewItem)
' Check all existing MRUs for duplicate For i = 0 To MRUCount If UCase$(Me.mnuMRU(i).Caption) = NewItem Then ' Duplicate found, return the location of the duplicate CheckForDuplicateMRU = i
' Stop searching Exit Function End If Next i
' No duplicate found, so return -1 CheckForDuplicateMRU = -1 End Function
Private Sub mnuQuit_Click() ' Close the program Unload Me End Sub
Private Sub AddMenuElement(NewItem As String) Dim i As Long
' Check that we will not exceed maximum MRUs If (MRUCount < (MaxMRU - 1)) Or (MaxMRU = -1) Then 'Increment the menu count MRUCount = MRUCount + 1
' Check if this is the first item If MRUCount <> 0 Then ' Add a new element to the menu Load mnuMRU(MRUCount) End If
' Make new element visible mnuMRU(MRUCount).Visible = True End If
' Shift items to maintain most recent to least recent For i = (MRUCount) To 1 Step -1 ' Set the captions mnuMRU(i).Caption = mnuMRU(i - 1).Caption Next i
' Set caption for new item mnuMRU(0).Caption = NewItem End Sub
Private Sub ReorderMRUList(DuplicateMRU As String, DuplicateLocation As Long) Dim i As Long
' Move entries previously "more recent" than the ' duplicate down one in the MRU list For i = DuplicateLocation To 1 Step -1 mnuMRU(i).Caption = mnuMRU(i - 1).Caption Next i
' Set caption of newitem mnuMRU(0).Caption = DuplicateMRU End Sub
Private Sub GetMRUFileList() Dim i As Long 'Loop control variable Dim result As String 'Name of MRU from registry
' Loop through all entries Do ' Retrieve entry from registry result = GetSetting(App.Title, "MRUFiles", Trim$(CStr(i)), "")
' Check if a value was returned If result <> "" Then ' Call sub to additem to MRU list AddMRUItem result End If
' Increment counter i = i + 1 Loop Until (result = "") End Sub
Private Sub SaveMRUFileList() Dim i As Long ' Loop control variable
' Loop through all MRU For i = 0 To MRUCount ' Write MRU to registry with key as it's position in list SaveSetting App.Title, "MRUFiles", Trim$(CStr(i)), mnuMRU(i).Caption Next i End Sub
Actually, this wont work because it assumes that you have stored the MRU File paths using the SaveSetting Function.
The SaveSetting Function stores data in HKEY_CURRENT_USER\Software\VB and VBA Program Settings\YourAppName
You can try searching through files in the C:\Documents and Settings\<user>\Recent directory.
Please forgive me for my lack of knowledge, I am learning and would like to learn. Could you explain to me the steps that I would take to accomplish this and to make sure it works as well? Thanks.
Relax, everyone on earth lacks knowledge in something or the other.
First I need to know what exactly you need. When you say MRU Files, are you concerned with only a certain set of files (for example .dat or .mp3 files) or just any files that were used by the user?
Which type of files does your application deal with? You can just store the name of each file (as soon as it is used by your application) into another file, and then Open it later and display these Filenames in your Menu Dynamically.
Post your needs and requirements as it will be easier for us to help you.
Once we get that, we can help you out with code too.
Relax, everyone on earth lacks knowledge in something or the other.
First I need to know what exactly you need. When you say MRU Files, are you concerned with only a certain set of files (for example .dat or .mp3 files) or just any files that were used by the user?
Which type of files does your application deal with? You can just store the name of each file (as soon as it is used by your application) into another file, and then Open it later and display these Filenames in your Menu Dynamically.
Post your needs and requirements as it will be easier for us to help you.
Once we get that, we can help you out with code too.
Well basically I am building a text editor (start-up program) that can read rtf's, txt's, and etc. Those are the type of files that I would like to have shown in the recently opened files. Not necessarily going with the MRU, if there is a better way then i am all ears. That was the only reference that I saw on the net about it. Thanks for helping me born2c0de.
Ok Great. What you need to do is to store the filename of every file that your program has used.
Like for example, as soon as the user clicks "Open", once the file is selected, save the filename either in the registry or in a separate file (say mru.txt)
Then open the registry key/file and check for filename entries. Add them dynamically to your "File" Menu.
Ok Great. What you need to do is to store the filename of every file that your program has used.
Like for example, as soon as the user clicks "Open", once the file is selected, save the filename either in the registry or in a separate file (say mru.txt)
Then open the registry key/file and check for filename entries. Add them dynamically to your "File" Menu.
OK, I understand that. But how do I create a separate file that is used to store this information and on the flipside, to pull the information from this file into the program? Thanks.
to create a separate file you need to create a text file to do this you need to create this code:
CODE
Open "c:\mru.txt" For Output As #1 Print #1, 'Path and filename of recently opened files Close #1
all you have to do now is to retrieve the content of the mru.txt then you now have a recent opened files...
Yes, but in your case you should use Append instead of Output like this:
CODE
open "c:\mru.txt" for append as #1
Using Append Mode will add data to a file. Using output erases the original data and puts in the new.
File Handling is tricky, I couldn't find any decent tutorials on it immediately, so since I'm already writing one on it, I'll post it in a few days or so.
If you have any problems writing the code, let us know.
Yes, but in your case you should use Append instead of Output like this:
CODE
open "c:\mru.txt" for append as #1
Using Append Mode will add data to a file. Using output erases the original data and puts in the new.
File Handling is tricky, I couldn't find any decent tutorials on it immediately, so since I'm already writing one on it, I'll post it in a few days or so.
If you have any problems writing the code, let us know.
Yeah, I think I might know how to do it, but as you saw in a previous question on this forum, I had asked about saving a file and overwriting it. So if you could explain further, that would be great. Thanks.