I need to export line 3 and line 4 from an email. I have a VBA code stored in the Microsoft Outlook Session. It works well, but it exports the entire mail body. I need only lines 3 and 4.
Here is the sample code:
Option Explicit
Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set Items = objNS.GetDefaultFolder(olFolderInbox).Folders("Leads").Items
End Sub
Private Sub Items_ItemAdd(ByVal item As Object)
On Error GoTo ErrorHandler
Dim Msg As Outlook.MailItem
Dim oXL As Object
Dim oWS As Object
Dim lngRow As Long
Set oXL = CreateObject("Excel.Application")
oXL.Workbooks.Open FileName:="C:\temp\Mail_Export.xlsx", AddTOMRU:=False, UpdateLinks:=False
Set oWS = oXL.Sheets("Sheet1")
lngRow = oWS.Range("A" & oXL.Rows.Count).End(-4162).Offset(1).Row
If TypeName(item) = "MailItem" Then
Set Msg = item
With oWS
.cells(lngRow, 1).Value = Msg.SenderName
.cells(lngRow, 2).Value = Msg.SenderEmailAddress
.cells(lngRow, 3).Value = Msg.Body
End With
With oXL
.activeworkbook.Save
.activeworkbook.Close SaveChanges:=2 '
.Application.Quit
End With
Set oXL = Nothing
Set oWS = Nothing
End If
ExitPoint:
Exit Sub
ErrorHandler:
MsgBox Err.Number & " - " & Err.Description
Resume ExitPoint
Resume
End Function
Mail_Export.xlsx
is auto aving the emails exported from "Leads" folder in MS Outlook. However, I need only lines 3 and 4.
This is the sample mail body:
Hi Team
Following people contacted your helpdesk:-
Name: ABC DEF
Email: abc.def@gmail.com
Thank you
XYZ Consultants