Get the MAPI folder in Outlook from the folder

I am trying to use this function on this page: http://www.outlookcode.com/d/code/getfolder.htm to use the folder path to navigate to the folder. (I will copy this code at the bottom of this question - I used it as it was, not modified at all.) The reason I need to use this is because the default mailbox in Outlook does not match the inbox that I need to be active. I know the path to the corresponding mailbox by right-clicking on it, and hit the properties, and looked at the location.

This is the code I'm using:

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 a 91 runtime error, an object variable, or with a locked block variable.

I have no idea what that means. If you could help me solve this error, that would be awesome, and if you had a way so that I could completely avoid this problem, it would be awesome too. 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 
+4
source share
3 answers

I have found the answer. It turns out that something is stupid, as usual :)

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

should be

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

. This fixes the problem. I decided that I would leave it here, if anyone else has this problem, the solution is simply to follow this format ...

+8
source

Just add this line ...

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

+2
source

Apparently this line is also needed:

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

But after the previous line.

So in the context of:

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

Source: https://habr.com/ru/post/1485653/


All Articles