PDA

View Full Version : Cant remove a submenu control via the onaction procedure



RustyCocoPat
05-28-2009, 05:31 AM
Hi. I really hope that someone can help me with this.

I can create menu items and buttons on shortcut menus and can delete them from the main menu but I can't delete them from the procedure called via the OnAction method. (I want to manipulate the menu structure and do other tasks in the called procedure via the menu button's OnAction method.)

Here is the code:

Option Explicit
'---------------------------------------------------------------
'The MainMenYou procedure creates the MainMenu menu item on the
'Headings shortcut menu. Sub menus are added to this menu in the
'procedure called by the .OnAction method.
'---------------------------------------------------------------

Public Sub MainMenYou()

Dim cbpop As CommandBarControl
Dim cbsub2 As CommandBarControl
Dim cbsub3 As CommandBarControl
Dim cbctl2 As CommandBarControl

CustomizationContext = ActiveDocument.AttachedTemplate

Set cbpop = CommandBars("Headings").Controls.Add( _
Type:=msoControlPopup, Before:=1)
cbpop.Caption = "=> Main Menu"
cbpop.Tag = "MainMenu"
cbpop.OnAction = "SubMenYou"

End Sub

'-------------------------------------------------------------
'The SubMenYou procedure below adds or modifies menus to the
'MainMenu menu item created above.
'-------------------------------------------------------------

Sub SubMenYou()

CustomizationContext = ActiveDocument.AttachedTemplate

Dim cbpop As CommandBar
Dim oCustomMenuItem As CommandBarControl
Dim sMenuItemTag As String

sMenuItemTag = "MainMenu"
Set cbpop = Application.CommandBars("Headings")
Set oCustomMenuItem = cbpop.FindControl(Tag:=sMenuItemTag)

Set cbsub2 = oCustomMenuItem.Controls.Add(Type:=msoControlPopup)
cbsub2.Visible = True
cbsub2.Caption = "Submenu 1"
cbsub2.Tag = "Submenu1"

Set cbsub3 = cbsub2.Controls.Add(Type:=msoControlPopup)
cbsub3.Visible = True
cbsub3.Caption = "Submenu 2"
cbsub3.Tag = "Submenu2"

'--------------------------------------------------------------------------
'Here is the problem. It either can't find the menu control or doesn't
'allow deletion from the sub-procedure called by .OnAction
'The result is that it adds an extra menu item each time without deleting
'any.
'--------------------------------------------------------------------------

sMenuItemTag = "SubmenuButton"
Set oFileMenu = Application.CommandBars("Headings")
Set oCustomMenuItem = oFileMenu.FindControl(Tag:=sMenuItemTag)

If Not oCustomMenuItem Is Nothing Then
oCustomMenuItem.Delete
Else
Set cbctl2 = cbsub3.Controls.Add(Type:=msoControlButton)
cbctl2.Visible = True
cbctl2.Style = msoButtonCaption
cbctl2.Caption = "Submenu Button"
cbctl2.Tag = "SubmenuButton"
End If

End Sub

Thanks in anticipation.

.
~Oorang

fumei
05-28-2009, 09:21 AM
1. You are not telling us everything. If I execute your code, nothing happens because there is no Headings commandbar.

2. Ditto not telling us everything. You have:
Dim cbsub2 As CommandBarControl
Dim cbsub3 As CommandBarControl
Dim cbctl2 As CommandBarControl
in your MainmenYou procedure, but never use them...why?

You do use them in your SubMenuYou procedure, but Option Explicit should, in fact, stop you from even executing as it should return the "Variable not defined" compile error...right here:
Set cbsub2 = oCustomMenuItem.Controls.Add(Type:=msoControlPopup)
because...well, cbsub2 is NOT defined within the Scope of SubMenuYou. cbsub2 is ONLY in Scope in MainMenYou.

RustyCocoPat
05-28-2009, 01:33 PM
Thanks for that. I simply omitted the other menu item and button to save space while showing my method.

Funny that that code doesn't work for you because it does for me (running Office 2003 on XP.)

From your comments that it should be able to run anyway due to the lack of a Headings CommandBar and the Option Explicit issue, maybe it is picking up code from somewhere else - but how?

Here are the whole procedures:

Public Sub MainMenuYou()

Dim cbpop As CommandBarControl
Dim cbsub2 As CommandBarControl
Dim cbsub3 As CommandBarControl
Dim cbctl2 As CommandBarControl

