Consulting

Results 1 to 6 of 6

Thread: Solved: AutoForward or AutoSave

  1. #1
    VBAX Mentor jammer6_9's Avatar
    Joined
    Apr 2007
    Location
    Saudi Arabia
    Posts
    318
    Location

    Solved: AutoForward or AutoSave

    Hi all,

    Can I make a rule or code to save or forward automatically an specific file to a desired target location when email arrives.

    Target Location is:
    F:\Business Optimization Sales Analysis\DSR Link File

    T-ogether
    E-veryone
    A-chieves
    M-ore


    One who asks a question is a fool for five minutes; one who does not ask a question remains a fool forever.

  2. #2
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    First you need to add this code to the "ThisOutlookSession" module in the Outlook VBE window:

    [vba]
    Sub SaveAttachmentRule(Item As Outlook.MailItem)
    Dim att As Attachment
    Dim strPath As String


    strPath = "F:\Business Optimization Sales Analysis\DSR Link File\"

    For Each att In Item.Attachments
    ' change the name of the attached file you are looking for
    If att.FileName = "Name of attachment file.txt" Then
    att.SaveAsFile strPath & att.FileName
    End If
    Next att
    End Sub
    [/vba]

    I'm assuming that you're looking for a specific attachment, so change "Name of attachment file.txt" to the name of the attached file you are expecting to receive.

    When you setup your rule, you need to check incoming mail. If the mail message has a consistent subject, you can specific the subject to look for, otherwise you may have to look at every message with an attachment.

    The action to perform is "run a script", and you should be able to select "Project1.ThisOutlookSession.SaveAttachmentRule" as the script to run.

  3. #3
    VBAX Mentor jammer6_9's Avatar
    Joined
    Apr 2007
    Location
    Saudi Arabia
    Posts
    318
    Location
    What about if there is a file on that folder having the same file name. How can I replace the existing file to the latest file I have recieved?
    T-ogether
    E-veryone
    A-chieves
    M-ore


    One who asks a question is a fool for five minutes; one who does not ask a question remains a fool forever.

  4. #4
    VBAX Mentor jammer6_9's Avatar
    Joined
    Apr 2007
    Location
    Saudi Arabia
    Posts
    318
    Location
    This what I did so far. I don't know if I did it right. please check over it.
    [vba]
    Sub SaveAttachmentRule(Item As Outlook.MailItem)
    Dim att As Attachment
    Dim strPath As String


    strPath = "F:\Business Optimization Sales Analysis\DSR Link File\"

    For Each att In Item.Attachments
    If att.FileName = "SALES-2007-05.xls" And "Home Delivery Sales 2007.xls" And "4.SALES MAY-2007.xls" Then
    att.SaveAsFile strPath & att.FileName
    End If
    Next att

    End Sub
    Private Sub Application_NewMail()
    Project1.ThisOutlookSession.SaveAttachmentRule ' Compile Error: Argument Not Optional
    End Sub

    [/vba]
    T-ogether
    E-veryone
    A-chieves
    M-ore


    One who asks a question is a fool for five minutes; one who does not ask a question remains a fool forever.

  5. #5
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    [VBA]
    Sub SaveAttachmentRule(Item As Outlook.MailItem)
    Dim att As Attachment
    Dim strPath As String


    strPath = "F:\Business Optimization Sales Analysis\DSR Link File\"

    ' loop through each attachment
    For Each att In Item.Attachments
    Select Case att.FileName
    Case "SALES-2007-05.xls", "Home Delivery Sales 2007.xls", _
    "4.SALES MAY-2007.xls"
    att.SaveAsFile strPath & att.FileName
    End Select
    Next att
    End Sub
    [/VBA]

    To setup the rule, in Outlook, select Tools | Rules and Alerts | New Rule | Start from a blank rule. In Step 1, select "Check messages when they arrive", then Next. Tick the condition "which has an attachment", then Next. Tick the action "run a script".

    In the "Step 2" box, click on "a script" (which is underlined) and select "Project1.ThisOutlookSession.SaveAttachmentRule". Click Next twice, give your rule a name, then click Finish.

  6. #6
    VBAX Mentor jammer6_9's Avatar
    Joined
    Apr 2007
    Location
    Saudi Arabia
    Posts
    318
    Location
    Well I can adjust my time schedule right now. Instead of 8am, I guess I'll be moving it to 10am ... geekgirlau you have just made my life easier. Thanks the code works well...

    Quote Originally Posted by geekgirlau
    [vba]
    Sub SaveAttachmentRule(Item As Outlook.MailItem)
    Dim att As Attachment
    Dim strPath As String


    strPath = "F:\Business Optimization Sales Analysis\DSR Link File\"

    ' loop through each attachment
    For Each att In Item.Attachments
    Select Case att.FileName
    Case "SALES-2007-05.xls", "Home Delivery Sales 2007.xls", _
    "4.SALES MAY-2007.xls"
    att.SaveAsFile strPath & att.FileName
    End Select
    Next att
    End Sub
    [/vba]

    To setup the rule, in Outlook, select Tools | Rules and Alerts | New Rule | Start from a blank rule. In Step 1, select "Check messages when they arrive", then Next. Tick the condition "which has an attachment", then Next. Tick the action "run a script".

    In the "Step 2" box, click on "a script" (which is underlined) and select "Project1.ThisOutlookSession.SaveAttachmentRule". Click Next twice, give your rule a name, then click Finish.
    T-ogether
    E-veryone
    A-chieves
    M-ore


    One who asks a question is a fool for five minutes; one who does not ask a question remains a fool forever.

Posting Permissions

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