Pog
04-20-2015, 02:19 AM
Hi,
When I run this macro, I expect it to check each email in folder "Batch Print" and move the email to another folder depending on whether: I) it has NO attachments, II) It has PDF only attachments, III) It has a mixture of attachments.
Two problems:
A) I have four test emails in the folder: "No Attachments", "PDF Only", "Excel Only", "Both Types". When I run the macro it only processed the last two. When I ran it again, it processed the first one only. Then I ran it a third time and it processed the second item. Why doesn't it process all four?
B) It thinks that the email with a PDF attachment has three other attachments (one .png and two .jpg's) - I cannot see these attachments! They are not visible! I didn't attach them. Likewise the one with no attachments, it thinks it has three (the same three as just stated)
Can you help please? In summary: Why doesn't the loop process ALL items in the folder? And where are these invisible attachments coming from??
Thanks.
Sub SortFolder()
Dim olNs As Outlook.NameSpace
Dim olMailFolder As Outlook.MAPIFolder
Dim olItems As Outlook.Items
Dim olMailItem As Outlook.MailItem
Dim olAttach As Attachment
Dim NonPDFFlag As Boolean
On Error GoTo err_Handler
Set olNs = GetNamespace("MAPI")
Set myDestFolder = olNs.GetDefaultFolder(olFolderInbox).Folders("For Investigation")
Set NoAttachFolder = olNs.GetDefaultFolder(olFolderInbox).Folders("No Attachments")
Set PDFOnlyFolder = olNs.GetDefaultFolder(olFolderInbox).Folders("PDF Only")
Set olMailFolder = olNs.GetDefaultFolder(olFolderInbox).Folders("Batch Prints")
Set olItems = olMailFolder.Items
For Each olMailItem In olItems
If olMailItem.Attachments.Count > 0 Then
For Each olAttach In olMailItem.Attachments
If LCase(olAttach.FileName) Like "*.pdf" Then
Else ' **
NonPDFFlag = True
End If
Next olAttach
If NonPDFFlag = True Then ' **
olMailItem.Move myDestFolder ' ** 'For investigation
NonPDFFlag = False ' **
Else
olMailItem.Move PDFOnlyFolder
End If
Else
olMailItem.Move NoAttachFolder ' **
End If
DoEvents
Next olMailItem
err_Handler:
Set olNs = Nothing
Set olMailFolder = Nothing
Set olItems = Nothing
Set olMailItem = Nothing
lbl_Exit:
Exit Sub
End Sub
When I run this macro, I expect it to check each email in folder "Batch Print" and move the email to another folder depending on whether: I) it has NO attachments, II) It has PDF only attachments, III) It has a mixture of attachments.
Two problems:
A) I have four test emails in the folder: "No Attachments", "PDF Only", "Excel Only", "Both Types". When I run the macro it only processed the last two. When I ran it again, it processed the first one only. Then I ran it a third time and it processed the second item. Why doesn't it process all four?
B) It thinks that the email with a PDF attachment has three other attachments (one .png and two .jpg's) - I cannot see these attachments! They are not visible! I didn't attach them. Likewise the one with no attachments, it thinks it has three (the same three as just stated)
Can you help please? In summary: Why doesn't the loop process ALL items in the folder? And where are these invisible attachments coming from??
Thanks.
Sub SortFolder()
Dim olNs As Outlook.NameSpace
Dim olMailFolder As Outlook.MAPIFolder
Dim olItems As Outlook.Items
Dim olMailItem As Outlook.MailItem
Dim olAttach As Attachment
Dim NonPDFFlag As Boolean
On Error GoTo err_Handler
Set olNs = GetNamespace("MAPI")
Set myDestFolder = olNs.GetDefaultFolder(olFolderInbox).Folders("For Investigation")
Set NoAttachFolder = olNs.GetDefaultFolder(olFolderInbox).Folders("No Attachments")
Set PDFOnlyFolder = olNs.GetDefaultFolder(olFolderInbox).Folders("PDF Only")
Set olMailFolder = olNs.GetDefaultFolder(olFolderInbox).Folders("Batch Prints")
Set olItems = olMailFolder.Items
For Each olMailItem In olItems
If olMailItem.Attachments.Count > 0 Then
For Each olAttach In olMailItem.Attachments
If LCase(olAttach.FileName) Like "*.pdf" Then
Else ' **
NonPDFFlag = True
End If
Next olAttach
If NonPDFFlag = True Then ' **
olMailItem.Move myDestFolder ' ** 'For investigation
NonPDFFlag = False ' **
Else
olMailItem.Move PDFOnlyFolder
End If
Else
olMailItem.Move NoAttachFolder ' **
End If
DoEvents
Next olMailItem
err_Handler:
Set olNs = Nothing
Set olMailFolder = Nothing
Set olItems = Nothing
Set olMailItem = Nothing
lbl_Exit:
Exit Sub
End Sub