VBA无法识别MAPI

时间:2015-05-14 22:49:33

标签: excel vba excel-vba outlook outlook-vba

我正在使用VBA进入outlook文件夹并将邮件正文放入单元格中。但是

 set ns = getnamespace("MAPI")

我收到错误“未注册自动化错误库”。我选择了以下与Outlook相关的参考文献(在Excel中):

  • Outlook 14.0对象库,
  • Outlook SharePoint社交提供程序,
  • Outlook社交提供程序可扩展性,
  • Outlook View Control

我正在使用Excel 2010.整个代码如下。任何帮助将不胜感激。

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 个答案:

答案 0 :(得分:2)

Namespace()在Outlook VBA中有效,因为有一个内部Application变量指向Outlook.Application对象,变量是全局变量。即,所有属性和方法都可用,而无需指定"应用程序。*"

对于Excel VBA,Application指向Excel.Application对象的实例,因此您必须显式创建并初始化Outlook.Application对象:

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