Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-11-18
5
Medium Priority
?
369 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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 use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

927 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