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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17049546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 15:50:32  来源:igfitidea点击:

Get MAPI Folder in Outlook from Folder Path

vbaoutlookmapi

提问by Wolves

I am trying to use the function from on this page: http://www.outlookcode.com/d/code/getfolder.htmto use the folder path to navigate to a folder. (I will copy that code onto the bottom of this question--I used it as-is, unmodified at all.) The reason I need to use this is that the default inbox in Outlook is not the same as the inbox I need to be active. I know the path of the relevant inbox by right clicking on it and hit properties, and looking at location.

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

This is the code I use:

这是我使用的代码:

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

This returns runtime error 91, Object variable or With block variable not set.

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

I have no idea what this means. If you could help me solve this error, that would be awesome, and if you have a way that I could avoid this problem entirely, that would be awesome also. Thanks!

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

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

回答by Wolves

I found the answer. Turns out it's something stupid, as per usual :)

我找到了答案。事实证明这是愚蠢的,像往常一样:)

Set Inbox = GetFolder("\[email protected]\inbox")

needs to be

需要是

Set Inbox = GetFolder("[email protected]/inbox")

. This fixes the problem. I figured I would leave this here in case anyone else has this problem, the solution is simply to follow the given format...

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

回答by user2803771

Just add this line...

只需添加这一行...

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

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

回答by Bob

Apparently also needs this line:

显然还需要这一行:

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

But afterthe previous line.

但是上一行之后。

So, in context:

因此,在上下文中:

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