Outlook 2007 VBA and Dumping emails into a CSV File

  • Thread starter Thread starter Rob
  • Start date Start date
R

Rob

Hi,

I need to dump emails in a specific folder into a text file. I get about
10-20 of these emails per day.

I will then use VBA code to handle the text file and import the data into
excel. I just don't know how to handle the messages in outlook. How would I
write this code?

Thanks,
 
The most important first step is to state what your goal is, because
there appear to be multiple ways to do what you want. Saying you want
to dump emails into a text file is limiting your goal to just one
particular solution.

There are also some details missing. You want to place the text of the
email into Excel, just the Body? What about the headers? Do they all
need to be in one row or column?

There's a tip here that might get you started.

http://www.exceltip.com/st/Log_files_using_VBA_in_Microsoft_Excel/467.html

It would be simple to loop through a folder in Outlook, assign the
Body of each email to a string, and then write the string to a text
file using the macro code in that link.

HTH,
JP
 
JP said:
The most important first step is to state what your goal is, because
there appear to be multiple ways to do what you want. Saying you want
to dump emails into a text file is limiting your goal to just one
particular solution.

There are also some details missing. You want to place the text of the
email into Excel, just the Body? What about the headers? Do they all
need to be in one row or column?

There's a tip here that might get you started.

http://www.exceltip.com/st/Log_files_using_VBA_in_Microsoft_Excel/467.html

It would be simple to loop through a folder in Outlook, assign the
Body of each email to a string, and then write the string to a text
file using the macro code in that link.

HTH,
JP
Thank you for your reply JP. My end goal is to automate the task of
transporting the contents of these emails into an Excel table. I want to do
it programmatically.

Each Email contains 7 rows with a row in between. It looks like the
following...

Overall service rating: 10

Assisting Agent Name: Josh

Additional Comments: Great customer service

Customer Name:

Customer Email Address:

Customer Phone #:

Customer Account #:

In the meantime, I will read this page you suggested and see if I can figure
it out myself...
 
If you know for sure that the contents of each email are in that same
exact format, it's a simple matter to parse each email body and look
for the text you want.
Here's some VBA in Outlook that sets a reference to each email in your
default Inbox and writes some properties to an Excel worksheet. It's
untested but it should work to show you how you can write to a
worksheet from Outlook.

(Based on http://www.codeforexcelandoutlook.com/blog/2008/09/export-outlook-contacts-to-excel/)

Option Explicit

Sub ExtractEmailsFromOutlookToExcel()

Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Dim myItems As Outlook.Items
Dim ThisItem As Object
Dim Msg As Outlook.MailItem
Dim xlApp As Excel.Application
Dim MyBook As Excel.Workbook
Dim MySheet As Excel.Worksheet
Dim arrData() As Variant
Dim i As Long

Application.ScreenUpdating = False

Set olApp = Outlook.Application

Set olNS = olApp.GetNamespace("MAPI")
Set myItems = olNS.GetDefaultFolder(olFolderInbox).Items

If myItems.Count > 0 Then

' resize data array to hold 3 random msg properties
ReDim arrData(1 To myItems.Count, 1 To 3)

' get Excel and set up some basic wksht properties
Set xlApp = GetExcelApplication
Set MyBook = xlApp.Workbooks.Add
Set MySheet = MyBook.Sheets(1)
MySheet.Name = "Emails"

' loop through each item and write to array
For i = 1 To myItems.Count
If TypeName(myItems.Item(i)) = "MailItem" Then
Set Msg = ThisItem

With Msg
arrData(i, 1) = .Sensitivity
arrData(i, 2) = .ReceivedTime
arrData(i, 3) = .SenderEmailType
End With

End If
Next i

' dump array to worksheet in one shot
MySheet.Range("A1").Offset(1, 0).Resize(myItems.Count, 3).Value =
arrData

End If


ExitProc:
Set olApp = Nothing
Set olNS = Nothing
Set myItems = Nothing
End Sub

Function GetExcelApplication()

On Error Resume Next
Set GetExcelApplication = GetObject(, "Excel.Application")

If Err <> 0 Then
Set GetExcelApplication = CreateObject("Excel.Application")
End If
On Error GoTo 0

End Function
 
Hi, there!

I have a similar problem, where I have an email message sent by our webform,
which body content has to be parsed into an Excel file.

I'm not familar with VBA, so I've copied and pasted the code below into my
Outlook VBA and tried to run, but first of all, it gives me an error message
at line "arrData(i, 1) = .Sensitivity". Would anybody have any clue about why
it is happening?

Also, I have lots of e-mails in my Inbox, but I just need the ones with
certain words in their Subject. How would I define these words?

Thank you!
 
Back
Top