Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

exporting email body text into excel

Posted on 2011-02-10
6
Medium Priority
?
820 Views
Last Modified: 2012-05-11
Hi experts,
    I'm working on a problem that needs me to export my email body text to existing excel work sheet, both of them are 2007 version.
Here's the code i formatting my current opened email:
 
Dim objOutlook As Outlook.Application
Dim oMail As Outlook.MailItem
Set oMail = Application.ActiveInspector.CurrentItem
Dim strBody As String
strBody = oMail.Body

Dim strCustomerNo As String
Dim strContact As String
Dim strEmail As String
Dim strType As String
Dim strFrequency As String

'Formatting Email body before export into excel--------------------
strBody = Mid(strBody, InStr(strBody, "Customer:"))


strCustomerNo = Left(strBody, InStr(strBody, "Contact:") - 1)
strCustomerNo = Mid(strCustomerNo, InStr(strCustomerNo, ": ") + 1)
strCustomerNo = Trim(strCustomerNo)

strBody = Mid(strBody, InStr(strBody, "Contact:"))

strContact = Left(strBody, InStr(strBody, "Pricelevel:") - 1)
strContact = Mid(strContact, InStr(strContact, ": ") + 1)
strContact = Trim(strContact)

strBody = Right(strBody, InStr(strBody, "Requested email details:"))
strBody = Mid(strBody, InStr(strBody, "Email:"))

strEmail = Left(strBody, InStr(strBody, "Type:") - 1)
strEmail = Mid(strEmail, InStr(strEmail, ": ") + 1)
strEmail = Trim(strEmail)

strBody = Mid(strBody, InStr(strBody, "Type:"))

strType = Left(strBody, InStr(strBody, "Frequency:") - 1)
strType = Mid(strType, InStr(strType, ": ") + 1)
strType = Trim(strType)


strBody = Mid(strBody, InStr(strBody, "Frequency:"))

strFrequency = strBody
strFrequency = Mid(strFrequency, InStr(strFrequency, ": ") + 1)
strFrequency = Trim(strFrequency)

Open in new window


result comes up like this:
  strCustomerNo : "KAR0156"
  strContact : "Andrew"
  strEmail : "andrew@abc.co.nz"
  strType : "XLSX"
  strFrequency : "Weekly"


Now i need to export the string values into my excel work sheet1, my work sheet's first row is
column headers, so will be 5 columns in total. and i need to export these email information everytime i receive them. how do i do that?
0
Comment
Question by:miketonny
  • 3
  • 3
6 Comments
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 2000 total points
ID: 34866167
Set up a reference to Excel like you did with Outlook. Then you can do something like
Dim objExcel As Excel.Application
Dim objWS As Excel.Worksheet
Dim lastRow As Integer

objExcel.Visible = True
'Change the path and workbook name here to what it should be
Set objWS = objExcel.Workbooks.Open("C:\temp\workbook.xls").Worksheets("Sheet1")
lastRow = objWS.UsedRange.Rows.Count + 1
objWS.Range("A" & lastRow) = strCustomerNo
objWS.Range("B" & lastRow) = strContact
objWS.Range("C" & lastRow) = strEmail
objWS.Range("D" & lastRow) = strType
objWS.Range("E" & lastRow) = strFrequency

Open in new window

0
 
LVL 2

Author Comment

by:miketonny
ID: 34866649
thanks that works well, but i still have some problems
 
Dim objExcel As Excel.Application
Dim objWS As Excel.Worksheet
Dim lastRow As Integer
Set objExcel = CreateObject("Excel.Application")
'Change the path and workbook name here to what it should be
Set objWS = objExcel.Workbooks.Open("C:\CustomerPricelists\test.xlsx").Worksheets("Sheet1")
lastRow = objWS.UsedRange.Rows.Count + 1
objWS.Range("A" & lastRow) = strCustomerNo
objWS.Range("B" & lastRow) = strContact
objWS.Range("C" & lastRow) = strEmail
objWS.Range("D" & lastRow) = strType
objWS.Range("E" & lastRow) = strFrequency

objExcel.ActiveWorkbook.Save
'objExcel.Visible = True
'objExcel.UserControl = True

objExcel.Quit
Set objWS = Nothing
Set objExcel = Nothing

Open in new window


i have code like that but everytime i run it it creates a new excel file and ask me if i want to replace the existing one, how do i save it in the original file and stop the message?
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34866895
Remove your line 14 and replace it with
objExcel.DisplayAlerts = False 'Turn off the "Do you want to overwrite?" alert (and all others)
objWS.Parent.Save 'This way it's guaranteed to use the right workbook. ActiveWorkbook isn't as stable
objExcel.DisplayAlerts = True 'Turn alerts back on


objWS.Parent.Close False 'Close the file so it won't prompt again
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 2

Author Comment

by:miketonny
ID: 34867081
thx i did that and message went away however,
objExcel.Quit isn't working ? after this line 'EXCEL.EXE' is still in Task Manager and i cant manually open the file, is this the wrong command i use?
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34867192
That should be correct. I don't see why it would do that. I'll take a couple stabs at what might be causing it.

Are you sure that isn't just hanging around from before? Have you tried killing it from Task Manager and running the script again? If every CreateObject("Excel.Application") is coupled with a Quit, then the whole thing should work.
You are turning the alerts back on before the quit statement right?

0
 
LVL 2

Author Comment

by:miketonny
ID: 34867327
yes, i did it again and it all worked well, it's strange. but thanks a lot for helping me out, much appreciate:)
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to import Lotus Notes Contacts into Outlook 2016, 2013, 2010 and 2007 etc. with a few manual steps. You can easily export and migrate Lotus Notes contacts into Microsoft Outlook without having to use any third party tools.
Mailbox Corruption is a nightmare every Exchange DBA wishes he never has. Recovering from it can be super-hectic if not entirely futile. And though techniques like the New-MailboxRepairRequest cmdlet have been designed to help with fixing minor corr…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question