I am trying to use the folder path to navigate to a folder.
The reason is the default inbox in Outlook is not the inbox I need.
I know the path of the relevant inbox by right clicking on it, hitting properties, and looking at location.
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")
This returns
runtime error 91, Object variable or With block variable not set.
I am using this function as is from:
http://www.outlookcode.com/d/code/getfolder.htm
Note: Broken link.
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
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...