0

I am trying to grab target email received today from specific folder. My current VBA code is :

Sub ExportOutlookTableToExcel()

Dim oLookInspector As Inspector
Dim oLookMailitem As MailItem

Dim oLookWordDoc As Word.Document
Dim oLookWordTbl As Word.Table

Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook 
Dim xlWrkSheet As Excel.Worksheet

Dim Today As String
Today = Date


'Grab Email Item
 Set oLookMailitem =Application.ActiveExplorer.CurrentFolder.Items("Apples Sales")

 Set oLookInspector = oLookMailitem.GetInspector

 Set oLookWordDoc = oLookInspector.WordEditor

However, my email is in specific folder called "Apples", if i move it to Inbox folder it works with CurrentFolder emthod. Is it any way to specify in which folder VBA should grab an email?

Camilla
  • 111
  • 10
  • Is the folder you need a subfolder of `Inbox`? – FaneDuru Dec 24 '21 at 17:46
  • yes it is a subfolder – Camilla Dec 24 '21 at 18:08
  • Please, try `Set oLookMailitem = Application.Session.GetDefaultFolder(olFolderInbox).Folders("Apples").items("Apples Sales")`. Of course, supposing that the subject of the incriminated mail is "Apples Sales" and it exists in subfolder "Apples". – FaneDuru Dec 24 '21 at 19:45
  • Does this answer your question? [Using visual basic to access subfolder in Inbox?](https://stackoverflow.com/questions/8322432/using-visual-basic-to-access-subfolder-in-inbox) – niton Jan 01 '22 at 18:41

2 Answers2

0

Assuming the folder is the subfolder on the Inbox folder, try

set folder = Application.Session.GetDefaultFolder(olFolderInbox).Folders("Folder Name")

if it is on the same level as the Inbox, use

set folder = Application.Session.GetDEfaultFolder(olFolderInbox).Parent.Folders("Folder Name")
Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
0

You can use the NameSpace.PickFolder method which displays the Pick Folder dialog box. The Pick Folder dialog box is a modal dialog box which means that code execution will not continue until the user either selects a folder or cancels the dialog box. The method returns a Folder object that represents the folder that the user selects in the dialog box, or Nothing if the dialog box is canceled by the user.

So, each time your VBA macro runs you can choose the right folder at runtime. Otherwise, you will have to specify the hard-coded folder name.

The NameSpace.GetDefaultFolder method returns a Folder object that represents the default folder of the requested type for the current profile; for example, obtains the default ``InboxorCalendar` folder for the user who is currently logged on.

Then you can use the Folder.Folders property which returns the Folders collection that represents all the folders contained in the specified Folder. So, you may find a subfolder.

Set item = Application.GetNamespace("MAPI)".GetDefaultFolder(olFolderInbox).Folders("Apples").Items("Apples Sales")
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45