New2VBA
New2VBA

Reputation: 347

VBA not Recognizing MAPI

I'm using VBA to enter into an outlook folder and put the message body into a cell. However on

 set ns = getnamespace("MAPI")

I get an error "automation error library not registered". I have the following outlook related references selected (within Excel):

I'm using Excel 2010. The entire code follows. Any help would be greatly appreciated.

Dim ns As Namespace
Dim inbox As Mapifolder
Dim item As Object
Dim atmt As Attachment
Dim FileName As String
Dim i As Integer
Dim SubFolder As Mapifolder
Dim SubSubFolder As Mapifolder
Dim VariableName As Name
Dim Working As Workbook
Dim Sheet As Worksheet


Set ns = getnamespace("MAPI")
Set inbox = ns.GetDefaultFolder(olFolderInbox)
Set SubFolder = inbox.Folders("xx") 
Set SubSubFolder = SubFolder.Folders("xxxx")
Set Working = ThisWorkbook
Set Sheet = Working.Worksheets("Sheet1")

If SubSubFolder.Items.Count > 0 Then

 For Each item In SubSubFolder.Items
Sheet.Range("A1") = item.Body
Next item

End If

Upvotes: 1

Views: 10702

Answers (1)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66245

Namespace() will be valid in Outlook VBA since there is an intrinsic Application variable that points to the Outlook.Application object and the variable is global. i.e. all of its properties and methods are available without specifying "Application.*"

In case of Excel VBA, Application points to an instance of the Excel.Application object, so you must explicitly create and initialize the Outlook.Application object:

set olApp = CreateObject("Outlook.Application")
Set ns = olApp.getnamespace("MAPI")
...

Upvotes: 2

Related Questions