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

Posted on 2013-11-18
Medium Priority
Last Modified: 2013-11-27
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

    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.

Question by:David Phelops
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 35

Expert Comment

by:[ fanpages ]
ID: 39656282
Hi again,

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

[ http://www.experts-exchange.com/Microsoft/Development/MS_Access/Access_Coding-Macros/Q_22727602.html#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"
[ http://www.experts-exchange.com/Applications/MS_Office/Excel/Q_21565906.html ]

"I need a way to get the name of a workbook without the file extension and it must be very robust"
[ http://www.experts-exchange.com/Applications/MS_Office/Excel/Q_21760433.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.



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".

Author Comment

by:David Phelops
ID: 39656599
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"
Error Message

And for the Late Binding Example:  "  Get_Base_Name = objScripting_FileSystemObject.getbasename(strFileName)"
error message

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

LVL 35

Accepted Solution

[ fanpages ] earned 2000 total points
ID: 39662171

The "Early Binding" version of the code (shown below) will require the addition of a Reference within the Visual Basic environment; "Microsoft Scripting Runtime".

Public Function Get_Base_Name(ByVal strFileName As String) As String

  On Error Resume Next
  Dim objScripting_FileSystemObject                     As Scripting.FileSystemObject
  Set objScripting_FileSystemObject = New Scripting.FileSystemObject
  Get_Base_Name = objScripting_FileSystemObject.GetBaseName(strFileName)
  Set objScripting_FileSystemObject = Nothing

End Function

Open in new window

The "Late Binding" variant, seems to be OK for me:

Public Function Get_Base_Name(ByVal strFileName As String) As String

  On Error Resume Next
  Dim objScripting_FileSystemObject                     As Object
  Set objScripting_FileSystemObject = CreateObject("Scripting.FileSystemObject")
  Get_Base_Name = objScripting_FileSystemObject.GetBaseName(strFileName)
  Set objScripting_FileSystemObject = Nothing

End Function

Open in new window

Note that "GetBaseName" is in MixedCase, whereas your code snippet shows lowercase ("getbasename").

Please can you try copying the above routine & pasting into a new workbook to try it again?




Author Closing Comment

by:David Phelops
ID: 39674730
Hi fp

Excuse the delay.

I have followed your advice and all runs fine, thanks very much.
Starting to become clearer now.
LVL 35

Expert Comment

by:[ fanpages ]
ID: 39682197
No need to apologise.

You are very welcome David.

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question