Automate Extraction of Specific Word from Outlook Message

How can we extract a specific word from an Outlook email message automatically using VBA.

Creating reference to Microsoft Outlook Library
Creating reference to Microsoft Outlook Library

Watch this video on YouTube.

Here’s the complete VBA code:

Option Explicit

Sub GetDataFromOutlook()
https://www.exceltrainingvideos.com/get-data-from-outlook-into-excel-worksheet/
Dim OutlookApp As Outlook.Application
‘allows access to all Outlook data stored in the user’s mail stores
Dim OutlookNamespace As Namespace
Dim Folder As MAPIFolder
Dim OutlookMail As Variant
Dim i As Integer

Set OutlookApp = New Outlook.Application
Set OutlookNamespace = OutlookApp.GetNamespace(“MAPI”)
‘Set Folder = OutlookNamespace.GetDefaultFolder(olFolderInbox).Folders(“Net Sales Report”).Folders(“Sales”)
Set Folder = OutlookNamespace.GetDefaultFolder(olFolderInbox)
i = 1

For Each OutlookMail In Folder.Items
If OutlookMail.ReceivedTime >= Range(“email_Date”).Value Then
Range(“eMail_subject”).Offset(i, 0).Value = OutlookMail.Subject
Range(“eMail_date”).Offset(i, 0).Value = OutlookMail.ReceivedTime
Range(“eMail_sender”).Offset(i, 0).Value = OutlookMail.SenderName
Range(“email_Body”).Offset(i, 0).Value = OutlookMail.Body

i = i + 1
End If
Next OutlookMail

Set Folder = Nothing
Set OutlookNamespace = Nothing
Set OutlookApp = Nothing

cleanData
usingSplitWithPattern

End Sub

Intellisense at work
Intellisense at work

Option Explicit

Sub cleanData()

Dim c As Range

Dim s As String

For Each c In Range(“E3:E5”)

‘Chr(10) is the Line Feed character and Chr(13) is the Carriage Return character
‘https://www.petefreitag.com/item/863.cfm

c = Replace(c, Chr(13) & Chr(10), ” “)

Next c

End Sub

Option Explicit

Sub usingSplitWithPattern()
Dim c As Range
Dim s As String
Dim myArray() As String
Dim i As Long, count As Long
Dim first As Long, last As Long
Dim lengthofarray As Long
Dim x As Long, y As Long

x = 3 ‘row
y = 6 ‘column

For Each c In Range(“E3:E5”)

s = c

myArray = Split(s, ” “) ‘The quick brown fox –> myarray(0)=The, myarray(1)=quick,etc

first = LBound(myArray)
last = UBound(myArray)

lengthofarray = last – first

‘MsgBox lengthofarray

For i = 0 To lengthofarray

If myArray(i) Like “[A-Za-z]#######” Then

‘initially x=3, y=6

Cells(x, y) = myArray(i)

y = y + 1

End If

Next i

x = x + 1
y = 6

lengthofarray = 0

Next c

End Sub

Published by

Dinesh Kumar Takyar

Welcome to exceltrainingvideos.com! My aim is to help you learn MS Excel including VBA. I always felt that a good way to learn something was to find solutions to problems in that domain. That is why I share these Excel videos with you. Mostly these are questions asked during my corporate training sessions. I've been training individuals and companies in computers since 1991. I did my B. Sc. (Vordiplom), M. Sc. (Diplom) and Ph.D. (Dr. rer. nat.) from Hamburg, Germany. The best thing about solving some of my visitor's questions is that I also gain new insights. For more Excel VBA solutions you may like to visit my YouTube channel: http://youtube.com/familycomputerclub For a structured Excel VBA training course online you can visit: https://www.youtube.com/excelvbaonline

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.