Using Outlook Object Library in Excel VBA - supporting older versions

cova

Board Regular
Joined
Jun 9, 2010
Messages
84
Hi everyone,
I have been having trouble with libraries in Excel VBA. I need access to some basic Outlook VBA, so have added the Outlook 14.0 Object Library. Only problem then is if that is used on a machine running Office 2007, it does not then reference 12.0 as it does with other libraries. Is there a way to get it to automatically downgrade the version? As otherwise it throws up errors. I think a few people even still use 2003 office, so could do with it working for that too. Is there a way to do this? I tried searching in my 2010 excel libraries but could not find ant 12.0 libraries in there so cant do it that way.
Cheers
Ben
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
You need to late bind the code so that you don't need to set a reference at all - that way you avoid the problem (assuming of course you don't try and use any code that simply won't work on older versions!).
 
Upvote 0
Use late binding rather than early binding.

Instead of
Code:
Dim oL as Outlook.Application

use
Code:
Dim oL as Object
Set oL = CreateObject("OutLook.Application")

You will lose intellisense and it will be ever so slightly slower in operation but that is the only (or the easiest) way to run different versions of Outlook. See Early binding vs Late binding in Office VBA | msofficefun for more info.
 
Last edited by a moderator:
Upvote 0
I also use Outlook.ContactItem in my code. Would I just need to set it so that it was
Code:
Dim olitem as Object
Set olitem = CreateObject("Outlook.ContactItem")
as right now it is just
Code:
Dim olitem as Outlook.ContactItem
(not really had to deal with early/late binding too much as generally stuck to the main libraries!)
 
Upvote 0
No, you only use CreateObject where you would use
Code:
Set ... = New ...
in your early bound code. For a contactitem you would still use the CreateItem method of the Outlook application but because you do not have the reference set, you need to declare the olContactItem constant:
Code:
Const olContactItem as Long = 2

then you can use:
Code:
set olItem = appOL.CreateItem(olcontactitem)
 
Upvote 0
OK. One more thing that seems to have broken is I have the following code:
Code:
Set runoutlook = CreateObject("Outlook.Application")Set findnamespace = runoutlook.GetNamespace("MAPI")
Set activefolder = findnamespace.Folders
n = 1
Do Until activefolder.Item(n) = mailfoldername
n = n + 1
Loop
as it runs on a secondary mailbox on each users machine (mailfoldername is defined earlier and checks the office version as Office 2010 and above displays it as user@domain.com but below it is "Mailbox - Username")

Would I need to create an item for "Item" in this instance?

EDIT: Never mind, it was my referencing of mailfoldername that was causing the issue
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,635
Messages
6,120,660
Members
448,975
Latest member
sweeberry

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top