VBA not Recognizing MAPI

两盒软妹~` 提交于 2020-01-25 20:57:43

问题


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:


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")
...


来源:https://stackoverflow.com/questions/30248748/vba-not-recognizing-mapi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!