CustomizationContext = ActiveDocument.AttachedTemplate

Set cbpop = CommandBars("Headings").Controls.Add(Type:=msoControlPopup, Before:=1)
cbpop.Caption = "=> Main Menu"
cbpop.Tag = "MainMenu"
cbpop.OnAction = "SubMenYou"

End Sub


Sub SubMenYou()

CustomizationContext = ActiveDocument.AttachedTemplate

Dim cbpop As CommandBar
Dim oCustomMenuItem As CommandBarControl
Dim sMenuItemTag As String
Dim cbsub2 As CommandBarControl
Dim cbsub3 As CommandBarControl
Dim cbctl2 As CommandBarControl


sMenuItemTag = "MainMenu"
Set cbpop = Application.CommandBars("Headings")
Set oCustomMenuItem = cbpop.FindControl(Tag:=sMenuItemTag)

Set cbsub2 = oCustomMenuItem.Controls.Add(Type:=msoControlPopup)
cbsub2.Visible = True
cbsub2.Caption = "Submenu 1"
cbsub2.Tag = "Submenu1"

Set cbsub3 = cbsub2.Controls.Add(Type:=msoControlPopup)
cbsub3.Visible = True
cbsub3.Caption = "Submenu 2"
cbsub3.Tag = "Submenu2"


sMenuItemTag = "SubmenuButton"
Set oFileMenu = Application.CommandBars("Headings")
Set oCustomMenuItem = oFileMenu.FindControl(Tag:=sMenuItemTag)

If Not oCustomMenuItem Is Nothing Then
oCustomMenuItem.Delete
Else
Set cbctl2 = cbsub3.Controls.Add(Type:=msoControlButton)
cbctl2.Visible = True
cbctl2.Style = msoButtonCaption
cbctl2.Caption = "Submenu Button"
cbctl2.Tag = "SubmenuButton"
End If

End Sub

lucas
05-28-2009, 02:33 PM
Please edit your post and select your code and hit the vba button so it will be formatted for the forum.

RustyCocoPat
05-29-2009, 04:46 AM
Sorry. Didn't know about that.

RustyCocoPat
05-30-2009, 06:31 PM
Folks, any suggestions?

macropod
05-31-2009, 04:53 PM
Hi RustyCocoPat,

It seems you've asked the same question at: http://www.wopr.com/index.php?showtopic=763840
(as Jarosite99) and received the help you needed there.

It's rude - and a great waste of peoples' time - to post the same question in multiple forums and not let on.

RustyCocoPat
06-01-2009, 12:18 AM
Macropod, you made the same complaint about me with regard to another issue. It was pointed out to you by others that it is not inappropriate to post to different forums in other domains. See here: http://www.tek-tips.com/viewthread.cfm?qid=1538154. You were also adamant in that case that what I was seeking to do was impossible but you were shown to lack the knowledge of others who provided the simple answer.

In addition, I only posted to WOPR after I received no response from this forum. I think that you are being rude and intolerant and you would be better off trying to help people rather than finding fault with them.

lucas
06-01-2009, 07:41 AM
Well rusty old pal, I disagree with you on oh so many counts. It is indeed very rude to post in multiple forums without posting a link and if you continue to do it you will find yourself not finding anyone willing to help you anywhere.

In the thread you referenced it was not only Macropod who had the opinion which you disparage in this thread. These are respected members of the vba community and you are not.

Gerry posted a link in the thread that we use here and it will help you understand why you are the one who is out of line. You obviously did not even go to the link.

We are here to help you but if you crap on people why do you think they would help you again?

Read this link thoroughly before posting back here and remember we are nice people but we don't appreciate being abused:

http://www.excelguru.ca/node/7

I think you are being rude and arrogant especially considering that you are the one asking for free help and you want to act like everyone is on your payroll.......they are not.

You are welcome here as long as you understand that we decide, not you, what is acceptable behavior and you have very nearly crossed the line into oblivion as far as I am concerned.

I say how dare you talk to anyone the way you did, let alone people who are trying to help you.

This is your last opportunity to "get it" or you will be forced to move along. We are friendly here but you can't just treat members badly and expect to we welcome.

Thanks for your understanding.

ps just because you say you didn't understand is not an excuse. A link was provided and you obviously ignored it in lieu of your own opinion. I expect you to apologize especially to Macropod before you will be allowed to move forward here. Maybe you will decide this is not the place for you, I don't know and it is entirely up to you.

