PC Review


Reply
Thread Tools Rate Thread

Declaring Command Bar Control

 
 
Orion Cochrane
Guest
Posts: n/a
 
      15th Oct 2008
I have a custom menu that is enabled under certain circumstances. I just want
to clean up my code a bit, so I wanted to declare a commandbar control at the
outset of my procedure, and I keep getting an "Invalid use of property"
error. Here is my code:
Sub MenuEnable()
'Enables / disables Reports submenus
On Error GoTo NoFile
Dim spath As String
spath = ActiveWorkbook.Path
If Left(spath, 22) = "<path>" Or _
Left(spath, 28) = "<path>" Then
CommandBars(1).Controls("Main").Controls("Control1").Enabled = True
Else: CommandBars(1).Controls("Main").Controls("Control1").Enabled = False
End If
If spath = "<path>" Then
CommandBars(1).Controls("Main").Controls("Control2").Enabled = True
Else: CommandBars(1).Controls("Main").Controls("Control2").Enabled = False
End If
Exit Sub
NoFile:
MsgBox "There is no active workbook open.", vbCritical, "Error: Main Menu"
End Sub

This macro is triggered when I click on "Main" in the Worksheet Menu Bar
(CommandBars(1)). If there is no active workbook open, the error handler
kicks in (it kicks in probably under other circumstances, but this is the
only one I can think of). The constant is CommandBars(1).Controls("Main").
How do I go about declaring this so I can clean up this code?

I tried: (Dim rpt as...)
CommandBar
CommandBars
CommandBarControl
CommandBarControls

I do not have any formal training in VBA, and any macros I write are a
result of experimentation (which gets me very far, but even I know when I am
beat).

The code presented here works. I hope I explained it enough. Thanks in
advance.
--
I am running on Excel 2003, unless otherwise stated. Please rate posts so we
know when we have answered your questions. Thanks.
 
Reply With Quote
 
 
 
 
Jim Cone
Guest
Posts: n/a
 
      15th Oct 2008
Dim rpt As CommandBarControl
Set rpt = Application.CommandBars(1).Controls("Main")
--
Jim Cone
Portland, Oregon USA



"Orion Cochrane"
wrote in message
I have a custom menu that is enabled under certain circumstances.
I just want to clean up my code a bit, so I wanted to declare a
commandbar control at the outset of my procedure,
and I keep getting an "Invalid use of property" error.
Here is my code:
Sub MenuEnable()
'Enables / disables Reports submenus
On Error GoTo NoFile
Dim spath As String
spath = ActiveWorkbook.Path
If Left(spath, 22) = "<path>" Or _
Left(spath, 28) = "<path>" Then
CommandBars(1).Controls("Main").Controls("Control1").Enabled = True
Else: CommandBars(1).Controls("Main").Controls("Control1").Enabled = False
End If
If spath = "<path>" Then
CommandBars(1).Controls("Main").Controls("Control2").Enabled = True
Else: CommandBars(1).Controls("Main").Controls("Control2").Enabled = False
End If
Exit Sub
NoFile:
MsgBox "There is no active workbook open.", vbCritical, "Error: Main Menu"
End Sub

This macro is triggered when I click on "Main" in the Worksheet Menu Bar
(CommandBars(1)). If there is no active workbook open, the error handler
kicks in (it kicks in probably under other circumstances, but this is the
only one I can think of). The constant is CommandBars(1).Controls("Main").
How do I go about declaring this so I can clean up this code?

I tried: (Dim rpt as...)
CommandBar
CommandBars
CommandBarControl
CommandBarControls

I do not have any formal training in VBA, and any macros I write are a
result of experimentation (which gets me very far,
but even I know when I am beat).
The code presented here works. I hope I explained it enough.
Thanks in advance.
--
I am running on Excel 2003, unless otherwise stated. Please rate posts so we
know when we have answered your questions. Thanks.
 
Reply With Quote
 
Orion Cochrane
Guest
Posts: n/a
 
      15th Oct 2008
