Link to home
Start Free TrialLog in
Avatar of davidcowler
davidcowlerFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Convert incoming emails to text files - have code but it's not working

Hi

I borrowed some code from another issue on here but it isn't working properly and I don't know why.

It should create a text file for every email coming in to the Inbox. It does create the text files, but doesn't always fire. For example, I just sent 6 mails to the test account, and only 4 were converted. I repeated the excercise and again only 4 were converted, although one of those was one that wasn't converted previously, and one that was previously converted wasn't on the second attempt. I hope that makes sense! Basically, it appears to be random.

My latest test was to wait for 5 minutes and then send one email. It did not convert. It might be important to point out that I am forwarding mails to the test account, not composing new ones, as this is the way in which we will be using it.

I am very experiened with VBA in Excel but this is my first attempt at Outlook.
Code is attached.
General - Declarations:

Private WithEvents objInboxItems As Items


Private Sub Application_Startup()
    Dim objNS As NameSpace
    Set objNS = Application.GetNamespace("MAPI")
    ' instantiate Items collections for folders we want to monitor
    Set objInboxItems = objNS.GetDefaultFolder(olFolderInbox).Items
    Set objNS = Nothing
End Sub


Private Sub Application_Quit()
    ' disassociate global objects declared WithEvents
    Set objInboxItems = Nothing
End Sub


Private Sub objInboxItems_ItemAdd(ByVal Item As Object)
    Dim olItems As Items, _
        olItem As Object, _
        olMailItem As MailItem, _
        objFSO As New FileSystemObject, _
        objMsgFile As TextStream
    Set olItems = objInboxItems.Restrict("[Unread] = True")
    For Each olItem In olItems
        If olItem.Class = olMail Then
            Set olMailItem = olItem
            Set objMsgFile = objFSO.CreateTextFile("C:\LicenceEmails\" & Replace(Date, "/", "-") & "@" & Int(Timer) & ".txt")
            objMsgFile.WriteLine "Subject: " & olMailItem.Subject
            If olMailItem.BodyFormat = olFormatHTML Then
                objMsgFile.WriteLine olMailItem.HTMLBody
            Else
                objMsgFile.WriteLine olMailItem.Body
            End If
            objMsgFile.Close
            olMailItem.UnRead = False
        End If
    Next
End Sub

Open in new window

Avatar of David Lee
David Lee
Flag of United States of America image

Hi, davidcowler.

Try this approach.  As I mentioned on Twitter (I'm TechnicLee there) the approach your code is using can be a problem if messages arrive in groups.  The approach I've taken below avoids that problem.  Replace you existing code with this and let's see what happens.
Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
    Dim arrEID As Variant, varEID As Variant, olkItm As Object, objFSO As New FileSystemObject, objMsgFile As TextStream
    On Error Resume Next
    arrEID = Split(EntryIDCollection, ",")
    For Each varEID In arrEID
        Set olkItm = Session.GetItemFromID(varEID)
        If olkItm.Class = olMail Then
            Set objMsgFile = objFSO.CreateTextFile("C:\LicenceEmails\" & Replace(Date, "/", "-") & "@" & Int(Timer) & ".txt")
            objMsgFile.WriteLine "Subject: " & olkItm.Subject
            If olkItm.BodyFormat = olFormatHTML Then
                objMsgFile.WriteLine olkItm.HTMLBody
            Else
                objMsgFile.WriteLine olkItm.Body
            End If
            objMsgFile.Close
            olkItm.UnRead = False
        End If
    Next
    On Error GoTo 0
    Set olkItm = Nothing
    Set objMsgFile = Nothing
    Set objFSO = Nothing
End Sub

Open in new window

Avatar of davidcowler

ASKER

Hi

Have removed all the existing code and replaced with the above but having sent 6 emails in quick succession, only the first one was converted. Is there something else I need to do?

Thanks
You received all 6 but only 1 was written to the file.  Correct?  That's very strange.  Are the items all mail items?  
Yes, that is correct. 6 in, 1 out. Seems to be the first received mail that goes through. All the mails are exactly the same format but different in content.
Are you familiar with debugging in VBA?  If so, set a breakpoint at line #5.  Test the value of arrEID.  It should contain an entry for each message received.  If it does, then step through the code and see what's happening.  Also, do you have any rules that affect the items?  If so, then disable those rules.
I write VBA for Excel a lot, so I'm familiar with the IDE. There are no rules in operation, so I'll set the breakpoint and see what happens. Thanks.
Also, test olkItm after line #6 executes and make sure it's not empty.
Have checked it all and it seems to be populating the variables and objects correctly, but the For loop seems to only run through once. I checked ubound(arrEID) and it's always 0.
What about the value of EntryIDCollection?
Just sent a test mail and the value was 00000000111D8B41BF956B489E05DF0368CBF7B107001CF7980CC3958C448506897D9CFD5D320000005085AC00001CF7980CC3958C448506897D9CFD5D3200000050CB2F0000

Does this seem correct?
Yes.  If multiple items arrive at once, then you should see a string that looks like that for each item.  Each ID should be separated by a comma.
Would it not give an ID per mail? I added a msgbox to display the value but it gives a result similar to what I posted before, with no commas. This obviously halts the code and only the one file gets through. Without the msgbox only some get through. Is it to do with whether the messages are read or unread? Should any of the settings be changed?
oh wait, I see how that works now.... so there WOULD be multiple IDs in a single string, and the code marks the message as read once it has been dealt with.
Correct.
We're on MS Exchange here - would that have a bearing on the problem at all?
ASKER CERTIFIED SOLUTION
Avatar of davidcowler
davidcowler
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry the automated solution didn't work.
Solution not found, so using a completely different method to achieve the desired result.