Log in

View Full Version : [SOLVED:] Help, trying to move a message after its attachment has been printed and saved.



elivergara
10-13-2015, 03:52 PM
The code below checks incoming mail and if the attachment is a PDF, prints the attachment, marks the message as "read", and saves a copy of that attachment on the computer.
How can I then MOVE that message, into the "Invoices" folder in Outlook? :beg:

Any help will be very appreciated!


Private Declare Function ShellExecute Lib "shell32.dll" Alias _
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Private WithEvents Items As Outlook.Items

Private Sub Application_Startup()
Dim Ns As Outlook.NameSpace
Dim Folder As Outlook.MAPIFolder

Set Ns = Application.GetNamespace("MAPI")
Set Folder = Ns.GetDefaultFolder(olFolderInbox)
Set Items = Folder.Items
End Sub



Private Sub Items_ItemAdd(ByVal Item As Object)
If TypeOf Item Is Outlook.MailItem Then
Item.UnRead = False
PrintAttachments Item

End If

End Sub



Private Sub PrintAttachments(oMail As Outlook.MailItem)
On Error Resume Next
Dim colAtts As Outlook.Attachments
Dim oAtt As Outlook.Attachment
Dim sFile As String
Dim sDirectory As String
Dim sFileType As String

sDirectory = "C:\Attachments\"

Set colAtts = oMail.Attachments

If colAtts.Count Then
For Each oAtt In colAtts

' This code looks at the last 4 characters in a filename
sFileType = LCase$(Right$(oAtt.FileName, 4))

Select Case sFileType

' Add additional file types below
Case ".pdf"

sFile = sDirectory & oAtt.FileName
oAtt.SaveAsFile sFile
ShellExecute 0, "print", sFile, vbNullString, vbNullString, 0
End Select
Next

End If
End Sub

gmayor
10-13-2015, 09:12 PM
Assuming a sub folder of Inbox, Add the line

Item.Move Session.GetDefaultFolder(olFolderInbox).folders("Invoices")after printing the item.

elivergara
10-13-2015, 09:38 PM
Assuming a sub folder of Inbox, Add the line

Item.Move Session.GetDefaultFolder(olFolderInbox).folders("Invoices")after printing the item.

Well, the folder Invoices was not a sub folder of Inbox, and that's where my problem was, but I couldn't see it until you pointed it out. So I moved the folder into the inbox (made it a sub folder of it) and now the code works.

Thanks!!!:clap:

gmayor
10-13-2015, 11:50 PM
It wouldn't have mattered where the folder was positioned, provided you addressed its actual location in the path. Making it a sub folder of Inbox does simplify things. :)