Solved

exporting email body text into excel

Posted on 2011-02-10
6
792 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 500 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

MS Outlook is a world-class email client application that is mainly used for e-communication globally.  In this article, we will discuss the basic idea about MS Outlook, its advanced features, and types of MS Outlook File formats.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

751 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