Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

exporting email body text into excel

Posted on 2011-02-10
6
Medium Priority
?
806 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 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
In this article I discuss my selections of the Top Four free Outlook OST File Viewers available. Open, view and read even damaged OST files by using these tools. They all provide a clear preview of all data such as emails, notes, tasks, calendars, e…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

670 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