+ Reply to Thread
Results 1 to 3 of 3

How to tell if an object has a method?

Hybrid View

AdamBecker How to tell if an object has... 05-10-2013, 10:18 AM
JosephP Re: How to tell if an object... 05-10-2013, 10:43 AM
AdamBecker Re: How to tell if an object... 05-10-2013, 01:24 PM
  1. #1
    Registered User
    Join Date
    12-18-2012
    Location
    Houston TX
    MS-Off Ver
    Excel 2007
    Posts
    19

    How to tell if an object has a method?

    How do I tell if an object has a method? I'm wanting to write a function in a regular module, something like

    public function NextSpan(Cnt as Object) as string
      if( HasMethod(Cnt, "Span") then
         NextSpan = Cnt.Span
      else
         NextSpan = DefaultSpan()
    end function
    I suppose I could just trap the error
    public function NextSpan(Cnt as Object) as string
      on error GoTo DefaultSpan
      NextSpan = Cnt.Span
      exit function
    DefaultSpan:
         NextSpan = DefaultSpan()
    End Sub
    But that doesn't distinguish between the cases where Cnt doesn't have the method, and cases where Cnt has the method, but it's buggy and throwing an error.

  2. #2
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: How to tell if an object has a method?

    perhaps
    Function HasMethod(o As Object, sMethod As String) As Boolean
        On Error Resume Next
        CallByName o, sMethod, VbMethod
        HasMethod = Err.Number <> 438
    End Function
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  3. #3
    Registered User
    Join Date
    12-18-2012
    Location
    Houston TX
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: How to tell if an object has a method?

    Thanks. That tells me
    - there's no native VBA function which would directly give me what I'm looking for.
    - there's a reasonable workaround.

    It also led me to research VbMethod, and discover its siblings, VbLet, VbSet and VbGet.

+ 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