Code to load reference / Object library automatically for office 2007 & 2010 versions

ramanicwa

New Member
Joined
Nov 10, 2013
Messages
1
Hi,


I'm working on an automation that will connect ms excel and outlook.


I want a code to work on both office 2007 & 2010 versions.


I'm getting a compile error "Can't find project or library" when i used a code taken from the below thread.


VBA References


I've given the code below which i used. Kindly help to overcome this issue as this an urgent assignment to be completed.

Kindly help me on this issue.

Thanks in advance.


Code:
Private Sub Workbook_Open()
Call OutlookLibrary("ADD")


Sub OutlookLibrary(action As String)
Dim olPath As String, ObjLib As String, olShell As String, olVersion As String
Dim WSH As Object
Set WSH = CreateObject("WScript.Shell")
olShell = "HKCR\CLSID\{00020D75-0000-0000-C000-000000000046}"
olPath = WSH.RegRead(olShell & "\Shell\Open\Command\")
olPath = Mid(olPath, 2, Len(olPath) - 13)
olVersion = WSH.RegRead("HKCR\Outlook.Application\")
Select Case olVersion
    Case "Microsoft Outlook 12.0 Object Library"
        ObjLib = "msoutl.olb"
    Case "Microsoft Outlook 14.0 Object Library"
        ObjLib = "msoutl.olb"
End Select
On Error Resume Next
If UCase(action) = "ADD" Then
    Application.VBE.ActiveVBProject.References.AddFromFile olPath & ObjLib
Else
    Application.VBE.ActiveVBProject.References.Remove Application.VBE.ActiveVBProject.References("Outlook")
End If
Set WSH = Nothing
End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

Forum statistics

Threads
1,216,076
Messages
6,128,670
Members
449,463
Latest member
Jojomen56

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