Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing email messages to flat files in Outlook with VBA

I have written a VBA app that opens a folder in outlook and then iterates through the messages. I need to write the message bodies (with some tweaking) to a single flat file. My code is as follows...

Private Sub btnGo_Click()
    Dim objOutlook As New Outlook.Application
    Dim objNameSpace As Outlook.NameSpace
    Dim objInbox As MAPIFolder
    Dim objMail As mailItem
    Dim count As Integer

    Set objNameSpace = objOutlook.GetNamespace("MAPI")
    Set objInbox = objNameSpace.GetDefaultFolder(olFolderInbox)
    count = 0

    For Each objMail In objInbox.Items
       lblStatus.Caption = "Count: " + CStr(count)
       ProcessMailItem (objMail)
       count = count + 1
    Next objMail

  End If
End Sub

The part in question is "ProcessMailItem". As I am not overly concerned with performance at this stage so the very inefficent "open, append, close" file methodology is fine for this example.

I know I could spend some time looking up the answer with google but I checked here first and there was no good answers for this. Being a fan of Stackoverflow I hope that putting this up here will help future developers looking for answers. Thanks for your patience.

like image 246
Craig Avatar asked Dec 30 '08 17:12

Craig


3 Answers

You can get away with writing to a file without using any objects, just using the built in VBA file tools:

Open "C:\file.txt" for append as 1
Print #1, SomeStringVar
Close #1
like image 194
Jon Fournier Avatar answered Oct 21 '22 10:10

Jon Fournier


If you don't mind re-opening the output file each time you append some text, then this should work.

Private Sub ProcessMailItem(objMail As MailItem)

    Dim fso As New FileSystemObject
    Dim ts As TextStream

    Set ts = fso.OpenTextFile("C:\Outputfile.txt", ForAppending, True)

    ts.Write(objMail.Body)

    ts.Close()
    Set ts = Nothing
    Set fso = Nothing

End Sub

You'll also need to add a reference to the Microsoft Scripting Runtime library. This has FileSystemObject in it.

like image 33
Eric Ness Avatar answered Oct 21 '22 11:10

Eric Ness


You also have to take care of the security popup "trying to access email addresses" which is covered in Outlook "Object Model Guard" Security Issues for Developers

Public Sub ProcessMailItem(objMail As MailItem)
Dim FSO As New FileSystemObject
Dim ts As TextStream
Dim loc As String
Dim subject As String
Dim strID As String
' per http://www.outlookcode.com/article.aspx?ID=52
Dim olNS As Outlook.NameSpace
Dim oMail As Outlook.MailItem

strID = MyMail.EntryID
Set olNS = Application.GetNamespace("MAPI")
Set oMail = olNS.GetItemFromID(strID)
subject = oMail.subject
Set ts = FSO.OpenTextFile("C:\Documents and Settings\tempuser\My Documents\EMAILS\" + subject, ForAppending, True)
ts.Write (oMail.Body)
ts.Close
Set ts = Nothing
Set FSO = Nothing
Set oMail = Nothing
Set olNS = Nothing

End Sub

like image 23
jim Avatar answered Oct 21 '22 11:10

jim