Solved

exporting email body text into excel

Posted on 2011-02-10
6
790 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
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

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.

Question has a verified solution.

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

This process describes the steps required to Import and Export data from and to .pst files using Exchange 2010. We can use these steps to export data from a user to a .pst file, import data back to the same or a different user, or even import data t…
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

821 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