How to receive e-mail using Excel VBA

L

Leonardo

Hello. In this moment, I am writing a code, using the excellent
information obtained from Ron de Bruin's site, about sending e-mail
from excel.

But my boss asks me to receive an e-mail to in order to modify VBA code
execution, depending if there area some keywords inside the e-mail
body, or FROM field. Where can I find info about receiving e-mail using
Excel VBA ?

Best regards.
 
T

Tim Williams

Are you asking about scanning Outlook for received mails, or connecting to an account on a mailserver?

Tim
 
L

Leonardo

Hello. First choice is good to me: scanning Outlook to verifiy if a
received e-mail contains some keywords in order to take some actions or
not.


LQ
 
T

Tim Williams

Try this:

'*************************************************************
Sub CheckOutlookMessages2()

Const S_SUBJECT As String = "YourSubjectHere"
Dim olApp As Outlook.Application
Dim fInbox As MAPIFolder

Dim olInboxCollection As Object
Dim olInboxItem As Object
Dim iCount As Integer
Dim itemCount, n

On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
If olApp Is Nothing Then
MsgBox "Outlook is not running: please open the application first"
Err.Clear
Exit Sub
End If
On Error GoTo haveError

'Get the inbox folder
Set fInbox = olApp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
Set olInboxCollection = fInbox.Items

itemCount = olInboxCollection.Count
iCount = 0
For n = itemCount To 1 Step -1
Set olInboxItem = olInboxCollection(n)
If StrComp(olInboxItem.Subject, S_SUBJECT) = 0 Then

iCount = iCount + 1
Application.StatusBar = "Processing # " & iCount

'here's where you get the info from the attachment....
MsgBox olInboxItem.Body

End If
Next n

haveError:
If Err <> 0 Then MsgBox "Error:" & vbCrLf & Err.Description
Application.StatusBar = False

End Sub
'***************************************************

Tim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top