从文件夹路径获取Outlook中的MAPI文件夹

时间:2013-06-11 16:48:57

标签: vba outlook mapi

我正在尝试使用此页面上的功能:http://www.outlookcode.com/d/code/getfolder.htm使用文件夹路径导航到文件夹。 (我会把这个代码复制到这个问题的底部 - 我原样使用它,完全没有修改。)我需要使用它的原因是Outlook中的默认收件箱与我需要的收件箱不同积极点。通过右键单击并点击属性并查看位置,我知道相关收件箱的路径。

这是我使用的代码:

Set objOutlook = CreateObject("Outlook.Application", "localhost")
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set Inbox = GetFolder("\\itadmin@email.org\inbox")
Debug.Print Inbox '<-- This fails
Set InboxItems = Inbox.Items '<-- This also fails
InboxItems.SetColumns ("SentOn")

这将返回运行时错误91,对象变量或未设置块变量。

我不知道这意味着什么。如果你可以帮我解决这个错误,那就太棒了,如果你有办法让我完全避免这个问题,那也会很棒。谢谢!

Public Function GetFolder(strFolderPath As String)As MAPIFolder
  ' strFolderPath needs to be something like 
  '   "Public Folders\All Public Folders\Company\Sales" or
  '   "Personal Folders\Inbox\My Folder"

  Dim objApp As Outlook.Application
  Dim objNS As Outlook.NameSpace
  Dim colFolders As Outlook.Folders
  Dim objFolder As Outlook.MAPIFolder
  Dim arrFolders() As String
  Dim I As Long
  On Error Resume Next

  strFolderPath = Replace(strFolderPath, "/", "\")
  arrFolders() = Split(strFolderPath, "\")
  Set objApp = Application
  Set objNS = objApp.GetNamespace("MAPI")
  Set objFolder = objNS.Folders.Item(arrFolders(0))
  If Not objFolder Is Nothing Then
    For I = 1 To UBound(arrFolders)
      Set colFolders = objFolder.Folders
      Set objFolder = Nothing
      Set objFolder = colFolders.Item(arrFolders(I))
      If objFolder Is Nothing Then
        Exit For
      End If
    Next
  End If

  Set GetFolder = objFolder
  Set colFolders = Nothing
  Set objNS = Nothing
  Set objApp = Nothing
End Function

3 个答案:

答案 0 :(得分:8)

我找到了答案。结果证明这是愚蠢的,按照惯例:)

Set Inbox = GetFolder("\\itadmin@email.org\inbox")

需要

Set Inbox = GetFolder("itadmin@email.org/inbox")

。这解决了这个问题。我想我会留下这个,以防其他人有这个问题,解决方案只是遵循给定的格式......

答案 1 :(得分:2)

只需添加此行......

  

strFolderPath = Replace(strFolderPath,“\\”,“”)

答案 2 :(得分:0)

显然也需要这一行:

strFolderPath = Replace(strFolderPath, "\", "/")

上一行之后。

所以,在上下文中:

strFolderPath = Replace(strFolderPath, "\\", "")
strFolderPath = Replace(strFolderPath, "\", "/")