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

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Forum statistics

Threads
1,214,601
Messages
6,120,462
Members
448,965
Latest member
grijken

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