I am trying to write a Macro inside Outlook 2003. The macro is designed to run through every email inside a selected folder, save each email as a .msg file to a location, then the delete the email from the folder.
See my code below:
Dim sSubject As String Dim FNme As String Dim DirName As String Dim iCount As Integer Dim itm As Object DirName = "C:\Documents and Settings\name\Desktop\folder\" iCount = 1 Set Folder = Outlook.ActiveExplorer.CurrentFolder 'selects the active mailbox For Each itm In Folder.Items On Error GoTo ErrorHandler sSubject = itm.Subject MsgBox ("Email number: " & iCount & " Subject: " & sSubject) FNme = DirName & sSubject & ".msg" itm.SaveAs FNme, olMSG itm.Delete ErrorHandler: MsgBox ("The email " & sSubject & " failed to save.") Exit Sub iCount = iCount + 1 Next MsgBox ("Emails saved")
When I run the macro in debug mode, it looks like the ErrorHandler is being called at the "itm.SaveAs FNme, olMSG" line.
I am wondering if there is anything wrong with my code that could be causing this? Is the SaveAs function being used properly?
Also, the name of the emails I am trying to save often contain symbols such as * $ @ &, could there be certain characters that cannot be used in a file name? However I don't think this is the problem, as I am able to manually save the emails to the same location (File -> Save As..) without problems.
Oh, and I am using Microsoft XP.
Would appreciate any help I can get.