To Object or not to object

LisaGreen
5StarLounger
Posts: 964
Joined: 08 Nov 2012, 17:54

To Object or not to object

Post by LisaGreen »

HEllo everyone,

Can someone tell me the difference between declaring something specifically with a reference in the references and declaring an object please?

More specifically, what's the difference between...

Dim vbclComponent As VBIDE.VBComponent
and..
Dim vbclComponent As Object

... for work within VBE to say, add lines of code.?

TIA
Lisa

YasserKhalil
PlatinumLounger
Posts: 4913
Joined: 31 Aug 2016, 09:02

Re: To Object or not to object

Post by YasserKhalil »

Using this line

Code: Select all

Dim vbclComponent As VBIDE.VBComponent
requires a reference as this is called ealry binding and this helps during the devising of the code as it shows the methods and properties associated to that object

While the line using As Object doesn't require a reference but doesn't help you while writing the code and this s called Late Binding

LisaGreen
5StarLounger
Posts: 964
Joined: 08 Nov 2012, 17:54

Re: To Object or not to object

Post by LisaGreen »

Hi Yasser,

Thank you for answering!

I was wondering if there was a more "fundamental" difference. Possibly in terms of what properties each exposes.

Lisa

YasserKhalil
PlatinumLounger
Posts: 4913
Joined: 31 Aug 2016, 09:02

Re: To Object or not to object

Post by YasserKhalil »

I have read that using Early Binding is a bit faster than using the Late Binding .. but I don't see that difference

User avatar
HansV
Administrator
Posts: 78493
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: To Object or not to object

Post by HansV »

Although, as YasserKhalil mentioned, writing code is easier if you use early binding (setting a reference to the specific library), there are situations in which late binding is preferable.
For example, if you automate Excel from Word, you need to use an Excel.Application object.
If you set a reference to the Microsoft Excel n.0 Object Library (early binding), you can declare

Dim objExcel As Excel.Application
Set objExcel = New Excel.Application

and you will profit from IntelliSense.

BUT... older versions of Word won't recognize a reference to a newer version of the Excel library. For example, if you have Office 2016/2019, you would set a reference to the Microsoft Excel 16.0 Object Library. A user who has Office 2013 or earlier would see MISSING: Microsoft Excel 16.0 Object Library in Tools > References and would experience errors when they try to run VBA code.

If you don't set a reference (late binding), you have to declare the variable as an Object:

Dim objExcel As Object
Set objExcel = CreateObject("Excel.Application")

You won't be able to use IntelliSense, but the code will work in older versions of Word too (as long as you don't use features that were added in the newer version).
So if you need your project to work with older versions of Office than the one you have, you must use late binding.
Best wishes,
Hans

User avatar
Doc.AElstein
BronzeLounger
Posts: 1499
Joined: 28 Feb 2015, 13:11
Location: Hof, Bayern, Germany

Re: To Object or not to object

Post by Doc.AElstein »

Hi Lisa
The general points I think are as Yasser said. ( Edit: and Hans just said also ) .

As far as this is concerned
LisaGreen wrote:. Specifically …..rence between...
Dim vbclComponent As VBIDE.VBComponent
and..
Dim vbclComponent As Object
... for work within VBE to say, add lines of code.?
I don’t know for sure, but I am thinking.._
Dim vbclComponent As VBIDE.VBComponent
_..will mean that you can get to all the things from that VBIDE class/ library thingy, and use all Methods , Properties, etc.. . But if you only do.._
Dim vbclComponent As Object
_..then you are relying on an instantiated one of the things from that VBIDE class/ library thingy being there already, and then you can only get at some of the stuff which that particular thing you later Set to that object has… ( so like then the thing you Set the object to later has lines in it .. sort of ). In your case, using the Dim vbclComponent As Object is less to do with Binding issues, and more to do with seting to an existing object.. maybe..

That is my uneducated way of explaining it.
Last edited by Doc.AElstein on 02 Jan 2019, 21:06, edited 4 times in total.
I am having difficulty logging in with this account just now.
You can find me at DocAElstein also

User avatar
HansV
Administrator
Posts: 78493
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: To Object or not to object

Post by HansV »

See Post=61085 for some instructions on using late binding.
Best wishes,
Hans

