ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   VBA reference to Outlook (https://www.excelbanter.com/excel-programming/286672-vba-reference-outlook.html)

Torbjörn Steijer[_2_]

VBA reference to Outlook
 
Hi,

I picked the code below from a very good site (can't find it again). For
those who have higher version than 9.0 this doesn't work. Is there any other
way to write to code so that it adds the reference depending on what version
you have?

If anybody know where I might have picked up the code, please let me know.

Best regards
Torbjörn

'All this code only works if Excel has a reference to the Outlook library
'the sub below ensures we do
Public Sub CheckReferences()

Dim ref As Object
Static blnRefFound As Boolean
If blnRefFound = True Then Exit Sub

'do we have a reference already? if so, exit
For Each ref In Application.VBE.ActiveVBProject.References
If ref.Name = "Outlook" Then
blnRefFound = True
Exit Sub
End If
Next ref

'no reference? add it
If blnRefFound = False Then
'if you get an error below, the Outlook library is somewhere else. Search
for msoutl9.olb on your C drive, and
'when you find it, change the pathname on the next line and try again
Application.VBE.ActiveVBProject.References.AddFrom File
"C:\Program\Microsoft Office\Office\msoutl9.olb"
End If

End Sub



Colo[_55_]

VBA reference to Outlook
 
Hello,
Since only I have Outlook XP then I could not test this code, but pls
try something like this.


Code:
--------------------

With Application.VBE.ActiveVBProject.References
On Error Resume Next
.AddFromFile Filename:="msoutl9.olb"
If Err Then .AddFromFile Filename:="msoutl.olb"
On Error GoTo 0
End With

--------------------



---
Message posted from http://www.ExcelForum.com/


Daniel Klann

VBA reference to Outlook
 
Hi Torbjörn,

You can get around version problems by using the the GUID (Globally Unique
IDentifier) for the Outlook type library. The GUID is the same for all
versions of Outlook, although I can only confirm that this code works with
Excel and Outlook 2003.

Change this line:-

Application.VBE.ActiveVBProject.References.AddFrom File _
"C:\Program\Microsoft Office\Office\msoutl9.olb"

to this:-

Application.VBE.ActiveVBProject.References.AddFrom Guid _
"{00062FFF-0000-0000-C000-000000000046}", 0, 0

Regards,
Dan

-----------------------------------------
Website : http://www.danielklann.com
Favourites Add-in for Microsoft Excel -
http://www.danielklann.com/excel/exc...tes_add-in.htm



"Torbjörn Steijer" <torbjorn.steijer@ wrote in message
...
Hi,

I picked the code below from a very good site (can't find it again). For
those who have higher version than 9.0 this doesn't work. Is there any

other
way to write to code so that it adds the reference depending on what

version
you have?

If anybody know where I might have picked up the code, please let me know.

Best regards
Torbjörn

'All this code only works if Excel has a reference to the Outlook library
'the sub below ensures we do
Public Sub CheckReferences()

Dim ref As Object
Static blnRefFound As Boolean
If blnRefFound = True Then Exit Sub

'do we have a reference already? if so, exit
For Each ref In Application.VBE.ActiveVBProject.References
If ref.Name = "Outlook" Then
blnRefFound = True
Exit Sub
End If
Next ref

'no reference? add it
If blnRefFound = False Then
'if you get an error below, the Outlook library is somewhere else.

Search
for msoutl9.olb on your C drive, and
'when you find it, change the pathname on the next line and try again
Application.VBE.ActiveVBProject.References.AddFrom File
"C:\Program\Microsoft Office\Office\msoutl9.olb"
End If

End Sub





Thor[_2_]

VBA reference to Outlook
 

A good suggestion that took me one step further but I ran in to some
other problems when I used the application at work. I get the following
error message;

'Programmatic access to Visual Basic Project not trusted'

when it came to the 'With Application...'-line.

I really would like to add the reference without having to guide the
users in the VB menues. Some users aren't really up to that challenge!

(I have not tried the general reference that Dan suggested yet).

Best regards,

Thor



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Colo[_77_]

VBA reference to Outlook
 
In XP, we have to change the security setting.
From main tool bar, Tools Macro security.
At the second tab, press Alt + V key.
Sorry I don't have an English version, but the caption would be
something like this..(Trust access to a VBA project

--
Message posted from http://www.ExcelForum.com



All times are GMT +1. The time now is 12:01 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com