Example VBIDE Microsoft Visual Basic for Applications Extensibility 5.3 For coding coding


Based on notes here http://www.excelfox.com/forum/showth...r-code-modules and a discussion here http://www.eileenslounge.com/viewtopic.php?f=30&t=31547

This is a good example to show the inexactness and sometimes uncertainty amongst even experts in the theme of Binding…
_a) Unlike often seen in literature, we can in fact do things in the VB Editor without a reference to this Library "Microsoft Visual Basic for Applications Extensibility 5.3" , ( Class Name: VBIDE ). It is also not at all clear at all if strictly speaking we can do Early Binding in this case.

_b) Also I am not convinced that we can do Late binding either. In a very indirectly way we could argue that somebody once maybe did a reference to "Microsoft Visual Basic for Applications Extensibility 5.3" to get the things there in front of us..

Before we start, lets look at the Laterly Early Binding Technique , as this appears to be most useful to have to hand.
At the same time we will remind ourselves that doing anything to references empties global variables because it resets the VBA project..
Here is a set of fairly self explanatory demo routines. All routines should be run sequentially in the order given. They attempt to remove and add the reference to VBIDE , "Microsoft Visual Basic for Applications Extensibility 5.3"
At he end we have the reference set to VBIDE , "Microsoft Visual Basic for Applications Extensibility 5.3"
Code:
Option Explicit
Dim StringyGloby As String
Sub FillMeGloby()
 Let StringyGloby = "Hi, I am here"
End Sub
Sub WotsMeGloby()
 MsgBox Prompt:=StringyGloby
End Sub
Sub TakeItOff()
Dim objThisVBAProject As Object: Set objThisVBAProject = Application.VBE.ActiveVBProject
Dim MyToolBoxRefutations As Object: Set MyToolBoxRefutations = objThisVBAProject.References
Dim Tool As Object, TrashtVBIDE As Boolean: Let TrashtVBIDE = False
    For Each Tool In MyToolBoxRefutations
    Dim ToolGUID As String
     Let ToolGUID = Tool.GUID
        If ToolGUID = "{0002E157-0000-0000-C000-000000000046}" Then
         MyToolBoxRefutations.Remove Tool ' Remove reference for  "Microsoft Visual Basic for Applications Extensibility 5.3"
         MsgBox Prompt:="You will find that your globies are empty because you took off the referrence and in doing so reset the VBA project at the end of this procedure"
         Let TrashtVBIDE = True
        Else
        End If
    Next Tool
    If TrashtVBIDE = False Then MsgBox Prompt:="You did not remove the referrence, so presumably it was not checked, and so at the end of this procedure your globies should be intact"
End Sub
Sub WotsMeGloby2andPhilIt()
 MsgBox Prompt:=StringyGloby
 Let StringyGloby = "Hi, I am here"
End Sub
Sub LaterlyEarlyBinding()
' Add the referrence to  Microsoft Visual Basic for Applications Extensibility 5.3  VBIDE   if it is not already there.  Note: This will take into efffect when this procedure ends
   With ThisWorkbook.VBProject.References
    On Error Resume Next ' Just in case you already have it checked, as the next code line would error if yoou had it checked
     .AddFromguid GUID:="{0002E157-0000-0000-C000-000000000046}", Major:=5, Minor:=3 '
        If Err.Description = "" Then ' this section must go before On Error GoTo 0 because On Error GotTo 0 will empty  Err  registers
         MsgBox Prompt:="You have added a referrence so at the end of this procedure the VBA project will be reset and your globies will be empty"
        Else
         MsgBox Prompt:="You did not add the referrenc so presumably it was checked already and your globies should be intact"
        End If
    On Error GoTo 0
   End With
End Sub
Sub WotsMeGloby3andPhilIt()
 MsgBox Prompt:=StringyGloby
 Let StringyGloby = "Hi, I am here"
End Sub
Running those procedures should get us at the start point of looking at Binding issues.
The following procedures address these issues
As far as I can tell,
the following is sometimes regarded as Early Binding
Code:
Sub EarlyBirly()' Early Binding to Microsoft Visual Basic for Applications Extensibility 5.3  VBIDE
Dim vbcmlCodeModule As VBIDE.CodeModule
 ' Set vbcmlCodeModule = New VBIDE.CodeModule ' I am not allowed to do this
 Set vbcmlCodeModule = Application.VBE.ActiveVBProject.VBComponents.VBE.ActiveCodePane.CodeModule
 MsgBox Prompt:=vbcmlCodeModule.CountOfLines ' Count of number of used lines in this code module
 MsgBox Prompt:=VBIDE.vbext_pk_Proc
