Home > OS >  Extract specific data in Outlook emails to Excel s/sheet
Extract specific data in Outlook emails to Excel s/sheet

Time:11-09

Good evening all,

I have an Excel Macro I am trying to make work and also adapt slightly. There are 3 issues I need to fix:

  1. My code throws up a Compile Error: "End if without block if". I am a beginner and don't know what this means?

  2. I need to modify my code so it scans the auto signature of the email as well, but don't know how to do this.

  3. I would like to modify the code so that it extracts any data from the Subject field, email body and auto signature that is contained in brackets and send it to individual rows in Excel. However I would like to use markers R1, R2, R3, R4, R5, R6 so that the data in brackets marked R1 is spat out into Row 1 of my Excel s/sheet, data in brackets marked R2 is spat out into Row 2 etc. See image below. It has been suggested that I can use the FIND and MID functions but again my knowledge is not strong enough to code this.

I would be grateful if someone can help me please :)

Image

My code:

Sub GetFromOutlook()

Dim OutlookApp As Outlook.Application
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("NoV")

i = 1

For Each OutlookMail In Folder.Items
        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_text").Offset(i, 0).Value = OutlookMail.Body

i = i   1
    End If
Next OutlookMail

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

End Sub

CodePudding user response:

In the following piece of code:

For Each OutlookMail In Folder.Items
        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_text").Offset(i, 0).Value = OutlookMail.Body

i = i   1
    End If
Next OutlookMail

There is no If operator in the code, so the End If is not required:

For Each OutlookMail In Folder.Items
        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_text").Offset(i, 0).Value = OutlookMail.Body

        i = i   1
   
Next OutlookMail
  • Related