Consulting

Results 1 to 4 of 4

Thread: Help, trying to move a message after its attachment has been printed and saved.

  1. #1

    Question Help, trying to move a message after its attachment has been printed and saved.

    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?

    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

  2. #2
    Assuming a sub folder of Inbox, Add the line
    Item.Move Session.GetDefaultFolder(olFolderInbox).folders("Invoices")
    after printing the item.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3

    It worked! Thanks!

    Quote Originally Posted by gmayor View Post
    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!!!

  4. #4
    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.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •