在Excel VBA中设置对Outlook 365文件夹的引用

时间:2019-03-14 17:21:07

标签: excel vba outlook outlook-vba

我的公司正在从Outlook 16迁移到Outlook 365,现在下面的Excel VBA脚本需要引用Outlook 365中的邮箱。

在线错误

Set oMapi = oApp.GetNamespace("MAPI").Folders(strMail).Folders("inbox")

错误

  

尝试的操作失败。找不到对象。

是否可能需要以其他方式添加邮箱?还是有其他方法可以在Outlook 365中执行此功能?

对于使用Outlook 365进行此操作,我没有看到很多不同的方式。

Sub Import_Email_Preferences()
    Const strMail As String = "borrowerservicesshiftbid@glhec.org"
    Dim oApp As Outlook.Application
    Dim oMapi As Outlook.MAPIFolder
    Dim oMail As Outlook.MailItem
    Dim strEmailAddress As String
    Dim strSenderName As String
    Dim strSubject As String
    Dim intRow As Integer

    Dim i As Long
    Dim tbl As ListObject
    Dim ltblRow As Long
    Set tbl = ThisWorkbook.Worksheets("Preferences").ListObjects(1)
    ltblRow = tbl.DataBodyRange.Rows.Count

    On Error Resume Next
    Set oApp = GetObject(, "OUTLOOK.APPLICATION")
        If (oApp Is Nothing) Then Set oApp = CreateObject("OUTLOOK.APPLICATION")
    On Error GoTo 0

    'Getting Error Here
    Set oMapi = oApp.GetNamespace("MAPI").Folders(strMail).Folders("inbox")

    For i = oMapi.Items.Count To 1 Step -1
        Set oMail = oMapi.Items(i)

        If TypeOf oMail Is Outlook.MailItem Then
            MsgBox = "Blue"
        End If
    Next i

    Set oApp = Nothing
    Set oMapi = Nothing
    Set oMail = Nothing
    Set oHTML = Nothing
    Set oElColl = Nothing
End Sub

2 个答案:

答案 0 :(得分:0)

请尝试通过GetDefaultFolder引用收件箱:

On Error Resume Next
If oApp Is Nothing Then
    Set oApp = GetObject(, "Outlook.Application")
    If oApp Is Nothing Then
        Set oApp = New Outlook.Application
    End If
End If
On Error GoTo 0

Set oMapi = oApp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)

如果无法访问默认文件夹,请注意,文件夹名称取决于语言。 G。 Namespace.Folders("Posteingang")用德语。

答案 1 :(得分:0)

Set oMapi = oApp.GetNamespace("MAPI").Folders(strMail).Folders("inbox")的麻烦在于您将引用串联在一起。如果可以,这很好,但是如果不成功,您不知道是什么原因。

如果Set oMapi = oApp.GetNamespace("MAPI")不是对象,

oApp将失败。

如果Set oMapi = oApp.GetNamespace("MAPI").Folders(strMail)不是对象,

oApp.GetNamespace("MAPI")将失败。

如果Set oMapi = oApp.GetNamespace("MAPI").Folders(strMail).Folders("inbox")不是对象,

oApp.GetNamespace("MAPI").Folders(strMail)将失败。

我建议尝试使用下面的子例程来确定哪个步骤失败了。该子例程可一步完成所有操作。它还尝试了实现相同效果的不同方法。如果一个Set失败,请将其注释掉,看看下一个是否有效。

我已经提出了一些可能的故障原因,但是我相信还有其他可能性。知道失败的步骤后,如果需要进一步的帮助,请回来。

您也可以尝试使用GetDefaultFolder的Asger建议。它在我的系统上不起作用,因为未使用默认的收件箱。我必须命名包含要访问的收件箱的商店。

Sub TestGetInbox()

  Dim olApp As Outlook.Application
  Dim olInbox1 As Outlook.MAPIFolder
  Dim olInbox2 As Outlook.Folder
  Dim olInbox3 As Outlook.MAPIFolder
  Dim olInbox4 As Outlook.Folder
  Dim olNs As Outlook.Namespace
  Dim olSession As Outlook.Namespace
  Dim olStore1 As Outlook.Folder
  Dim olStore2 As Outlook.Folder

  ' If execution stops her, you have a problem accessing Outlook
  Set olApp = CreateObject("Outlook.Application")

  ' If execution stops here, you have a problem accessing Outlook's namespace
  ' using this method. Comment out statements down to  "Set olSession = olApp.Session"
  Set olNs = olApp.GetNamespace("MAPI")

  ' If execution stops here, look at your folder pane.  Is "borrowerservicesshiftbid@glhec.org"
  ' the name of a store? Case does not seem to matter but a single letter change in the name
  ' does.
  Set olStore1 = olNs.Folders("borrowerservicesshiftbid@glhec.org")
  Debug.Print olStore1.Name

  Set olInbox1 = olStore1.Folders("Inbox")
  Debug.Print olInbox1.Name

  Set olInbox2 = olStore1.Folders("Inbox")
  Debug.Print olInbox1.Name

  ' If execution stops her, you have a problem accessing Outlook's namespace
  ' This is a different methods of accessing Outlook's namespace.  The documentation
  ' says the two method are identical but I once had the olApp.Namespace("MAPI")
  ' fail so I now always use the olApp.Session method which has never failed for me.
  Set olSession = olApp.Session

  Set olStore2 = olSession.Folders("borrowerservicesshiftbid@glhec.org")
  Debug.Print olStore2.Name

  Set olInbox3 = olStore1.Folders("Inbox")
  Debug.Print olInbox1.Name

  Set olInbox4 = olStore1.Folders("Inbox")
  Debug.Print olInbox1.Name

  Set olInbox1 = Nothing
  Set olInbox2 = Nothing
  Set olStore1 = Nothing
  Set olNs = Nothing

  Set olInbox3 = Nothing
  Set olInbox4 = Nothing
  Set olStore2 = Nothing
  Set olSession = Nothing

  Set olApp = Nothing

End Sub