Home > Enterprise >  How to grab an email from specific Outlook folder with VBA
How to grab an email from specific Outlook folder with VBA

Time:12-25

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?

CodePudding user response:

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")

CodePudding user response:

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.

  • Related