Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Outlook VBA

Status
Not open for further replies.

chvchk

Programmer
Aug 18, 2004
33
US
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
 
You should instantiate ns before using it:
Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
Set Subfolder = Inbox.Folders("J")


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thank you!

Is there a method to save replies and forwards? I've searched the boards and googled but I'm not having much luck.
I added a counter that adds an incrementing number to the file name so that duplicate subjects are saved without overwriting the previous file. But I'm finding that replies and forwards aren't being saved in the manner I'm expecting. I end up with a file named RE or FW of file type "File" with a size of 0 KB.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top