EXCEL/VBA QUESTION - how to open outlook and access one email content from inbox and further process using VBA

shrivallabh amrute

New Member
Joined
Oct 12, 2019
Messages
2
hello friends,

i wish to do following steps with vba code
1. open an outlook application and then go to subfolder of inbox , open one email with perticular subject and then open csv file attached to it

please help me with that rest thing i will manage as there is further process involved based on that cvs file
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Hi,
You may start with this code:
Rich (BB code):
Sub Save_CSV_attachment()
 
  ' Outlook constant
  Const olFolderInbox = 6
  
  ' Settings, change to suit
  Const sDestFolder = "D:\Temp\"    ' Where to save CSV attachment
  Const sSubject = "My Subject"     ' Subject of the mail with attachment
  Const sSubFolderInInbox = "Test"  ' Name of Subfolder in Inbox
 
  ' Variables
  Dim OutlookApp As Object, oMail As Object, oAtt As Object
  Dim sFilter As String
 
  ' Check destinarion folder
  If Dir(sDestFolder, vbDirectory) = "" Then
    MsgBox "Destination folder not found: " & sDestFolder, vbExclamation, "Exit"
    Exit Sub
  End If
  
  ' Get/create outlook object
  On Error Resume Next
  Set OutlookApp = GetObject(, "Outlook.Application")
  If Err Then
    Set OutlookApp = CreateObject("Outlook.Application")
  End If
  On Error GoTo 0
 
  ' Build filter for restriction items by Subject
  sFilter = "[Subject] = '" & sSubject & "'"
 
  ' Main
  For Each oMail In OutlookApp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Folders(sSubFolderInInbox).Items.Restrict(sFilter)
    For Each oAtt In oMail.Attachments
      If LCase(oAtt.Filename) Like "*.csv" Then
        oAtt.SaveAsFile sDestFolder & oAtt.Filename
        MsgBox "Attachment is saved as " & sDestFolder & oAtt.Filename
        Exit For
      End If
    Next
  Next
  
  ' Release memory of the object variables
  Set oAtt = Nothing
  Set oMail = Nothing
  Set OutlookApp = Nothing
  
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,875
Members
452,363
Latest member
merico17

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