Thanks. I didn't know about set. When is it used?
--
I am running on Excel 2003, unless otherwise stated. Please rate posts so we
know when we have answered your questions. Thanks.


"Jim Cone" wrote:

> Dim rpt As CommandBarControl
> Set rpt = Application.CommandBars(1).Controls("Main")
> --
> Jim Cone
> Portland, Oregon USA
>
>
>
> "Orion Cochrane"
> wrote in message
> I have a custom menu that is enabled under certain circumstances.
> I just want to clean up my code a bit, so I wanted to declare a
> commandbar control at the outset of my procedure,
> and I keep getting an "Invalid use of property" error.
> Here is my code:
> Sub MenuEnable()
> 'Enables / disables Reports submenus
> On Error GoTo NoFile
> Dim spath As String
> spath = ActiveWorkbook.Path
> If Left(spath, 22) = "<path>" Or _
> Left(spath, 28) = "<path>" Then
> CommandBars(1).Controls("Main").Controls("Control1").Enabled = True
> Else: CommandBars(1).Controls("Main").Controls("Control1").Enabled = False
> End If
> If spath = "<path>" Then
> CommandBars(1).Controls("Main").Controls("Control2").Enabled = True
> Else: CommandBars(1).Controls("Main").Controls("Control2").Enabled = False
> End If
> Exit Sub
> NoFile:
> MsgBox "There is no active workbook open.", vbCritical, "Error: Main Menu"
> End Sub
>
> This macro is triggered when I click on "Main" in the Worksheet Menu Bar
> (CommandBars(1)). If there is no active workbook open, the error handler
> kicks in (it kicks in probably under other circumstances, but this is the
> only one I can think of). The constant is CommandBars(1).Controls("Main").
> How do I go about declaring this so I can clean up this code?
>
> I tried: (Dim rpt as...)
> CommandBar
> CommandBars
> CommandBarControl
> CommandBarControls
>
> I do not have any formal training in VBA, and any macros I write are a
> result of experimentation (which gets me very far,
> but even I know when I am beat).
> The code presented here works. I hope I explained it enough.
> Thanks in advance.
> --
> I am running on Excel 2003, unless otherwise stated. Please rate posts so we
> know when we have answered your questions. Thanks.
>

 
Reply With Quote
 
Jim Cone
Guest
Posts: n/a
 
      15th Oct 2008

"Set" is required for all objects.
It tells Excel which object the variable refers to.
--
Jim Cone
Portland, Oregon USA



"Orion Cochrane"
wrote in message
Thanks. I didn't know about set. When is it used?
--

 
Reply With Quote
 
Orion Cochrane
Guest
Posts: n/a
 
      15th Oct 2008
OK. Thanks. I have only declared strings and VbMsgBoxResults. This is my
first attempt at declaring an object. Lots to learn.
--
I am running on Excel 2003, unless otherwise stated. Please rate posts so we
know when we have answered your questions. Thanks.


"Jim Cone" wrote:

>
> "Set" is required for all objects.
> It tells Excel which object the variable refers to.
> --
> Jim Cone
> Portland, Oregon USA
>
>
>
> "Orion Cochrane"
> wrote in message
> Thanks. I didn't know about set. When is it used?
> --
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Declaring variables for multiple command buttons RussCRM Microsoft Access Form Coding 5 28th Mar 2008 02:09 PM
Declaring PageMethods within Custom Web Control Damien Microsoft ASP .NET 1 15th Aug 2007 07:27 PM
Declaring a user control as a parameter =?Utf-8?B?VG9ueSBIZWRnZQ==?= Microsoft Dot NET 3 8th Nov 2006 04:04 AM
Declaring control array Nathan Microsoft VB .NET 18 5th Jan 2004 03:07 PM
Re: Declaring control in parent Herfried K. Wagner [MVP] Microsoft Dot NET Framework Forms 0 29th Aug 2003 12:25 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:34 PM.