Find out Mail type - Outlook loop

MUKESHY12390

Well-known Member
Joined
Sep 18, 2012
Messages
901
Office Version
  1. 2013
  2. 2011
  3. 2010
  4. 2007
Platform
  1. Windows
Hi,
I'm trying to loop through the mails in particular folder, the code I have written code it works but gives the problem when folder has other items other then simple mail(mailItem).

currently I have 3 things on folder 2mails and 1 meeting invitation, because of meeting invitation I get error. Is there anyway we resolve it.


Code:
Sub oAutomail()
Dim oOutlookApp As Outlook.Application
Dim oMail As MailItem  '''' it  don't produce any error if I change to Object
Dim oNamespace As Outlook.Namespace
Dim oFolder As Outlook.Folder
Dim ofolderlist As Outlook.Folder
Dim oattachement As Attachment
Dim lr


Set oOutlookApp = New Outlook.Application
Set oNamespace = oOutlookApp.GetNamespace("MAPI")
Set oFolder = oNamespace.GetDefaultFolder(olFolderInbox)
'Set ofolder = ofolder.Folders("LivePerson")  '' will allow to Direct work through folder
  i = 1
  
For Each ofolderlist In oFolder.Folders   '''1st loop through folder
'Debug.Print ofolderlist.Name




    If ofolderlist.Name = "Vz" Then
           
     For Each oMail In ofolderlist.Items  '''2nd Loop through mail
       Debug.Print oMail.Subject
        For Each oattachement In oMail.Attachments ''3rd Loop Loop attachments
            lr = oattachement.DisplayName   ''' attachment Name very importent to save file
            Debug.Print lr
           ' oattachement.SaveAsFile "Z:\Operations\O2\Documents\Transactional\Mukesh\Query Wise\DMacro\outlook\" & lr
            i = i + 1
        Next oattachement
     Next oMail
    End If
Next ofolderlist




End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
First, declare an object variable as a generic object...

Code:
    Dim oItem As Object

Then you can test for a mail item as follows...

Code:
    For Each oItem In [COLOR=#574123]ofolderlist[/COLOR].Items
        If TypeName(oItem) = "MailItem" Then
            'etc
            '
            '
        End If
    Next oItem

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,215,241
Messages
6,123,824
Members
449,127
Latest member
Cyko

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top