Solved

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

Posted on 2013-11-18
5
305 Views
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

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
0
Comment
Question by:David Phelops
  • 3
  • 2
5 Comments
 
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.

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

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?

Cheers
David
0
 
LVL 35

Accepted Solution

by:
[ fanpages ] earned 500 total points
ID: 39662171
Hi,

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?

Thanks.

BFN,

fp.
0
 

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.
David
0
 
LVL 35

Expert Comment

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

You are very welcome David.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

758 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now