PDA

View Full Version : Possible? Save email text in sequentially #'d files?



GreenTree
10-19-2007, 07:32 PM
Wanted to find out if I'm barking up the right or wrong tree here...

I've been getting familiar over the past few months with VBA in Excel, and feel pretty comfortable with that. I've used Outlook a little bit in years past, but only in the most basic read/write email role, so my familiarity with Outlook scripting/macros (or even rules, really) is about nil.

What I'd like to do is this: each time a new email arrives, Outlook saves the entire text of it (including the to/from/subject) into a sequentially numbered text file on my hard drive. Then, a separate program running in Excel VBA will every so often check for the existence of the next file in sequence, and if it exists, load it, parse it, and take various actions, including perhaps replying to it with a particular file attached. Then the file # counter increments and the process repeats. What I envision is that this setup would run 24/7 on a dedicated computer for maybe a week at a time, during which time it might deal with 200-500 emails. That's the rough size of the project.

I feel confident that I can write everything I need on the Excel end of it to see that the new file exists, load & parse & decide what needs to happen; what I don't know about it having Outlook save each new email as a sequential file (mymail0001.txt, mymail0002.txt, etc), and having the Excel program do the send-with-that-file-attached routine. I get the impression the latter is straightforward; the former... I don't know.

Am I trying to make a square hole with a round drillbit, or is what I'm trying to do reasonably straightforward? If the latter, any hints about examples/threads/KB articles/etc would be most appreciated & helpful.

Many thanks,

G.T.

Charlize
10-23-2007, 06:44 AM
What I'd like to do is this: each time a new email arrives, Outlook saves the entire text of it (including the to/from/subject) into a sequentially numbered text file on my hard drive.That's understandable and doable.
A separate program running in Excel VBA will every so often check for the existence of the next file in sequence, and if it exists, load it, parse it, and take various actions, including perhaps replying to it with a particular file attached. Then the file # counter increments and the process repeats. What I envision is that this setup would run 24/7 on a dedicated computer for maybe a week at a time, during which time it might deal with 200-500 emails. That's the rough size of the project.Why check on that sequential no. If the file is saved, process it immediately into an excel file ... and attach the result to a new mail to be sent ... ???

Charlize
10-23-2007, 06:59 AM
This saves every new mail to a txtmail by using a rule (every mail needs to be processed by this routine). You need the clickyes utility to be installed.Option Explicit
' Declare Windows' API functions
' To be used with ExpressClick Yes
Private Declare Function RegisterWindowMessage _
Lib "user32" Alias "RegisterWindowMessageA" _
(ByVal lpString As String) As Long
Private Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" (ByVal lpClassName As Any, _
ByVal lpWindowName As Any) As Long
Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" (ByVal hwnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Any) As Long
'These are used by the utility ClickYes Freeware
Public wnd As Long
Public uClickYes As Long
Public Res As Long
Sub save_newmails_txt(myItem As Outlook.MailItem)
Dim vno As Long
Dim mypath As String
mypath = "C:\Data\Textmail\"
vno = CountFiles(mypath)
vno = vno + 1
'prepare the clickyes utility to click when security
'window pops up
Call PrepareClickYes
'directory c:\tempmail must be present
myItem.SaveAs "C:\Data\Textmail\" & vno & "-txtmail.txt", olTXT
'push the ok button on the security window
Call PerformClickYes
End Sub
Sub PrepareClickYes()
'called before attempting to manipulate a message
uClickYes = RegisterWindowMessage("CLICKYES_SUSPEND_RESUME")
wnd = FindWindow("EXCLICKYES_WND", 0&)
Res = SendMessage(wnd, uClickYes, 1, 0)
End Sub
Sub PerformClickYes()
'called directly after the code that manipulates
'a message. Clicks the yes and places the ClickYes utility
'back in suspend mode. When some other routine (that's not
'controlled by you) wants to do something with your
'messages, you still get that warning.
Res = SendMessage(wnd, uClickYes, 0, 0)
End Sub
Function CountFiles(strPath As String) As Integer
'Counts the no of files in a directory
Dim FSO As Object
Dim fldr As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
Set fldr = FSO.GetFolder(strPath)
CountFiles = fldr.Files.Count
Set fldr = Nothing
Set FSO = Nothing
End Function