End Sub
The last Message box uses the one of only things that I have, ( apart from Intellisense ), which I have as extra using Early Binding: I can use some constants which are predefined in the library, instead of having to use the constants you use their literal values instead. ( In this case the literal value is that identifying a procedure type of the type normal Sub or Function

I am not allowed to use New. This has been "decided by Microsoft". The Instantiating has effectively been done when the VBA Project was created.

As far as I can tell, apart from the use of the named constants, I can do all without this Early Binding that I can with Early Binding.
( Another advantage that I have if I do it this way , is that typing mistakes in things from that library will be noticed at compile, as apposed to at runtime if I do not have the reference checked )

We should note finally that we are prevented ( by Microsoft ) in using New in the Set code line. Effectively The Set = New __ code line has been done for us. We are then only permitted to assign a variable to that of the VBIDE object. As we are using a VBIDE object type in the Dim, it is sometimes regarded as Early Binding.
It is typical of objects which are dependant on others and cannot exist alone , that we are prevented by Microsoft from creating new instances of them.

( Not ) Late Binding in VB Editor
There is no clear definition or statement as to whether there is such a thing as Late Binding to the Microsoft Visual Basic for Applications Extensibility 5.3 VBIDE
We are prevented ( by Microsoft ) of either using New or using CreateObject("…. .. "). It is typical of objects which are dependant on others and cannot exist alone , that we are prevented by Microsoft from creating new instances of them.
It appears that we can do anything to the VB Editor without Early Binding, ( that is to say without a reference to the Microsoft Visual Basic for Applications Extensibility 5.3 VBIDE ) that we can with early Binding.
If you wish to , you can say that the following routine is using Late Binding. It is up to debate. You could explain it as Late Binding by saying it is Late Binding because it is making use of an object without the use of a reference to the appropriate Library.
An alternative explanation of the situation would simply to say that we are referring to an existing object. As such, Binding issues are no longer relevant..
Code:
Sub LateNotEarly() ' http://www.eileenslounge.com/viewtopic.php?f=30&t=31547&p=246518#p246518
 MsgBox Prompt:=Application.VBE.ActiveVBProject.VBComponents.VBE.ActiveCodePane.CodeModule.CountOfLines ' Count of number of used lines in this code module
Dim vbcmlCodeModule As Object
 Set vbcmlCodeModule = Application.VBE.ActiveVBProject.VBComponents.VBE.ActiveCodePane.CodeModule
 MsgBox Prompt:=vbcmlCodeModule.CountOfLines ' Count of number of used lines in this code module
 'MsgBox Prompt:=VBIDE.vbext_pk_Proc ' I cannot do this with .. this...not Early Binding
End Sub
Understanding automation ,: Error 429 Object creation by ActiveX component not possible
Attempting to use New or CreateObject( .. .. ) or ( even GetObject( .. ) ) will fail, typically with the shown error _ 429 Object creation by ActiveX component not possible.
The technical terminology is that their "Automation Object Linking and Embedding, or Automation OLE or Automation" is not "exposed". In other words an object from the Class of this thing which you are trying to assign to a variable is not made available to be included in another thing.
This possibly explains why it is difficult to give a precise definition of Late Binding in some cases.
The ability to do almost anything is windows is attributed to optimisation of memory to allow for runtime getting of things. Clearly there are conflicts here in any attempt to make a rational explanation of what is going on.
Hear is one attempt ( http://eileenslounge.com/viewtopic.p...art=40#p246586 )
Summary is also in the next Post: ……._





Ref:
' https://www.excelforum.com/excel-pro...fers-it-4.html
' https://www.excelforum.com/excel-pro...ml#post4384945
' https://www.excelforum.com/excel-pro...offers-it.html
' 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 )
' Book: VBA for the VBE, Lisa Green. thinkz1.com
' http://www.eileenslounge.com/viewtop...246518#p246518