LisaGreen
5StarLounger
Posts: 964
Joined: 08 Nov 2012, 17:54

Re: To Object or not to object

Post by LisaGreen »

Yes. It could be. But, as always, a lot depends on what you are doing! Some processes take longer than others. Computers have different speeds and so on and so on. For most "small" programs, my humble opinion is that it's not all that important.

I was looking for an answer of the sort... You can't do this with Dim as Object but you can with Dim as Specific Reference.

Lisa

LisaGreen
5StarLounger
Posts: 964
Joined: 08 Nov 2012, 17:54

Re: To Object or not to object

Post by LisaGreen »

Hans!!!

Your experience is showing again!!

Thank you!!

LisaGreen
5StarLounger
Posts: 964
Joined: 08 Nov 2012, 17:54

Re: To Object or not to object

Post by LisaGreen »

@Alan,

Then you have answered your comment about the book where I say you *Microsoft Visual Basic for Applications Extenibility 5.3*.

Which is where my Q comes from.

Lisa'

User avatar
HansV
Administrator
Posts: 78493
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: To Object or not to object

Post by HansV »

You can do exactly the same things with late binding as with early binding. The same properties, methods and events are available. The difference is in the end user experience, as mentioned in one of my replies above.
Best wishes,
Hans

LisaGreen
5StarLounger
Posts: 964
Joined: 08 Nov 2012, 17:54

Re: To Object or not to object

Post by LisaGreen »

Thank you for clearing that up Hans.

Sooooo.... that means that to do stuff in the VBE you don't actually *need*, as a lot of sites say, a reference to the extensibility library???

Lisa

User avatar
HansV
Administrator
Posts: 78493
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: To Object or not to object

Post by HansV »

No, you don't. But keep in mind that the code will be more difficult to write, since you can't rely on IntelliSense.
Best wishes,
Hans

User avatar
Doc.AElstein
BronzeLounger
Posts: 1499
Joined: 28 Feb 2015, 13:11
Location: Hof, Bayern, Germany

Re: To Object or not to object

Post by Doc.AElstein »

Hi Lisa, I thought your question might be coming form there… :)
LisaGreen wrote:..you have answered your comment about the book where I say you *Microsoft Visual Basic for Applications Extenibility 5.3*....'
_.. you mean this what you said … You need a reference to "Microsoft Visual Basic for Applications Extenibility 5.3".
You need this for ANYTHING you do with VBA in the VBE
… ??

My question was whether that is true. I am still not sure. I thought you could do some stuff without it..
EDit : I think Hans is saying you can do some stuff without it
( I usually write code with referrences, to get intellisense, then often change them to without if i am sharing )
I am having difficulty logging in with this account just now.
You can find me at DocAElstein also

LisaGreen
5StarLounger
Posts: 964
Joined: 08 Nov 2012, 17:54

Re: To Object or not to object

Post by LisaGreen »

@Alan

From Hans' last post you are clearly correct. You *can* do things in the VBE without having a reference to the Ex. Lib.

We're updating the bok even as I speak!

Lisa

User avatar
Doc.AElstein
BronzeLounger
Posts: 1499
Joined: 28 Feb 2015, 13:11
Location: Hof, Bayern, Germany

Re: To Object or not to object

Post by Doc.AElstein »

LisaGreen wrote:We're updating the bok even as I speak!...
I look forword some time later to a nice Bockbier and another read of your bok :-)
I am having difficulty logging in with this account just now.
You can find me at DocAElstein also

LisaGreen
5StarLounger
Posts: 964
Joined: 08 Nov 2012, 17:54

Re: To Object or not to object

Post by LisaGreen »

LOL!!! You and Hans both get one!!!

User avatar
StuartR
Administrator
Posts: 12609
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: To Object or not to object

Post by StuartR »

HansV wrote:No, you don't. But keep in mind that the code will be more difficult to write, since you can't rely on IntelliSense.
And, of course, the code will be much harder to maintain and update in the future.
StuartR


User avatar
rory
5StarLounger
Posts: 817
Joined: 24 Jan 2010, 15:56

Re: To Object or not to object

Post by rory »

