Dear Excel Gutu
Below VBA code is looking at a an outlook folder and pasting contents of email in excel workbook
Below is output of code in Excel
Challenge I am having is - the VIN number (always 17 digit alphanumeric) should be extracted from body of email and be pasted in the "VIN" column
My code is doing it but it has few issues
1. It is parsing non VIN too (eg "hi" which it should not)
2 It is not picking it based on 17 characters but it is based on me hard coding based on my search criteria specifying "VIN" (please see code)
3. It is not removing the semi colons (see example 3) -- I need that to be removed and VIN to be clean only alphanumeric
What do I change in my code below to correct these issues? I also need it to be altered such that it checks for 17 characters and not by VIN as that requires manual intervention
Please help. I need to have this delivered by tonight so your prompt assistance would be greatly appreciated,
Thanks in advance.
Sam
Sub GetFromOutlook()
Dim OutlookApp As Outlook.Application
Dim OutlookNamespace As Namespace
Dim Folder As MAPIFolder
Dim OutlookMail As Variant
Dim strBody As String
Dim strFind As String
Dim strColA, strColB, strColC, strColD, strColE As String
Dim xlSheet As Object
Dim itm As Object
Dim i As Integer
Dim sFilterStart As String
Dim sFilterEnd As String
Dim sExtract As String
Dim aExtract() As String
Dim aExtractItems() As String
Set OutlookApp = New Outlook.Application
Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
Set Folder = OutlookNamespace.GetDefaultFolder(olFolderInbox).Folders("AJ")
i = 1
Worksheets("Import").Range("A4:E250").ClearContents
For Each OutlookMail In Folder.Items
If OutlookMail.ReceivedTime >= Range("From_date").Value Then
Range("email_subject").Offset(i, 0).Value = OutlookMail.Subject
Range("email_Subject").Offset(i, 0).Columns.AutoFit
Range("email_Subject").Offset(i, 0).VerticalAlignment = xlTop
Range("eMail_date").Offset(i, 0).Value = OutlookMail.ReceivedTime
Range("email_date").Offset(i, 0).Columns.AutoFit
Range("email_date").Offset(i, 0).VerticalAlignment = xlTop
Range("eMail_sender").Offset(i, 0).Value = OutlookMail.SenderName
Range("email_sender").Offset(i, 0).Columns.AutoFit
Range("email_sender").Offset(i, 0).VerticalAlignment = xlTop
Range("eMail_text").Offset(i, 0).Value = OutlookMail.Body
Range("email_text").Offset(i, 0).Columns.AutoFit
Range("email_text").Offset(i, 0).VerticalAlignment = xlTop
strBody = OutlookMail.Body
strFind = "VIN"
strColA = Mid(strBody, InStr(1, strBody, strFind, 1) + Len(strFind))
strColA = Left(strColA, InStr(strColA, vbLf) - 1)
Range("VIN").Offset(i, 0).Value = strColA
'Cells.wrapText = True
i = i + 1
End If
Next OutlookMail
On Error Resume Next
'Find the next empty line of the worksheet
rCount = xlSheet.Range("A" & xlSheet.Rows.Count).End(-4162).Row
'needed for Exchange 2016. Remove if causing blank lines.
rCount = rCount + 1
i = 1
'For Each OutlookMail In Folder.Items
'If OutlookMail.ReceivedTime >= Range("From_date").Value Then
'strBody = OutlookMail.Body
'strFind = "VIN"
'strColA = Mid(strBody, InStr(1, strBody, strFind, 1) + Len(strFind))
'strColA = Left(strColA, InStr(strColA, vbLf) - 1)
'strFind = "Foreman Name and Number: "
'strColB = Mid(strBody, InStr(1, strBody, strFind, 1) + Len(strFind))
'strColB = Left(strColB, InStr(strColB, vbLf) - 1)
'strFind = "GF Name and Number: "
'strColC = Mid(strBody, InStr(1, strBody, strFind, 1) + Len(strFind))
'strColC = Left(strColC, InStr(strColC, vbLf) - 1)
'strFind = "Location Address: "
'strColD = Mid(strBody, InStr(1, strBody, strFind, 1) + Len(strFind))
'strColD = Left(strColD, InStr(strColD, vbLf) - 1)
'strColE = OutlookMail.ReceivedTime
'Range("VIN").Offset(i, 0).Value = strColA
'Range("Foreman").Offset(i, 0).Value = strColB
'Range("General_Foreman").Offset(i, 0).Value = strColC
'Range("Location_Address").Offset(i, 0).Value = strColD
'Range("Email_Received_Time").Offset(i, 0).Value = strColE
'i = i + 1
'End If
'Next OutlookMail
Set Folder = Nothing
Set OutlookNamespace = Nothing
Set OutlookApp = Nothing
End Sub
Sub EnableWrapText()
Cells.wrapText = True
End Sub
Below VBA code is looking at a an outlook folder and pasting contents of email in excel workbook
Below is output of code in Excel
Challenge I am having is - the VIN number (always 17 digit alphanumeric) should be extracted from body of email and be pasted in the "VIN" column
My code is doing it but it has few issues
1. It is parsing non VIN too (eg "hi" which it should not)
2 It is not picking it based on 17 characters but it is based on me hard coding based on my search criteria specifying "VIN" (please see code)
3. It is not removing the semi colons (see example 3) -- I need that to be removed and VIN to be clean only alphanumeric
What do I change in my code below to correct these issues? I also need it to be altered such that it checks for 17 characters and not by VIN as that requires manual intervention
Please help. I need to have this delivered by tonight so your prompt assistance would be greatly appreciated,
Thanks in advance.
Sam
Sub GetFromOutlook()
Dim OutlookApp As Outlook.Application
Dim OutlookNamespace As Namespace
Dim Folder As MAPIFolder
Dim OutlookMail As Variant
Dim strBody As String
Dim strFind As String
Dim strColA, strColB, strColC, strColD, strColE As String
Dim xlSheet As Object
Dim itm As Object
Dim i As Integer
Dim sFilterStart As String
Dim sFilterEnd As String
Dim sExtract As String
Dim aExtract() As String
Dim aExtractItems() As String
Set OutlookApp = New Outlook.Application
Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
Set Folder = OutlookNamespace.GetDefaultFolder(olFolderInbox).Folders("AJ")
i = 1
Worksheets("Import").Range("A4:E250").ClearContents
For Each OutlookMail In Folder.Items
If OutlookMail.ReceivedTime >= Range("From_date").Value Then
Range("email_subject").Offset(i, 0).Value = OutlookMail.Subject
Range("email_Subject").Offset(i, 0).Columns.AutoFit
Range("email_Subject").Offset(i, 0).VerticalAlignment = xlTop
Range("eMail_date").Offset(i, 0).Value = OutlookMail.ReceivedTime
Range("email_date").Offset(i, 0).Columns.AutoFit
Range("email_date").Offset(i, 0).VerticalAlignment = xlTop
Range("eMail_sender").Offset(i, 0).Value = OutlookMail.SenderName
Range("email_sender").Offset(i, 0).Columns.AutoFit
Range("email_sender").Offset(i, 0).VerticalAlignment = xlTop
Range("eMail_text").Offset(i, 0).Value = OutlookMail.Body
Range("email_text").Offset(i, 0).Columns.AutoFit
Range("email_text").Offset(i, 0).VerticalAlignment = xlTop
strBody = OutlookMail.Body
strFind = "VIN"
strColA = Mid(strBody, InStr(1, strBody, strFind, 1) + Len(strFind))
strColA = Left(strColA, InStr(strColA, vbLf) - 1)
Range("VIN").Offset(i, 0).Value = strColA
'Cells.wrapText = True
i = i + 1
End If
Next OutlookMail
On Error Resume Next
'Find the next empty line of the worksheet
rCount = xlSheet.Range("A" & xlSheet.Rows.Count).End(-4162).Row
'needed for Exchange 2016. Remove if causing blank lines.
rCount = rCount + 1
i = 1
'For Each OutlookMail In Folder.Items
'If OutlookMail.ReceivedTime >= Range("From_date").Value Then
'strBody = OutlookMail.Body
'strFind = "VIN"
'strColA = Mid(strBody, InStr(1, strBody, strFind, 1) + Len(strFind))
'strColA = Left(strColA, InStr(strColA, vbLf) - 1)
'strFind = "Foreman Name and Number: "
'strColB = Mid(strBody, InStr(1, strBody, strFind, 1) + Len(strFind))
'strColB = Left(strColB, InStr(strColB, vbLf) - 1)
'strFind = "GF Name and Number: "
'strColC = Mid(strBody, InStr(1, strBody, strFind, 1) + Len(strFind))
'strColC = Left(strColC, InStr(strColC, vbLf) - 1)
'strFind = "Location Address: "
'strColD = Mid(strBody, InStr(1, strBody, strFind, 1) + Len(strFind))
'strColD = Left(strColD, InStr(strColD, vbLf) - 1)
'strColE = OutlookMail.ReceivedTime
'Range("VIN").Offset(i, 0).Value = strColA
'Range("Foreman").Offset(i, 0).Value = strColB
'Range("General_Foreman").Offset(i, 0).Value = strColC
'Range("Location_Address").Offset(i, 0).Value = strColD
'Range("Email_Received_Time").Offset(i, 0).Value = strColE
'i = i + 1
'End If
'Next OutlookMail
Set Folder = Nothing
Set OutlookNamespace = Nothing
Set OutlookApp = Nothing
End Sub
Sub EnableWrapText()
Cells.wrapText = True
End Sub