Solved

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

Posted on 2013-11-18
5
349 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
[X]
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
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
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…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

752 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