Solved

exporting email body text into excel

Posted on 2011-02-10
6
785 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 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 2

Author Comment

by:miketonny
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Create high volume marketing opportunities using email signatures with these top 10 DOs and DON'Ts of email signature marketing.
If you don't know how to downgrade, my instructions below should be helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

771 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now