Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

for each email in selection - manipulate category and flagstatus help

7 views
Skip to first unread message

Matthew Dyer

unread,
Nov 23, 2016, 10:14:05 AM11/23/16
to
I'm using the following code to loop through selected emails, re-categorize them as 'Matt', then print only PDF attachments and then only update those e-mails that had pdf attachments with FlagStatus = olFlagComplete. My problem is that the category and flagstatus is only changing for the very top-most item in my selection, and not for each selected item (category) or for the emails with pdf files (flagstatus). Can someone please let me know what I'm doing wrong here?

Public Sub SaveandPrintPDFAttachments()
Dim objOL As Outlook.Application
Dim objMsg As Outlook.MailItem
Dim objAttachments As Outlook.Attachments
Dim objSelection As Outlook.Selection
Dim i As Long
Dim lngCount As Long
Dim strFile As String
Dim strFolderpath As String
Dim strExePath As String

' Get the path to your My Documents folder
strFolderpath = "c:\temp\"


Set objOL = CreateObject("Outlook.Application")
Set objSelection = objOL.ActiveExplorer.Selection

' Set the Attachment folder. (Folder must exist.)
strFolderpath = strFolderpath

For Each objMsg In objSelection
objMsg.Categories = "Matt"
Set objAttachments = objMsg.Attachments
lngCount = objAttachments.Count
If lngCount > 0 Then

For i = lngCount To 1 Step -1
strFile = objAttachments.Item(i).FileName
If Right(strFile, 3) = "pdf" Then
strFile = strFolderpath & strFile
objAttachments.Item(i).SaveAsFile strFile
'use ShellExecute to open the file
'this may not work with zip extension if you use Compressed folders
ShellExecute 0, "print", strFile, vbNullString, vbNullString, 0
objMsg.FlagStatus = olFlagComplete

End If
Next
End If
Next

ExitSub:

Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing
End Sub
0 new messages