I'm new to VBA and I'm trying to write some code that will look at folder in the inbox, loop through each msg and save it as the subjectname.msg. I get a runtime 91 error when I test the code. Each Set statement = nothing. I checked my references and it looks like all references I need are set, but I continue getting the error. Can someone tell me if there's something wrong with my code?
Here is the code I'm using:
Private Sub SaveMsg()
Dim ns As NameSpace
Dim Inbox As MAPIFolder
Dim Subfolder As MAPIFolder
Dim Item As Object
Dim FileName As String
Dim i As Integer
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
Set Subfolder = Inbox.Folders("J")
Set ns = GetNamespace("MAPI")
i = 0
If Subfolder.Items.Count = 0 Then
MsgBox "There are no messages in the J folder.", vbInformation, "Nothing Found"
Exit Sub
End If
If Subfolder.Items.Count > 0 Then
For Each Item In Subfolder.Items
FileName = "C:\documents and settings\mystic\my documents\my e-mail\" & Item.FileName
Item.SaveAs FileName, olMSG
i = i + 1
Next Item
End If
End Sub
Here is the code I'm using:
Private Sub SaveMsg()
Dim ns As NameSpace
Dim Inbox As MAPIFolder
Dim Subfolder As MAPIFolder
Dim Item As Object
Dim FileName As String
Dim i As Integer
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
Set Subfolder = Inbox.Folders("J")
Set ns = GetNamespace("MAPI")
i = 0
If Subfolder.Items.Count = 0 Then
MsgBox "There are no messages in the J folder.", vbInformation, "Nothing Found"
Exit Sub
End If
If Subfolder.Items.Count > 0 Then
For Each Item In Subfolder.Items
FileName = "C:\documents and settings\mystic\my documents\my e-mail\" & Item.FileName
Item.SaveAs FileName, olMSG
i = i + 1
Next Item
End If
End Sub