none
MS Outlook VBA Script to Print an PDF attachment, and Move the mail to a folder RRS feed

  • Question

  • Hi.

    I use a VBA script on the inbox to Automatic save an
    PDF attachment to a folder (receiving invoices as PDF)

    this script runs on a rule, when reciving mails

    After saving the attachment, I want the script to move
    the mail to a specified folder ("saved PDF") but I can't figure out
    how to.

    The script I use are as follows:

    Sub SaveAttachments(myMail As MailItem)
    Dim vFrom As String, vSubject As String
    Dim vFile As Attachment
     
    vFrom = myMail.ReceivedByName
    vSubject = myMail.Subject
     
    If myMail.Attachments.Count > 0 Then
            For i = 1 To myMail.Attachments.Count
                Set vFile = myMail.Attachments(i)
                If LCase(vFile.FileName) Like "*.pdf" Then
                vFile.SaveAsFile "C:\Test\" & vFile.FileName
                End If
            Next i
    End If

    Set myMail = Nothing
    Set vFile = Nothing
    End Sub



    Friday, July 26, 2013 1:25 PM

All replies

  • set myMail = myMail.Move(SomeOtherFolder)

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Friday, July 26, 2013 2:05 PM
  • Thanks

    I tried that one, but I'm not sure how to Direct it to the correct folder.

    I get no errormessage, but the mail is not moved to the folder "Saved PDF" , I belive it is because, I not have defined the path right, may be..

    Monday, July 29, 2013 9:46 AM
  • Thanks

    I tried that one, but I'm not sure how to Direct it to the correct folder.

    I get no errormessage, but the mail is not moved to the folder "Saved PDF" , I belive it is because, I not have defined the path right, may be..

    My code now looks like this:

    Sub SaveAttachments(myMail As MailItem)
    Dim vFrom As String, vSubject As String
    Dim vFile As Attachment
    Dim MyDestFolder As Outlook.Folder
    Dim MyNameSpace As Outlook.NameSpace
    Dim MyInbox As Outlook.Folder

    vFrom = myMail.ReceivedByName
    vSubject = myMail.Subject

    If myMail.Attachments.Count > 0 Then
            For i = 1 To myMail.Attachments.Count
               Set vFile = myMail.Attachments(i)
                If LCase(vFile.FileName) Like "*.pdf" Then
                 vFile.SaveAsFile "Z:\Test\" & vFile.FileName
                 myMail.UnRead = False
                 Set MyNameSpace = Application.GetNamespace("MAPI")
                 Set MyInbox = MyNameSpace.GetDefaultFolder(olFolderInbox)
                 Set MyDestFolder = MyInbox.Folders("Lagret_PDF")
                 myMail.Move (MyDestFolder)
                End If
            Next i
    End If

    Set myMail = Nothing
    Set vFile = Nothing
    End Sub

    Monday, July 29, 2013 10:29 AM
  • Is "Lagret_PDF" a subfolder of Inbox?

    What happens when you step through that code?

    Do you have "on error resume next" anywhere in your code?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Monday, July 29, 2013 1:38 PM