|
|
Question : Macro to save e-mails to a file system location
|
|
I have an Outlook macro (the code is taken from accepted solution in EE question: http://www.experts-exchange.com/Applications/MS_Office/Outlook/Q_21528647.html - and I followed all the steps exactly) but I am unable to find what is causing the error.
The error thrown is shown next to the relevant line below, and shows as an "Error 13 - Type Mismatch."
Can someone please help me? Full points on offer. The question referred to above contained specific requirements, but I simply need a macro to save the e-mails to one single folder, so some of the macro below may be irrelevant for my purposes. I am using Outlook 2002.
Many Thanks.
The code is as follows: 'Macro Begins Here 'Change the path on the next line as needed Const ROOT_FOLDER_PATH = "C:\eeTesting\" Private WithEvents objInboxItems As Items
Private Sub Application_Startup() Dim objItem As MailItem Set objInboxItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items For Each objItem In objInboxItems '<<<---------------@@@@@@@#######@@@#####ERROR LINE If objItem.UnRead And objItem.Class = olMail Then objInboxItems_ItemAdd objItem End If Next End Sub
Private Sub Application_Quit() Set objInboxItems = Nothing End Sub
Private Sub objInboxItems_ItemAdd(ByVal Item As Object) Dim objFSO As Object, _ strYear As String, _ strFolder As String strYear = Left(Item.Subject, 2) strFolder = Mid(Item.Subject, 3, 4) If IsNumeric(strYear) And IsNumeric(strFolder) Then Set objFSO = CreateObject("Scripting.FileSystemObject") If Not objFSO.FolderExists(ROOT_FOLDER_PATH & strYear) Then objFSO.CreateFolder ROOT_FOLDER_PATH & strYear End If If Not objFSO.FolderExists(ROOT_FOLDER_PATH & strYear & "\" & strFolder) Then objFSO.CreateFolder ROOT_FOLDER_PATH & strYear & "\" & strFolder End If Item.SaveAs ROOT_FOLDER_PATH & strYear & "\" & strFolder & "\" & Item.Subject & ".txt", olTXT Item.UnRead = False Item.Save End If Set objFSO = Nothing End Sub 'Macro Ends Here
|
Answer : Macro to save e-mails to a file system location
|
|
Hi, Sigh_Man.
Sorry to be slow responding. Even though the error is occurring on this line
For Each objItem In objInboxItems
the problem is really this line
Dim objItem As MailItem
Type mismatches occur when a variable is declared as one type and then is asked to hold data of another type. objItem is declared as being a MailItem but in the processing of the items in the Inbox it is encountering a meeting request, read receipt, or some other type of item. That's a problem. The way around this is to declare objItem as a generic object. To do that we simply change the declaration to
Dim objItem as Object
That should do it.
|
|
|
|
|