fumei
06-01-2009, 11:06 AM
Well, I think it is probably useless, but I will toss in my 2 cents.

Rusty: "you would be better off trying to help people rather than finding fault with them."

1. I do not think macropod, or anyone else, was trying to find fault. You have offered plenty quite freely.

2. Even a cursory look around - and I seriously doubt you have done this - would easily show that macropod has contributed and helped hundreds (perhaps thousands) of people in a very polite and sincere manner on a number of forums. When you have actual helped...uhhhh...anyone...then perhaps you can give criticism.

The point of the link I posted at Tek-Tips (and that Steve has posted again in this thread) - which I also think you did not bother to read - is that it is not WRONG to post in multiple places.

What is wrong is not informing people that you have done so. THAT is what is impolite. It wastes our time.

This, or any other forum, is not a Help Desk. We are not here to serve you, and not even to help you actually. We are not being paid anything to help you, or anyone else. Absolutely no person has any right whatsoever for anything from us. Nothing. We do not owe anyone anything. We do this for a number of reasons...but they are OUR reasons...not yours, or anyone else.

The vast majority of people who do a lot of helpful posting (like macropod), do it because they DO like to help. They have an interest in the subjects raised, and the experience and articulation to give helpful responses. That being said, most are (in some way) actual professionals, and deserve to treated as such. One way is to respect the time we put into these forums.

And one way to do THAT, is by - if you had read the linked article - informing us when you have cross-posted.

That is all. Just so we do not waste time. It is also annoying when someone gets an answer...but simply does not like it, and posts somewhere else trying to get an answer they do like.

While true, answers can be of varying quality, insistence on ignoring serious players - well, like macropod - is just plain counter-productive. Word gets around.

RustyCocoPat
06-01-2009, 02:46 PM
I would like you to understand that I am a hobby programmer only and rarely visit these forums. I did, in fact read the cross-posting information provided but was guided by Lilliabeth's response in the Tek-Tips forum as to practical usage. I now understand that cross-posting is OK if you acknowledge it in your post. Also, I do appreciate that the people who try to resolve others' problems do so in their own time and with goodwill. Having said all that, my submissions to the forum have been made in good faith and courteous yet I have been personally attacked. I don't like being told that I am rude or arrogant what I was only responding (not rudely) to unwarranted criticism. Can I suggest that, in future, if someone cross-posts inappropriately you don't declare them to be rude but allow for the fact that they may not be as familiar with the posting protocols as you are and simply and politely explain the cross-posting rules to them. In conclusion, I have found my engagement with this forum to be a painful experience so, trust me, I will not be back.

macropod
06-01-2009, 02:56 PM
Hi RustyCocoPat,

Re:

It was pointed out to you by others that it is not inappropriate to post to different forums in other domains. See here: http://www.tek-tips.com/viewthread.cfm?qid=1538154.
Perhaps you should take a look at the discussion in the link you posted. As you will see. what I said then was that It's rude to post the same question in numersous forums without acknowledging in any of them the advice you've been given in the others. I don't see any suggestion from anyone else there that this advice was in any way incorrect. Your claim:

You were also adamant in that case that what I was seeking to do was impossible but you were shown to lack the knowledge of others who provided the simple answer.
is a lie. I did not say what you wanted was impossible, others did - and were unanimous about that. I said nothing at all about the practicality or otherwise of what you asked.

As for you last effort:

simply and politely explain the cross-posting rules to them
do remember this was the second time in a fairly short period that you cross-posted without letting on. That is why you're being jumped on - especially when the best you can do is to attack those who've pointed out firmly and politely the correct etiquette.

lucas
06-01-2009, 03:36 PM
Having said all that, my submissions to the forum have been made in good faith and courteous yet I have been personally attacked. I don't like being told that I am rude or arrogant what I was only responding (not rudely) to unwarranted criticism. Can I suggest that, in future, if someone cross-posts inappropriately you don't declare them to be rude but allow for the fact that they may not be as familiar with the posting protocols as you are and simply and politely explain the cross-posting rules to them.

No, you were never attacked. You were informed in a friendly way that crossposting without a link is rude and it is.

There is a FAQ at every website I know of, including this one. Why do you think you are being ganged up on? If you had been here long you would know that people like you come along rarely but you are all the same. You want to act like people were rude to you when in reality the exact opposite is true.

I hope you find what you need elsewhere on the web unless you can learn to be civil and get along with others you will not be welcome here.