473,387 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Generic menu handling code

DFS
Anyone written any code to work with the CommandBar object?

I have a custom menubar, with 6 buttons, and up to 3 levels of nested menus,
such as:

File
File | Print
(that's 2 levels)
Projects
Projects | Add
Projects | Add | Type 1
Projects | Add | Type 2
(3 levels)
Reports
(1 level)
I want to iterate the CommandBar collection with a loop, down to the Nth
level, setting all the objects Enabled and Visible. Something like my code
below, but mine bombs when there's no nested menu, like on the Reports item
in my example:
Dim menuObject As Object, mObject As Object, mObject1 As Object

Set menuObject = CommandBars("MainMenu")

For Each mObject In menuObject.Controls
mObject.Visible = True
mObject.Enabled = True

For Each mObject1 In mObject.Controls
mObject1.Enabled = True
mObject1.Visible = True
Next mObject1

Next mObject
Thanks


Nov 12 '05 #1
9 1971
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I created this when I was investigating the CommandBars collection.

Sub enumCommandBars()
Dim cbr As CommandBar
For Each cbr In Application.CommandBars
Debug.Print ":"; cbr.Name; ":"
Debug.Print "Index: " & cbr.index
Debug.Print "NameLocal: " & cbr.NameLocal
Debug.Print "Type: " & cbr.Type
Dim ctl As CommandBarControl
For Each ctl In cbr.Controls
Debug.Print " "; ":"; ctl.Caption; ":"
Next ctl
If vbCancel = MsgBox(cbr.Name & " done", vbOKCancel) Then
Exit Sub
End If
Debug.Print
Next cbr

End Sub

If you run this in Acc2K, or greater, the MsgBox displays on the db
window side & you can't see the Debug window (PITA). You could
comment it out & the debug window will be filled w/ the results of the
run.

Sub menus are referred to like this:

Dim ctl As CommandBarControl
Set ctl = CommandBars("Menu Bar").Controls(strItem)
ctl.Controls(intIndex).Visible = True

strItem is the main menu item name (like "File" or "Edit").
intIndex is the .Index value of the submenu.
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQDWwh4echKqOuFEgEQKHRgCeNFJrS1K+NB9U/r4+X3J2HC//H5AAoJZ9
jXPUAWclOOstM/euBJZ/9YVC
=ewX/
-----END PGP SIGNATURE-----

DFS wrote:
Anyone written any code to work with the CommandBar object?

< SNIP >
I want to iterate the CommandBar collection with a loop, down to the Nth
level, setting all the objects Enabled and Visible. Something like my code
below, but mine bombs when there's no nested menu, like on the Reports item
in my example:


< SNIP >

Nov 12 '05 #2
DFS
Thanks MG, but I'm using Access97 and can't get that to work.

There's no CommandBar or CommandBarControl object.

"MGFoster" <me@privacy.com> wrote in message
news:Dh****************@newsread2.news.pas.earthli nk.net...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I created this when I was investigating the CommandBars collection.

Sub enumCommandBars()
Dim cbr As CommandBar
For Each cbr In Application.CommandBars
Debug.Print ":"; cbr.Name; ":"
Debug.Print "Index: " & cbr.index
Debug.Print "NameLocal: " & cbr.NameLocal
Debug.Print "Type: " & cbr.Type
Dim ctl As CommandBarControl
For Each ctl In cbr.Controls
Debug.Print " "; ":"; ctl.Caption; ":"
Next ctl
If vbCancel = MsgBox(cbr.Name & " done", vbOKCancel) Then
Exit Sub
End If
Debug.Print
Next cbr

End Sub

If you run this in Acc2K, or greater, the MsgBox displays on the db
window side & you can't see the Debug window (PITA). You could
comment it out & the debug window will be filled w/ the results of the
run.

Sub menus are referred to like this:

Dim ctl As CommandBarControl
Set ctl = CommandBars("Menu Bar").Controls(strItem)
ctl.Controls(intIndex).Visible = True

strItem is the main menu item name (like "File" or "Edit").
intIndex is the .Index value of the submenu.
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQDWwh4echKqOuFEgEQKHRgCeNFJrS1K+NB9U/r4+X3J2HC//H5AAoJZ9
jXPUAWclOOstM/euBJZ/9YVC
=ewX/
-----END PGP SIGNATURE-----

DFS wrote:
Anyone written any code to work with the CommandBar object?


< SNIP >
I want to iterate the CommandBar collection with a loop, down to the Nth
level, setting all the objects Enabled and Visible. Something like my code below, but mine bombs when there's no nested menu, like on the Reports item in my example:


< SNIP >

Nov 12 '05 #3
There is a discussion on command bars in the A97 Developers Handbook if you have
that available.

DFS wrote:
Anyone written any code to work with the CommandBar object?

I have a custom menubar, with 6 buttons, and up to 3 levels of nested menus,
such as:

File
File | Print
(that's 2 levels)

Projects
Projects | Add
Projects | Add | Type 1
Projects | Add | Type 2
(3 levels)

Reports
(1 level)

I want to iterate the CommandBar collection with a loop, down to the Nth
level, setting all the objects Enabled and Visible. Something like my code
below, but mine bombs when there's no nested menu, like on the Reports item
in my example:

Dim menuObject As Object, mObject As Object, mObject1 As Object

Set menuObject = CommandBars("MainMenu")

For Each mObject In menuObject.Controls
mObject.Visible = True
mObject.Enabled = True

For Each mObject1 In mObject.Controls
mObject1.Enabled = True
mObject1.Visible = True
Next mObject1

Next mObject

Thanks


Nov 12 '05 #4
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I developed that code in Acc97. Make sure the Reference (VBA window
menu Tool > References) "The Microsoft Office # Object library" is
checked. # = version of Office. I believe Office 97 version is 8.0.
The Office library has the CommandBar controls.

MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQDkeEYechKqOuFEgEQLJIQCfe9kRQ3lW8DEfILzhALQu27 UNcY8An3fD
bJ6BMWerxBqRVmK87aKtcYHu
=8F4M
-----END PGP SIGNATURE-----
DFS wrote:
Thanks MG, but I'm using Access97 and can't get that to work.

There's no CommandBar or CommandBarControl object.


Nov 12 '05 #5
DFS
"MGFoster" <me@privacy.com> wrote in message
news:e6*****************@newsread2.news.pas.earthl ink.net...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I developed that code in Acc97. Make sure the Reference (VBA window
menu Tool > References) "The Microsoft Office # Object library" is
checked. # = version of Office. I believe Office 97 version is 8.0.
The Office library has the CommandBar controls.

I'm using Access97. Microsoft Access 8.0 Object Library is referenced.

The lines "Dim cbr As CommandBar" and "Dim ctl As CommandBarControl" each
throw "User-defined type not defined". But if I change them to "Dim cbr as
Variant" and "Dim ctl As Variant" it works.

But it still doesn't iterate through all the nested submenus of my custom
menu, and give me their properties.
Thanks

MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQDkeEYechKqOuFEgEQLJIQCfe9kRQ3lW8DEfILzhALQu27 UNcY8An3fD
bJ6BMWerxBqRVmK87aKtcYHu
=8F4M
-----END PGP SIGNATURE-----
DFS wrote:
Thanks MG, but I'm using Access97 and can't get that to work.

There's no CommandBar or CommandBarControl object.

Nov 12 '05 #6
On Mon, 23 Feb 2004 01:38:48 -0500, "DFS" <no****@nospam.com> wrote:
"MGFoster" <me@privacy.com> wrote in message
Make sure the Reference...."The Microsoft Office # Object library" is
checked.>


I'm using Access97. Microsoft Access 8.0 Object Library is referenced.


D:

MGF said *Office* Library. You're saying *Access* library.

I played with it on my A97 machine, and checking the Office library
solved the object declaration problem. Prior to checking it, I had
the same behavior you're seeing.

-Matt

Nov 12 '05 #7
DFS

"Matthew Sullivan" <Ma**@NoSpam.com> wrote in message
news:1o********************************@4ax.com...
On Mon, 23 Feb 2004 01:38:48 -0500, "DFS" <no****@nospam.com> wrote:
"MGFoster" <me@privacy.com> wrote in message
Make sure the Reference...."The Microsoft Office # Object library" is
checked.>
I'm using Access97. Microsoft Access 8.0 Object Library is referenced.


D:

MGF said *Office* Library. You're saying *Access* library.


Yes, I saw that - I figured he was just mistaken. There's no Office 8.0
Library available in my list of references. I have an Access 8.0 library,
an Access 9.0 library, and an Office 10.0 library, but no Office 8.0
library.

I played with it on my A97 machine, and checking the Office library
solved the object declaration problem. Prior to checking it, I had
the same behavior you're seeing.


Checking any two of the above libraries causes a variety of other errors.

Leaving my Access 8.0 lib reference in place, if I dim the variables as
Variants (rather than CommandBars) his code example runs fine; it just
doesn't do what I need.

Thanks

Nov 12 '05 #8
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

The code I posted was just a demonstration. You can add new
CommandBar properties to the loop - inside the loop type period (.)
and the IntelliSense will pop-up a list of CommandBar properties.
This will only work if you declare the variable as a CommandBar
object. Usually main menus have an index of ZERO and their submenus
have a higher index number.

It seems you have Office XP (10.0) library installed on your computer.
To get the Office 97 (8.0) library it may be necessary to install
something from the Office 97 (Access 97) CD. When the installation
program runs select the Custom installation & look for something like
Addins or Libraries (DLL files).

Then again, you may already have the Office 97 library in the
Office/Access folder (C:\Program Files\Microsoft Office\Office). Look
for the file mso97.dll.

MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQDrpq4echKqOuFEgEQIT1ACgtZLgKsRULj2hFsH2PwuIuL VdxDUAoL0f
VbqyD8HAm0v3LVhJVZ3UVy+X
=SRuo
-----END PGP SIGNATURE-----
DFS wrote:

< SNIP >

Leaving my Access 8.0 lib reference in place, if I dim the variables as
Variants (rather than CommandBars) his code example runs fine; it just
doesn't do what I need.


Nov 12 '05 #9
D:

OK, then the simple "brute-force" method is just to add
error-handling to get around where your original code was frying.

The function below seems to get past the "no sub-menus" problem. In
real-life I'd probably write something recursive, but this was pretty
easy to spank out just to see how things behaved.

If you want to have some fun, set bEnable to "False". ;-)

This help at all?

-Matt

==================================
Public Function PlayCommandBars()

On Error GoTo ErrorHandler

'declare variables
Dim bEnable As Boolean
Dim theCmdBar As Object
Dim TheCmdBarCtrl As Object
Dim TheCmdBarCtrlCtrl As Object
'Dim theCmdBar As CommandBar
'Dim TheCmdBarCtrl As CommandBarControl
'Dim TheCmdBarCtrlCtrl As CommandBarControl

'initialize variables
bEnable = True
'bEnable = False

For Each theCmdBar In Application.CommandBars
For Each TheCmdBarCtrl In theCmdBar.Controls

TheCmdBarCtrl.Enabled = bEnable

On Error Resume Next
For Each TheCmdBarCtrlCtrl In TheCmdBarCtrl.Controls
TheCmdBarCtrlCtrl.Enabled = bEnable
Next TheCmdBarCtrlCtrl
On Error GoTo ErrorHandler

Next TheCmdBarCtrl
Next theCmdBar


Done:
On Error Resume Next

'free resources
Set TheCmdBarCtrlCtrl = Nothing
Set TheCmdBarCtrl = Nothing
Set theCmdBar = Nothing
Exit Function

ErrorHandler:
MsgBox Err.Number & vbNewLine & Err.Description
Resume Done
End Function

On Tue, 24 Feb 2004 00:39:12 -0500, "DFS" <no****@nospam.com> wrote:
Leaving my Access 8.0 lib reference in place, if I dim the variables as
Variants (rather than CommandBars) his code example runs fine; it just
doesn't do what I need.

Thanks


Nov 12 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: gong | last post by:
hi i recently looked at alexandrescu's book on c++, and i found it pretty much unintelligible. i have a few points which i wonder about. 1. as a developer, it is important, from a bottom line...
10
by: tmaster | last post by:
When I try to dynamically add a second sub menu item to this ContextMenu item, I get an error 'Specified argument was out of the range of valid values'. Private Sub mnuTopics_Show_Select(ByVal...
3
by: dhussong | last post by:
I'm trying to implement a generic exception handling routine that will write information to a text file at the time the exception occurred. I am using the Microsoft Application Block for Exception...
2
by: Mac via DotNetMonster.com | last post by:
Hi all, I have a menu that is populated from the results of an SQL query. For example lets say the menu contains a list of clients IDs. I would like to click on a particular item and open up a...
2
by: Ludwig | last post by:
My application has a main tool strip menu (File, Options, help,...). There are also a number of context menu's that popup. Some main menu items should also be available in some context menu's. ...
4
by: MikeB | last post by:
Hello All, I am trying to cache a menu that I dynamically build out and I do not want to have to build it out each time the page loads. Here is how I am doing it however, it doesnt work. Can...
1
by: Kayvine | last post by:
Hi guys, this is a question I have for an assignment, it is pretty long, but I am not asking for the code(well if someone wants to write I'll be really happy, lol), but I just want to know how to...
11
by: Scott Stark | last post by:
Hello, The code below represents a singly-linked list that accepts any type of object. You can see I'm represting the Data variable a System.Object. How would I update this code to use...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.