5

我正在尝试使用此页面上的功能: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
4

3 回答 3

8

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

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

needs to be

Set Inbox = GetFolder("itadmin@email.org/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...

于 2013-06-11T17:36:28.820 回答
2

只需添加这一行...

strFolderPath = 替换(strFolderPath,“\\”,“”)

于 2013-09-22T08:08:27.790 回答
1

显然也需要这一行:

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

但是上一行之后。

因此,在上下文中:

strFolderPath = Replace(strFolderPath, "\\", "")
strFolderPath = Replace(strFolderPath, "\", "/")
于 2017-01-01T02:24:51.813 回答