Why does this work?

  • Thread starter Thread starter J S
  • Start date Start date
J

J S

This code does not work and generates an error like "Object required"

Dim oIE
Set oIE = CreateObject("InternetExplorer.Application")
WaitUntilReady (oIE)

Public Sub WaitUntilReady(oIE As Object)
End Sub

This code does work! Why do I need to declare the oIE variable as an object?
Should VB know that its an object after I Set it equal to one???

Dim oIE as Object
Set oIE = CreateObject("InternetExplorer.Application")
WaitUntilReady (oIE)

Public Sub WaitUntilReady(oIE As Object)
End Sub
 
J S said:
This code does not work and generates an error like "Object required"

Dim oIE
Set oIE = CreateObject("InternetExplorer.Application")
WaitUntilReady (oIE)

Public Sub WaitUntilReady(oIE As Object)
End Sub

This code does work! Why do I need to declare the oIE variable as an
object? Should VB know that its an object after I Set it equal to
one???

Dim oIE as Object
Set oIE = CreateObject("InternetExplorer.Application")
WaitUntilReady (oIE)

Public Sub WaitUntilReady(oIE As Object)
End Sub

Begging your pardon, but that code you say "does work" does *not*
work -- not on my system, at least, and I wouldn't expect it to. Your
calling syntax is incorrect for passing the oIE variable. You must
either write

WaitUntilReady oIE

or

Call WaitUntilReady(oIE)

to pass the object variable to the subroutine. By wrapping it in
parentheses unnecessarily, you are telling VB to pass the *value* of the
object; and what is the value of an IE application? As best I can tell
it's the string value "Microsoft Internet Explorer". This does work:

Dim oIE As Object
Set oIE = CreateObject("InternetExplorer.Application")
WaitUntilReady oIE

On the other hand, suppose you correct that error and still leave off
the "As Object", so you have

Dim oIE
Set oIE = CreateObject("InternetExplorer.Application")
WaitUntilReady oIE

The above doesn't compile, giving you "ByRef argument type mismatch".
That's because, by not specifying the type when you declare oIE, you are
declaring it as a Variant. WaitUntilReady wants an Object, not a
Variant -- not even a Variant that points to an Object, apparently.
Rudimentary tests suggest that if you declare WaitUntilReady to accept
its argument passed by value, not by reference ...

Public Sub WaitUntilReady(ByVal oIE As Object)

.... then it will work even with oIE as a Variant, so long as it points
to a suitable object. But there's no good reason for doing that, since
simply declaring oIE As Object, not as Variant, resolves the matter.
 
J S said:
Dim oIE
Set oIE = CreateObject("InternetExplorer.Application")
WaitUntilReady (oIE)

Public Sub WaitUntilReady(oIE As Object)
End Sub

What happens here is that the oIE variant remains unassigned until the IE
application is available, so it presumably has a value of Empty. This is
not a valid value for an Object, so it cannot be coerced, and VBA reports
that as an error. Change the function declaration to

Public Sub WaitUntilReady( oIE As Variant)

and you will avoid the message. It is an amateur error to pass one datatype
to a function expecting a different datatype without making an explicit
cast on the way. Variants are particularly prone to this.
Dim oIE as Object
Set oIE = CreateObject("InternetExplorer.Application")
WaitUntilReady (oIE)

Public Sub WaitUntilReady(oIE As Object)
End Sub

In this case, the unassigned Object variable can only be Nothing, so that
is what gets passed to the sub, and is quite variable. You may notice that
the Dim and the Sub declarations match this time.


Hope that helps


Tim F
 
Back
Top