Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 166
  • Last Modified:

How to

I have a VBA running in outlook 2010 where it asks for a mailbox (to search from).

This code was originally set up so that the user can select ANY mailbox to start searching in, however, I only need it to always start in the normal "Inbox".

I have tried everything, but I can't seem to get it.  It presents me with a dialog box (which I will always just pick the Inbox) and the code proceeds without any problems.

  'Set the start folder under which it will search
  '*********I don't know how to set it automatically to INBOX
  Set F = Application.Session.PickFolder

If Not F Is Nothing Then
    Set Folders = F.Folders
    DaFolder = Mid(F.FolderPath, 3)
.
.
.
.

Open in new window


What can I change the line
Set F = Application.Session.PickFolder
with so that it has automatically assigned the "Inbox" to F?
0
MoeMoe7
Asked:
MoeMoe7
  • 4
  • 3
  • 2
  • +1
1 Solution
 
MacroShadowCommented:
Try this:
Dim f As MAPIFolder
Dim olns As Outlook.NameSpace
Set olns = Outlook.GetNamespace("MAPI")
Set f = olns.Folders("Mailbox - Name, Name")
Set f = f.Folders("Inbox")

Open in new window

0
 
Dany BalianCTOCommented:
modify your code to the following: (for only one time run, select the inbox, and note the values returned)

'Set the start folder under which it will search
'*********I don't know how to set it automatically to INBOX
Set F = Application.Session.PickFolder
Dim myEntryID As String, myStoreID As String
myEntryID = F.EntryID 
myStoreID = F.StoreID
msgbox "myentryid="  & myentryid & vbcrlf & "mystoreid=" & mystoreid

Open in new window


then modify your code to the following:
Set F = Application.Session.GetFolderFromID(myEntryID, myStoreID) 'use the values that you got from step 1

Open in new window


hope this helps..
0
 
MoeMoe7Author Commented:
MacroShadow,

This is what I did:  (it did not work)

Private F As Outlook.MAPIFolder
Private Folders As Outlook.Folders
Private DaFolder As String
Private DaFolder2 As String
Private olns As Outlook.NameSpace
.
.
.
  'Set the start folder underwhich it will search
  '*********I don't know how to set it automatically to INBOX
    Set olns = Outlook.GetNamespace("MAPI")
    Set F = olns.Folders("Mailbox - Name, Name")
    Set F = F.Folders("Inbox")
    'Set F = Application.Session.PickFolder

Open in new window


It fails at
Set F = olns.Folders("Mailbox - Name, Name")

I dimensioned them like that because that is how I had the code and I am afraid to break other things down the line if I changed it.

How is this different from yours? Obviously the lines are different:
yours:  Dim f As MAPIFolder
mine:   Private Folders As Outlook.Folders

Are they not equivalent?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
MoeMoe7Author Commented:
Dany,

I understand that your code is basically seeing what the selection values are and I can see how that would work. But what are these values exactly?  And, if I port the code over to another computer, will these values have changed?

That is, I like it, but I am afraid it will be too custom.  What could go wrong?
0
 
Dany BalianCTOCommented:
oh, i thought that you were doing something just for one time, on one computer.. of course my solution is too custom.

then you have to get the folder using a folder path.

Function GetFolder(ByVal FolderPath As String) As Outlook.Folder
    Dim TestFolder As Outlook.Folder
    Dim FoldersArray As Variant
    Dim i As Integer
        
    On Error GoTo GetFolder_Error
    If Left(FolderPath, 2) = "\\" Then
        FolderPath = Right(FolderPath, Len(FolderPath) - 2)
    End If
    'Convert folderpath to array
    FoldersArray = Split(FolderPath, "\")
    Set TestFolder = Application.Session.Folders.item(FoldersArray(0))
    If Not TestFolder Is Nothing Then
        For i = 1 To UBound(FoldersArray, 1)
            Dim SubFolders As Outlook.Folders
            Set SubFolders = TestFolder.Folders
            Set TestFolder = SubFolders.item(FoldersArray(i))
            If TestFolder Is Nothing Then
                Set GetFolder = Nothing
            End If
        Next
    End If
    'Return the TestFolder
    Set GetFolder = TestFolder
    Exit Function
        
GetFolder_Error:
    Set GetFolder = Nothing
    Exit Function
End Function

Sub TestGetFolder()
    Dim folder As Outlook.Folder
    Set folder = GetFolder ("\\Mailbox - Dan Wilson\Inbox\Customers")
    If Not(folder Is Nothing) Then
	folder.Display
    End If
End Sub

Open in new window


Source: How to: Obtain a Folder Object from a Folder Path
0
 
MoeMoe7Author Commented:
Holy!

That seems like a lot of code for something quite basic, no?

Isn't there a simply method that simply says get the default mailbox inbox?
0
 
Dany BalianCTOCommented:
well.. i don't think there's a simple method that gets that.. i didn't write that code, just found it from the msdn.. anyways, you can check the following resource for available concepts from the msdn regarding Items, Folders, and Stores
0
 
MacroShadowCommented:
Try:
Dim f As MAPIFolder
Dim olns As Outlook.NameSpace
Set olns = Outlook.GetNamespace("MAPI")
Set f = olns.Folders("Mailbox - Name, Name")
Set f = f.Folders("Inbox")

Open in new window

Where "Name" is the name of the email account. e.g. Set f = olns.Folders("Mailbox - MoeMoe7, MoeMoe7")
0
 
MoeMoe7Author Commented:
I'll try that tomorrow.
0
 
Martin LissRetired ProgrammerCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now