There are, IIRC, some libraries that you might use for HTML parsing that don't expose all the same functionality when late bound but as a general rule there is no difference other than a small performance overhead.
Regards,
Rory

User avatar
Doc.AElstein
BronzeLounger
Posts: 1499
Joined: 28 Feb 2015, 13:11
Location: Hof, Bayern, Germany

Re: To Object or not to object

Post by Doc.AElstein »

rory wrote:some libraries that you might use for HTML parsing that don't expose all the same functionality when late bound.
One of those sort of issues I think I had in scrapping web stuff a few times…, I got some explanations like this https://www.mrexcel.com/forum/excel-que ... ost4031122" onclick="window.open(this.href);return false;
…. ….here late binding has to be used. The htmlfile is an ActiveX object that is a wrapper function for the IHTMLDocument2 interface in MSXML2….. As I recall, it was something to do with "msxml2.xmlhttp" and a .Write and/ or .Send from that thing_…_
_.. after about 200 posts later …_ ( https://www.excelforum.com/excel-progra ... hange.html" onclick="window.open(this.href);return false; ) _.. I gave up trying to understand that… but … one small thing I managed to glean was that .. the "msxml2.xmlhttp" thing was missing something when Early bound.. so Late Bounding had to be used.
You are saying ” some libraries that you might use for HTML parsing that don't expose all the same functionality when late bound….” …. I had been understanding that some sort of extra FunkyNationalities were available with Late Binding that weren’t available with Early Binding. So you saying what appears on the face of it to be the opposite, has helped me get back to full ignorance on that one. . …..so thank you very much for that ;)
( Edit: maybe you mean don't expose the same functionality rather than don't expose all the same functionality
_._____________

In Lisa’s case, a lot of stuff she wants to do in the VBE with VBA won’t work without the reference. Some will. Some wont. It is not just a binding issue I think, if at all. I am not sure.. …….
For example she uses things like vbext_pk_Proc in coding, and trying to do that craps out without the reference to the “..Microsoft Visual Basic for Applications Extensibility 5.3, Class Name: VBIDE ..” thing

_._________________________________________________________________________________________________

_...........

Edit: Mon 7th Jan: I was looking at some old stuff for something I am doing just now… and remembered https://www.excelforum.com/excel-progra ... ost4443658" onclick="window.open(this.href);return false; My previous comments above was talking about a request type part of a typical 2 part thing in a typical scrapping code. That first “request part” has something to do wit a “Microsoft XML Parser (MSXML)” technology that everyone has forgotten what its about but somehow you can “ask” an internet site for something and it gives you back if you’re lucky some crap in a HTML or similar format. The second part is taking this HTML type jumble of stuff and putting it in a thing called a “HTMLdoc” or similar which can sort of look at what is something like a long string of pointy bracket string text and make some sense of it in an Object Orientated Programming sort of a way. Depending on what things from that “HTMLdoc” object you decide to use you may have to use Early Binding , as in this case”…… the htmldocument/file returns a different interface than early binding it. This is somewhat of an oddity, most objects will work either way…. Specifically GetElementsByClassName is not available with late binding, but is with early binding….”

So it seems it can work both ways… and maybe I almost know something about it again . :-)



Ref
‘ Generally for Objects where there is dependency, that is to say....... they cannot exist independently and / or there are other Objects which are affected by the existence of such Objects..... , you will not be allowed to make a New Instance. Instancing by the user directly will not be allowed. This is likely because there will be some very complicated "Wiring" involved. There will need to be information given, for example, as "where should it go", as other Objects may be effected. So those things are best left to a Function or Method, ( a .Add or .Insert , for example ). There will always be arguments associated and require them ( if you ,leave them about, VBA at compile will try to guess ( based on other available information and / or arguments), what you want, or always using a particular default when you omit an argument )

https://www.excelforum.com/excel-progra ... -it-4.html" onclick="window.open(this.href);return false;
https://www.excelforum.com/excel-progra ... ost4384945" onclick="window.open(this.href);return false;
https://www.excelforum.com/excel-progra ... rs-it.html" onclick="window.open(this.href);return false;
Last edited by Doc.AElstein on 03 Mar 2019, 08:36, edited 2 times in total.
I am having difficulty logging in with this account just now.
You can find me at DocAElstein also