0

I have menu with submenu made of control buttons:

Set MenuItem = .Controls.Add(Type:=msoControlPopup)
With MenuItem
    .caption = "Code Type"

    With .Controls.Add(Type:=msoControlButton)
        .caption = "Lists"
        .OnAction = "'" & ThisWorkbook.Name & "'!" & "setCaption"
    End With

End With

Sub setCaption()
    MsgBox Application.caller
End Sub

When I try with the above example, I get Type mismatch error.

Mathieu Guindon
  • 69,817
  • 8
  • 107
  • 235
kittu
  • 6,662
  • 21
  • 91
  • 185
  • What does `MsgBox TypeName(Application.Caller)` give you? – Mathieu Guindon May 02 '20 at 16:08
  • @MathieuGuindon it gives variant – kittu May 02 '20 at 16:29
  • Weird, TypeName returns the runtime type, that... *should* be the variant subtype. Does `MsgBox TypeOf Application.Caller Is Object` return true? The idea is to get what type the caller is, IIRC should be a `CommandBarButton`. Alternatively you could take a string parameter and pass it along in the `OnAction` string, that would be more reliable than Application.Caller IMO. – Mathieu Guindon May 02 '20 at 16:35
  • @MathieuGuindon `MsgBox TypeOf Application.Caller Is Object` gives False – kittu May 02 '20 at 16:36
  • Where's the type mismatch error being raised exactly? – Mathieu Guindon May 02 '20 at 16:38
  • @MathieuGuindon At application.caller – kittu May 02 '20 at 16:41
  • @MathieuGuindon When I pass arg to Sub function, it says macro not found – kittu May 02 '20 at 16:42
  • Try [this approach](https://stackoverflow.com/a/25097883/1188513), or [this one](https://stackoverflow.com/a/25097792/1188513) - remember that string delimiters `"` should be doubled-up inside string literals. – Mathieu Guindon May 02 '20 at 16:44
  • For a commandbar control `Application.Caller(1)` seems to return the control's index. How about `OnAction = "'" & ThisWorkbook.Name & "'!" & .Controls(Application.Caller(1).Caption)` – Doug Glancy May 02 '20 at 17:06

0 Answers0