Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get MAPI Folder in Outlook from Folder Path

Tags:

vba

outlook

mapi

I am trying to use the function from on this page: http://www.outlookcode.com/d/code/getfolder.htm to 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.

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.

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
like image 912
Wolves Avatar asked Jun 11 '13 16:06

Wolves


People also ask

How do I find the path of a folder in Outlook?

Press the Ctrl + Shift + F keys simultaneously to open the Advanced Find dialog box, and then click the Browse button in the dialog box. Now you will get the exact folder path of the currently opening search result in the Select Folder(s) dialog box.

How do I copy a folder path into an email?

Right-click on the file/folder and drag it (with the right mouse button still pressed) to the location in the email where you want to insert the path.

What is MAPI folder?

MAPIFolder. View. Represents a Microsoft Outlook folder. A MAPIFolder object can contain other MAPIFolder objects, as well as Outlook items.

How do I find the path of an email in Outlook 2010?

Open (double-click) a message you know is in the folder. Press Ctrl-Shift-F to open Advanced Find. Click the Browse button to reveal the full path to the e-mail.


1 Answers

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...

like image 93
Wolves Avatar answered Oct 06 '22 21:10

Wolves