none
Outlook attachments : forward attachment based on file name RRS feed

  • Question

  • hi,

    how to forward a message according to the file name?

    hi receive every day a message that has a structure as follows : 

    - first six caracters, fixed value

    - then the word "giacenze"

    - then the time stamp

    i need to forward this message to an account, how to do it?

    tks, Francesco


    • Edited by Buni75 Monday, June 15, 2015 2:07 PM
    Monday, June 15, 2015 1:52 PM

All replies

  • The message title refers to 'attachments' while the message body refers to the message itself. Are we to assume that we are looking for a message with an attachment that has six characters and the word 'giacenze' in the filename? What then do you want to forward? The entire message or just the attachment?

    If the entire message then the following script associated with a rule to identify the messages should do the job. I have included a macro to test it with a selected message. Change the e-mail address to the required recipient's address:

    Sub SendOnMessage(olItem As MailItem)
    Dim olOutMail As Outlook.MailItem
    Dim olInsp As Outlook.Inspector
    Dim wdDoc As Object
    Dim oRng As Object
    Dim sFname As String
    Dim olAtt As Attachment
    Const sAddr As String = "someone@somewhere.com"        'address to forward to
        With olItem
            If olItem.Attachments.Count > 0 Then
                For Each olAtt In olItem.Attachments
                    If InStr(7, LCase(olAtt.FileName), "giacenze") > 0 Then
                        Set olOutMail = olItem.Forward
                        With olOutMail
                            Set olInsp = .GetInspector
                            Set wdDoc = olInsp.WordEditor
                            Set oRng = wdDoc.Range(0, 0)
                            .Display 'Do not remove this line
                            oRng.Text = "This is the covering message body"
                            .To = sAddr
                            '.Send 'Restore this line after testing
                        End With
    Exit For End If Next olAtt End If End With lbl_Exit: Set olOutMail = Nothing Exit Sub End Sub Sub TestMsg() Dim olMsg As MailItem On Error Resume Next Set olMsg = ActiveExplorer.Selection.Item(1) SendOnMessageB olMsg lbl_Exit: Set olMsg = Nothing Exit Sub End Sub


    Graham Mayor - Word MVP
    www.gmayor.com




    Tuesday, June 16, 2015 4:28 AM
  • Tks Graham, I meant just to send the entire mail.

    but let me write again the process because I tried the script and it didn't work.

    - the sender is always the same

    - object is always "R31529 ESITI" (if neccessary I can change)

    - file will be named as "R31529giacenze + time stamp + .txt"

    - it has to be sent to 2 mail address, is that possibile?

    now I wrote the rule based on sender, object and the script, but something went wrong.

    tks again, Francesco

    Tuesday, June 16, 2015 5:11 AM
  • By 'Object' do you mean 'Subject'?

    Does the incoming mail have an attachment containing the word  giacenze' (which is what the code looks for starting at character 7 of the attachment filename) or did you intend to save the message as a text file and attach that to the message.

    Adding a second recipient is easy enough, just change the line Const sAddr = to add the second address separated with a semi-colon.

    Const sAddr As String = "someone@somewhere.com; someoneelse@somewhere.com"
    Beyond that, if what you originally appeared to describe is correct, the macro should work. If this is not what is required please describe EXACTLY what you have and what you want to do with it.


    Graham Mayor - Word MVP
    www.gmayor.com

    Tuesday, June 16, 2015 8:55 AM

  • I applied the change for the second recipient, but when I execute the macro outlook tells me there's an error.

    I click DEBUG, and the line ".Display 'Do not remove this line" is highlighted in yellow.

    I rewrite better what I receive and what I need :

    - the sender is always the same

    - Subject of the message is always "R31529 ESITI" (if necessary I can change)

    - file attached will be named as "R31529giacenze + time stamp + .txt"

    - it has to be sent to 2 mail address

    tks for your time. Francesco

    Tuesday, June 16, 2015 11:30 AM
  • Replace all the code with the following and change the two e-mail addresses as required. Based on your message that should work. Use the test macro to test it with a typical message that has already been received.

    Option Explicit
    
    Sub SendOnMessage(olItem As MailItem)
    Dim olOutMail As Outlook.MailItem
    Dim olInsp As Outlook.Inspector
    Dim wdDoc As Object
    Dim oRng As Object
    Dim sFname As String
    Dim olAtt As Attachment
    Const sAddr As String = "someone@somewhere.com; someoneelse@somewhere.com"        'address to forward to
        With olItem
            If olItem.Attachments.Count > 0 Then
                For Each olAtt In olItem.Attachments
                    If InStr(1, olAtt.FileName, "R31529giacenze") > 0 Then
                        Set olOutMail = olItem.Forward
                        With olOutMail
                            Set olInsp = .GetInspector
                            Set wdDoc = olInsp.WordEditor
                            Set oRng = wdDoc.Range(0, 0)
                            .Display 'Do not remove this line
                            oRng.Text = "This is the covering message body"
                            .To = sAddr
                            '.Send 'Restore this line after testing
                        End With
                        Exit For
                    End If
                Next olAtt
            End If
        End With
    lbl_Exit:
        Set olOutMail = Nothing
        Exit Sub
    End Sub
    
    Sub TestMsg()
    Dim olMsg As MailItem
        On Error Resume Next
        Set olMsg = ActiveExplorer.Selection.Item(1)
        SendOnMessage olMsg
    lbl_Exit:
        Set olMsg = Nothing
        Exit Sub
    End Sub
    


    Graham Mayor - Word MVP
    www.gmayor.com

    Tuesday, June 16, 2015 12:38 PM
  • sorry Graham, it doesn't work but I don't have debug messages.

    may it be that after R31529giacenza the file name has the time stamp and .txt?

    last file i've received is "R31529giacenze160620151300.txt"

    tks, Francesco

    Tuesday, June 16, 2015 1:54 PM
  • The time stamp and extension shouldn't matter. The code looks for R31529giacenze in the filenames of any attachments i.e.

    If InStr(1, olAtt.FileName, "R31529giacenze") > 0

    If that matches, the code should then create and display a 'forward' message.

    Sometimes the line

    Set oRng = wdDoc.Range(0, 0)

    causes an error, in which case

    Set oRng = wdDoc.Range
    oRng.Collapse 1

    should address it, but the error should have been flagged,


    Graham Mayor - Word MVP
    www.gmayor.com

    Wednesday, June 17, 2015 4:38 AM
  • sorry, it doesn't work. I tried with an email that was in incoming mail.

    this is the script I wrote :

    Option Explicit

    Sub SendOnMessage(olItem As MailItem)
    Dim olOutMail As Outlook.MailItem
    Dim olInsp As Outlook.Inspector
    Dim wdDoc As Object
    Dim oRng As Object
    Dim sFname As String
    Dim olAtt As Attachment
    Const sAddr As String = "francesco.tribuni@gmail.com"        'address to forward to
        With olItem
            If olItem.Attachments.Count > 0 Then
                For Each olAtt In olItem.Attachments
                    If InStr(1, olAtt.FileName, "R31529giacenze") > 0 Then
                        Set olOutMail = olItem.Forward
                        With olOutMail
                            Set olInsp = .GetInspector
                            Set wdDoc = olInsp.WordEditor
                            Set oRng = wdDoc.Range
                            oRng.Collapse 1
                            .Display 'Do not remove this line
                            oRng.Text = "This is the covering message body"
                            .To = sAddr
                            '.Send 'Restore this line after testing
                        End With
                        Exit For
                    End If
                Next olAtt
            End If
        End With
    lbl_Exit:
        Set olOutMail = Nothing
        Exit Sub
    End Sub

    Sub TestMsg()
    Dim olMsg As MailItem
        On Error Resume Next
        Set olMsg = ActiveExplorer.Selection.Item(1)
        SendOnMessage olMsg
    lbl_Exit:
        Set olMsg = Nothing
        Exit Sub
    End Sub

    no debyg messages, but it doesn't work.

    what do you think it could be?

    tks, Francesco


    Wednesday, June 17, 2015 3:46 PM
  • Without seeing the message it is impossible to guess. It should work, and does here when the conditions are as you stated. If you can send me one of the messages (drag and drop it to a new message and send it to supportATgmayor.com, with your forum username in the subject) I may be able to spot the problem.

    Graham Mayor - Word MVP
    www.gmayor.com

    Thursday, June 18, 2015 12:26 PM
  • I think you can use that little bit easily

    Sub SendOnMessage(olItem As MailItem)
    Dim olAtt As Attachment, yest As Boolean, ol_newmail As MailItem
    Const sAddr As String = "francesco.tribuni@gmail.com"
    For Each olAtt In olItem.Attachments
        If InStr(1, olAtt.FileName, "R31529giacenze") > 0 Then yest = True: Exit For
    Next
    If yest = True Then
        Set ol_newmail = olItem.Forward
            ol_newmail.To = sAddr
            ol_newmail.Display '.send
    End If
    End Sub

    You do not have to loop all attachments if you want to ff all massge (not only one attachment from meny) and destroy objects - that is a garbagecollector job.


    Oskar Shon, Office System MVP - www.VBATools.pl
    if Helpful; Answer when a problem solved

    Thursday, June 25, 2015 9:09 AM
    Answerer