davidcowler
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.
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
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
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?
ASKER
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.
ASKER
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.
ASKER
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?
ASKER
Just sent a test mail and the value was 00000000111D8B41BF956B489E 05DF0368CB F7B107001C F7980CC395 8C44850689 7D9CFD5D32 0000005085 AC00001CF7 980CC3958C 448506897D 9CFD5D3200 000050CB2F 0000
Does this seem correct?
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.
ASKER
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?
ASKER
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.
ASKER
We're on MS Exchange here - would that have a bearing on the problem at all?
No.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry the automated solution didn't work.
ASKER
Solution not found, so using a completely different method to achieve the desired result.
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.
Open in new window