+ Reply to Thread
Results 1 to 14 of 14

New menu item OnAction can't see macro?

  1. #1
    Ed
    Guest

    New menu item OnAction can't see macro?

    I create a menu with two items in the list. The menu is created with a
    Workbook_Open macro in the ThisWorkbook module. The macro called by the
    menu item's OnAction also resides in the ThisWorkbook module. But when
    selecting that menu item, I get the error "Macro 'Workbook!Macro' does not
    exist." I call the same macro within the code of a
    Workbook_SheetSelectionChange macro, again in the ThisWorkbook module, and
    it works fine. What am I missing?

    Ed



  2. #2
    Jim Thomlinson
    Guest

    RE: New menu item OnAction can't see macro?

    The sub called by OnAction has to reside in a Module. It can not reside in
    ThisWorkbook or a sheet. Just the way it works. You could put a public sub in
    a module that calls a sub in thisworkbook if you want to.

    HTH

    "Ed" wrote:

    > I create a menu with two items in the list. The menu is created with a
    > Workbook_Open macro in the ThisWorkbook module. The macro called by the
    > menu item's OnAction also resides in the ThisWorkbook module. But when
    > selecting that menu item, I get the error "Macro 'Workbook!Macro' does not
    > exist." I call the same macro within the code of a
    > Workbook_SheetSelectionChange macro, again in the ThisWorkbook module, and
    > it works fine. What am I missing?
    >
    > Ed
    >
    >
    >


  3. #3
    JE McGimpsey
    Guest

    Re: New menu item OnAction can't see macro?

    You should put the macro in a regular code module.

    ThisWorkbook is a workbook class module, which means that workbook event
    procedures will by default call procedures within the class. When you
    call a class procedure from outside the class (e.g., from a menu's
    OnAction event), you need to fully qualify the reference, e.g.,

    .OnAction ="Workbook1.xls!ThisWorkbook.foo"

    From a menu item, you don't need to include the class module:

    .OnAction = "Workbook1.xls!bar"

    See

    http://cpearson.com/excel/codemods.htm

    In article <OpD5Iow$EHA.3840@tk2msftngp13.phx.gbl>,
    "Ed" <ed_millis@NO_SPAM.yahoo.com> wrote:

    > I create a menu with two items in the list. The menu is created with a
    > Workbook_Open macro in the ThisWorkbook module. The macro called by the
    > menu item's OnAction also resides in the ThisWorkbook module. But when
    > selecting that menu item, I get the error "Macro 'Workbook!Macro' does not
    > exist." I call the same macro within the code of a
    > Workbook_SheetSelectionChange macro, again in the ThisWorkbook module, and
    > it works fine. What am I missing?


  4. #4
    JE McGimpsey
    Guest

    Re: New menu item OnAction can't see macro?

    Actually, it *can* reside in the ThisWorkbook module (which is also a
    "Module" - it's just a special instance of a class module, rather than a
    regular/standard module). To use it, though, the reference has to
    include the class name, e.g.:

    Workbook1.xls!ThisWorkbook.foo

    In article <1C92C6C0-30C6-427D-B5C8-1A3C4EA28EAC@microsoft.com>,
    "Jim Thomlinson" <JimThomlinson@discussions.microsoft.com> wrote:

    > The sub called by OnAction has to reside in a Module. It can not reside in
    > ThisWorkbook or a sheet. Just the way it works. You could put a public sub in
    > a module that calls a sub in thisworkbook if you want to.


  5. #5
    Jim Thomlinson
    Guest

    Re: New menu item OnAction can't see macro?

    And you learn something new each and every day... Thanks...

    "JE McGimpsey" wrote:

    > Actually, it *can* reside in the ThisWorkbook module (which is also a
    > "Module" - it's just a special instance of a class module, rather than a
    > regular/standard module). To use it, though, the reference has to
    > include the class name, e.g.:
    >
    > Workbook1.xls!ThisWorkbook.foo
    >
    > In article <1C92C6C0-30C6-427D-B5C8-1A3C4EA28EAC@microsoft.com>,
    > "Jim Thomlinson" <JimThomlinson@discussions.microsoft.com> wrote:
    >
    > > The sub called by OnAction has to reside in a Module. It can not reside in
    > > ThisWorkbook or a sheet. Just the way it works. You could put a public sub in
    > > a module that calls a sub in thisworkbook if you want to.

    >


  6. #6
    Ed
    Guest

    Re: New menu item OnAction can't see macro?

    Thank you. At least it was something I didn't know, rather than something I
    screwed up!

    Ed

    "JE McGimpsey" <jemcgimpsey@mvps.org> wrote in message
    news:jemcgimpsey-E17405.10193920012005@msnews.microsoft.com...
    > You should put the macro in a regular code module.
    >
    > ThisWorkbook is a workbook class module, which means that workbook event
    > procedures will by default call procedures within the class. When you
    > call a class procedure from outside the class (e.g., from a menu's
    > OnAction event), you need to fully qualify the reference, e.g.,
    >
    > .OnAction ="Workbook1.xls!ThisWorkbook.foo"
    >
    > From a menu item, you don't need to include the class module:
    >
    > .OnAction = "Workbook1.xls!bar"
    >
    > See
    >
    > http://cpearson.com/excel/codemods.htm
    >
    > In article <OpD5Iow$EHA.3840@tk2msftngp13.phx.gbl>,
    > "Ed" <ed_millis@NO_SPAM.yahoo.com> wrote:
    >
    > > I create a menu with two items in the list. The menu is created with a
    > > Workbook_Open macro in the ThisWorkbook module. The macro called by the
    > > menu item's OnAction also resides in the ThisWorkbook module. But when
    > > selecting that menu item, I get the error "Macro 'Workbook!Macro' does

    not
    > > exist." I call the same macro within the code of a
    > > Workbook_SheetSelectionChange macro, again in the ThisWorkbook module,

    and
    > > it works fine. What am I missing?




  7. #7
    JE McGimpsey
    Guest

    Re: New menu item OnAction can't see macro?

    Should have been:

    From a regular module, where non-event macros should reside,

    In article <jemcgimpsey-E17405.10193920012005@msnews.microsoft.com>,
    JE McGimpsey <jemcgimpsey@mvps.org> wrote:

    > From a menu item,


  8. #8
    Jamie Collins
    Guest

    Re: New menu item OnAction can't see macro?


    JE McGimpsey wrote:
    > Should have been:
    >
    > From a regular module, where non-event macros should reside,


    The implicit suggestion is that non-event code has no place in the
    ThisWorkbook code module. I must disagree. I find ThisWorkbook far too
    convenient; it would be a waste to restrict myself in this way. If I
    have custom properties or methods which operate on the ThisWorkbook
    object (e.g. an IsProtected property for the workbook), for me the
    ThisWorkbook code module seems the most logical place for the code.

    ....But then I have an aversion to standard modules <g>. I do not use
    them unless I have to (e.g. required for a UDF called from a cell
    formula) and therefore do not use OnAction. Here is an alternative
    approach using only the ThisWorkbook code module:

    Option Explicit

    Private WithEvents m_Menu1 As CommandBarButton
    Private WithEvents m_Menu2 As CommandBarButton

    Private Sub Workbook_Open()
    With Application.CommandBars("Worksheet Menu Bar") _
    ..Controls(1).CommandBar
    Set m_Menu1 = .Controls(1) ' File, New
    Set m_Menu2 = .Controls(2) ' File, Open
    End With
    End Sub

    Private Sub m_Menu1_Click( _
    ByVal Ctrl As Office.CommandBarButton, _
    CancelDefault As Boolean _
    )
    MsgBox "Menu1 handler"
    End Sub

    Private Sub m_Menu2_Click( _
    ByVal Ctrl As Office.CommandBarButton, _
    CancelDefault As Boolean _
    )
    MsgBox "Menu2 handler"
    End Sub

    Jamie.

    --


  9. #9
    JE McGimpsey
    Guest

    Re: New menu item OnAction can't see macro?

    FWIW, since your technique inherently breaks cross-platform apps, it
    would be worthless to me and most of my clients.

    My comments were directed at what I perceived as the knowledge
    level/technique employed by the OP, who wished to use OnAction. There
    are, of course, almost always alternatives.

    Even if I used your technique, I would probably still use a separate
    class module rather than ThisWorkbook - it seems cleaner to me. De
    gustibus non disputandum est (as your aversion to standard modules
    attests).

    In article <1106568319.377518.19200@z14g2000cwz.googlegroups.com>,
    "Jamie Collins" <jamiecollins@xsmail.com> wrote:

    > The implicit suggestion is that non-event code has no place in the
    > ThisWorkbook code module. I must disagree. I find ThisWorkbook far too
    > convenient; it would be a waste to restrict myself in this way.


  10. #10
    Jamie Collins
    Guest

    Re: New menu item OnAction can't see macro?


    JE McGimpsey wrote:
    > since your technique inherently breaks cross-platform apps, it
    > would be worthless to me and most of my clients.


    Could you expand on that please?

    > if I used your technique, I would probably still use a separate
    > class module rather than ThisWorkbook - it seems cleaner to me.


    Interesting. I don't see the advantage of using a class module. There
    would be no code re-use within the project; you'd only need once
    instance. There is unlikely to be reuse between projects. Encapsulation
    for its own sake? It seems to me that if something is 'workbook level'
    it is more 'logical' to me to have it in the ThisWorkbook module.
    Jamie.

    --


  11. #11
    JE McGimpsey
    Guest

    Re: New menu item OnAction can't see macro?

    ActiveX control events work only in the Windows environment. I get paid
    pretty well to make my apps work in both WinOffice and MacOffice.

    In article <1106670811.843941.103260@z14g2000cwz.googlegroups.com>,
    "Jamie Collins" <jamiecollins@xsmail.com> wrote:

    > Could you expand on that please?


  12. #12
    JE McGimpsey
    Guest

    Re: New menu item OnAction can't see macro?

    Like I said, there's no accounting for taste...

    In article <1106670811.843941.103260@z14g2000cwz.googlegroups.com>,
    "Jamie Collins" <jamiecollins@xsmail.com> wrote:

    > Interesting. I don't see the advantage of using a class module. There
    > would be no code re-use within the project; you'd only need once
    > instance. There is unlikely to be reuse between projects. Encapsulation
    > for its own sake? It seems to me that if something is 'workbook level'
    > it is more 'logical' to me to have it in the ThisWorkbook module.


  13. #13
    Jamie Collins
    Guest

    Re: New menu item OnAction can't see macro?


    JE McGimpsey wrote:
    > ActiveX control events work only in the Windows environment. I get

    paid
    > pretty well to make my apps work in both WinOffice and MacOffice.


    I get it now. I'm so MS-centric these days I thought you meant cross
    Windows platforms. Lucrative market, eh? Thanks for the tip!
    Jamie.

    --


  14. #14
    JE McGimpsey
    Guest

    Re: New menu item OnAction can't see macro?

    more like a niche market...

    In article <1106691970.375498.79520@z14g2000cwz.googlegroups.com>,
    "Jamie Collins" <jamiecollins@xsmail.com> wrote:

    > Lucrative market, eh?


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1