I need to add them programmatically, i.e., when I query the DB to see which project files were last opened, I need to add them programmatically to the sub-menu.
There are 2 ways to do this. The first uses the AppendMenu or InsertMenu APIs (If you'd like an example of these, let me know and I will post them). The other method (and the simpler one) treat the menu items just like a control array.
Basically, to use the control array method, you need to create a "template menu item" at design time and make it a control array by making its index = 0. Also, this menu item should have its visible property set to false (this can be tricky since at least one sub menuitem MUST be visible . . . to overcome this, I would put a "label menu" that is just text header such as "Recent Projects). After that is completed, you simply have to load new menu items (with a unique index value) as need and then set its properties. For example . . .
for intRecentProjectIndex = 1 to 5
Load mnuRecentProject(intRecentProjectIndex)
with mnuRecentProject(intRecentProjectIndex)
.Caption = "Recent Project XYZ"
.Visible = True
End with
next intRecentProjectIndex
This is a very simple bit of code . . . if you'd like more detail, just ask, but this should be enough to get you started. Also, if you want more control, there are the APIs that I mentioned (and can demonstrate for you if needed), but they tend to raise other complications.
I got the sub menu to work, only I'm not sure how I handle events from the submenu items. Neither sub mnuRecentProjectsSubItem(0)_Click() nor sub mnuRecentProjectsSubItem_Click() seem to work.
Hi there, sarahmc22.
You might have already figured this one out, but here is what I did to obtain the results that I think you want.
I used Jmarler's example with the form and the code, but modified it to this:
'this project will put recent files in the menu
'recent.rec is the file where the last 5 opened files are
'stored. you can name it whatever you want.
'I am calling the form that creates the menus on startup,
'but you can probably do this routine anywhere, like after
'you open a file, to add it to the recent list.
Dim a(5) 'select maximum to display (also below)
Sub form_load()
Open "C:\recent.rec" For Input As #1
Do Until EOF(1)
If intrecentprojectindex = 5 Then Exit Do intrecentprojectindex = intrecentprojectindex + 1
Input #1, a(intrecentprojectindex)
Load recentproject(intrecentprojectindex)
With recentproject(intrecentprojectindex)
.Caption = a(intrecentprojectindex)
.Visible = True
End With
Loop
Close 1
End Sub
'this sub prints out which was clicked on.
Private Sub recentproject_click(index As Integer)
Print a(index)
End Sub
________________________________________________________
Instead of printing the file you select in the form, you can use it in the open command to open the file.
Hope this helps.
Yes AndyLee, I knew that, but I was having the same problem, and after searching for the answer I only found this thread. But since no one answered SarahMC22's last post, I thought I would throw in my solution, just in case someone else comes across the same problem.
Even late help is appreciated when it is really needed.
I sure hope the vast amount of information on this website doesnt ever become obsolete.
StrongM, what does Chortle mean?
Have a nice day, and Happy Programming.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.