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
Private Sub Application_Quit()
' disassociate global objects declared WithEvents
Set objInboxItems = Nothing
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
olMailItem.UnRead = False