VBA not Recognizing MAPI

2019-09-08 08:48发布

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):

  • Outlook 14.0 Object Library,
  • Outlook SharePoint Social Provider,
  • Outlook Social Provider Extensibility,
  • Outlook View Control

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

1条回答
Anthone
2楼-- · 2019-09-08 09:07

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")
...
查看更多
登录 后发表回答