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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting Items out of a Outlook Subfolder

Status
Not open for further replies.

klornpallier

Technical User
Aug 28, 2002
98
GB
I have code that saves outlook attachments to network locations based on the attachment filname. The code for this works fine but I need the delete all the items out of the Subfolder afterwards. The code is below:

' Declare variables
Dim ns As NameSpace
Dim Inbox As MAPIFolder
Dim SubFolder As MAPIFolder
Dim Item As Object
Dim Atmt As Attachment
Dim FileName As String
Dim PhaseNo As String
Dim Slash As String
Dim Trust As String
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim varResponse As VbMsgBoxResult
'Allocate Folders
Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
Set SubFolder = Inbox.Folders("Migration Reports") ' Enter correct subfolder name.

i = 0
' Check subfolder for messages and exit of none found
If SubFolder.Items.Count = 0 Then
MsgBox "There are no messages in the Migration Reports folder.", vbInformation, _
"Nothing Found"
Exit Sub
End If
' Check each message for attachments
For Each Item In SubFolder.Items
For Each Atmt In Item.Attachments
' Check filename of each attachment and save if it has "xls" extension

If Mid(Atmt.FileName, 5, 3) = "CER" Then
Trust = Mid(Atmt.FileName, 10, 3)
k = k + 1

ElseIf Mid(Atmt.FileName, 4, 1) = "_" Then
Trust = Left(Atmt.FileName, 3)
i = i + 1
End If

If Trust = "388" Or Trust = "545" Or Trust = "631"
Or Trust = "504" Or Trust = "775" Then
PhaseNo = "4"
End If

Slash = "\"
' This path must exist! Change folder name as necessary.
FileName = "\\isesrfiler\NHS_HR-Pay\Data Migration\Data Management\DM Testing\Main Pilot\Phase " & PhaseNo & Slash & Trust & Slash & Atmt.FileName

Atmt.SaveAsFile FileName

Next Atmt
Next Item

I've tried inserting Item.Delete after Next Atmt & it only deletes some of the mails. Help?
 
Deleting an item of a collection browsed with a For Each loop is risky.
I suggest to browse the SubFolder.Items from last to first with an indice.


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top