6

Is it possible to check whether an object supports a certain method without an error handler in VBA?

I've found numerous duplicates asking the question for for example JavaScript and Symphony2, but not yet in VBA.

I would like to use a .sendkeys "{ENTER}" command to an ie.document class item and learning how to check whether the object supports a method allows me to write cleaner code in the long run.

example code:

Set elements(17) = ie.document.getElementsByClassName("ng-binding ng-scope")
for each item in elements(17)
    item.sendkeys "{ENTER}"
next item
a.t.
  • 2,002
  • 3
  • 26
  • 66

1 Answers1

6

Short of looking at the documentation for the API you're using, you can't.

At least not on late-bound code. By definition, late-bound code is resolved at run-time, which means you have no compile-time way of validating whether a member will be available on that object's interface - that's why this code compiles:

Option Explicit

Public Sub Test(ByVal o As Object)
    Debug.Print o.FooBarBazz
End Sub

Or this somewhat more realistic one:

Debug.Print ThisWorkbook.Worksheets("Test").Naame 'Worksheets.Item returns an Object

The only way to know before run-time whether a member is supported, is to use early-bound calls:

Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Test")
Debug.Print ws.Naame ' typo won't compile!

Of course it's not that simple, because even early-bound interfaces can have a COM flag saying they're "extensible". Excel.Application is one such interface:

Debug.Print Excel.Application.FooBarBazz ' compiles!

But I'm drifting.

You say "without error handling" ...does this count?

On Error Resume Next
item.sendkeys "{ENTER}"
'If Err.Number = 438 Then Debug.Print "Not supported!"
On Error GoTo 0
Mathieu Guindon
  • 69,817
  • 8
  • 107
  • 235
  • Thank you very much @Mat's Mug, for your overcomplete, and direct answer. I understood little of the explanation, and I will come back to this question to see if I can follow the argumentation once I have a broader basis and understanding of the concepts/terminology. As far as I am aware, it does not count for "without error handling", but I would like to thank you for the contribution, I had not added the last `Next`, which was a problem that led me to pose this question in first instance. – a.t. Sep 26 '17 at 20:03
  • 1
    @a.t. you can think of "early-bound" as anything that's referring to a library that's referenced by your project (see Tools > References): these are resolved at compile-time. "late-bound" is any call made against an `Object` or `Variant/Object` - VBA resolves the member at run-time (and throws error 438 if it doesn't find it). – Mathieu Guindon Sep 26 '17 at 20:14
  • 1
    Thank you for the introduction, it led me to a more elaborate explanation on run-time and compile time code [here.](https://stackoverflow.com/questions/846103/runtime-vs-compile-time#846421) – a.t. Sep 26 '17 at 20:23
  • It's possible when the object itself has a method to evaluate if it supports a method. It is the case here with all the objects under `ie.document`. – Florent B. Sep 26 '17 at 23:16
  • @FlorentB. That's rather implementation-specific though (IDK, I don't do IE automation). Referencing the type library (working with early-bound objects) and using the *Object Browser* to see what members are supported on what types, seems a much simpler and universal solution IMO. For some reason *everyone* uses late-bound code to do this - and I bet it has something to do with the sheer amount of SO questions involving that type library. – Mathieu Guindon Sep 26 '17 at 23:22
  • @Mat's Mug, members created at runtime are not present in the Object Browser since it only enumerates the ones declared in the interface. – Florent B. Sep 26 '17 at 23:35
  • Aah! Well that explains a lot! Thanks! – Mathieu Guindon Sep 26 '17 at 23:38