Link to home
Start Free TrialLog in
Avatar of David Phelops
David PhelopsFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Using Early binding vs late binding for Outlook objects in Excel VBA

Is there a practical difference in using the two methods below? (Apart from clarity of reading and definition).

    Dim OutApp As Outlook.Application
    Dim OutMail As Outlook.MailItem
    
    Set OutApp = New Outlook.Application
    Set OutMail = OutApp.CreateItem(olMailItem) 

Open in new window

and
    Dim OutApp As Object
    Dim OutMail As Object

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

Open in new window

and  why do you have to have "(0)" after " "CreateItem" in the second example.

Thanks
David
Avatar of [ fanpages ]
[ fanpages ]

Hi again,

I was going to reply here, but I thought I must have answered this at least once before!

[ https://www.experts-exchange.com/questions/22727602/Late-Binding-Formatting-Excel-from-VBA.html?anchorAnswerId=19591174#a19591174 ]
---
...Adopting a "Late Binding" approach ensures that any differences between the library references for the external object you are attempting to create on your development machine & any of your run-time user machines does not cause any issues, because the validation of the object reference will occur at run-time, rather than with "Early Binding" where the validation occurs within the development environment (when the code is interpreted/compiled).

For example, if creating an MS-Outlook application object, the version of Outlook on your development machine may be 2003, but a user may only have version 2000.  If you use "Early Binding" you are restricting to the library reference for Outlook 2003 (on your development machine), as the validation of the libraries is undertaken in your development environment.  When a user with Outlook 2000 tries to run the Excel application they will receive a run-time error as the libraries for Outlook 2003 are not present pn their machine (as MS-Outlook 2003 has not been installed previously) & the application will not be able to be run.  Using "Late Binding" will allow the Outlook 2000 user to proceed as the validation of the libraries will occur as they run the application instead.


FYI: Previous questions I have contributed to on this subject:

"Redistributing VB6 Excel based application"
[ https://www.experts-exchange.com/questions/21565906/Redistributing-VB6-Excel-based-application.html ]

"I need a way to get the name of a workbook without the file extension and it must be very robust"
[ https://www.experts-exchange.com/questions/21760433/I-need-a-way-to-get-the-name-of-a-workbook-without-the-file-extension-and-it-must-be-very-robust.html ]


And a more detailed overview, here:

"Early vs. Late Binding"
[ http://peltiertech.com/Excel/EarlyLateBinding.html ]
---


The (0) refers to the ItemType created:

[ http://msdn.microsoft.com/en-us/library/office/ff869291.aspx ]


olAppointmentItem = 1
olContactItem = 2
olDistributionListItem = 7
olJournalItem = 4
olMailItem = 0
olNoteItem = 5
olPostItem = 6
olTaskItem = 3


If you would like me to expand on my previous answer, please ask.

BFN,

fp.

PS. Using "Early Binding" also allows you to use Microsoft Intellisense.

It is also marginally faster in execution whilst creating objects at run-time compared to "Late Binding".
Avatar of David Phelops

ASKER

Thanks again fp.... All starting to hazily come into focus now.

Re ItemTypes:
I prefer to use a descriptive consonant, if available, rather than a numerical constant.  Makes for much clearer programming as far as I am concerned!

Plenty of varied information for me to get my teeth into.

I tried using your public functions: "Get_Base_Name(ByVal strFileName As String) As String"
I received the following errors when running this sub:

Sub Base_Name()

MsgBox Get_Base_Name(ActiveWorkbook.Name)

End Sub

Open in new window


For Early Binding Example:  "  Dim objScripting_FileSystemObject As Scripting.FileSystemObject"
User generated image

And for the Late Binding Example:  "  Get_Base_Name = objScripting_FileSystemObject.getbasename(strFileName)"
User generated image

Have I missed something obvious, please? Or less obvious perhaps?

Cheers
David
ASKER CERTIFIED SOLUTION
Avatar of [ fanpages ]
[ fanpages ]

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi fp

Excuse the delay.

I have followed your advice and all runs fine, thanks very much.
Starting to become clearer now.
David
No need to apologise.

You are very welcome David.