Consulting

Results 1 to 8 of 8

Thread: Use VBA to move an attachment to the Inbox, then open it, then forward it to .....

  1. #1
    VBAX Newbie
    Joined
    Feb 2020
    Posts
    5
    Location

    Use VBA to move an attachment to the Inbox, then open it, then forward it to .....

    Use VBA to move an attachment to the Inbox, then open it, then forward it to a specified address, then move it and the original message to a folder.


    New to the Forum - apologies in advance if I break a rule!

    I have a somewhat unique situation that I would love some help with. Here are some details:

    Software: Outlook 2013, 2016, and 2019

    I would like to achieve the following:


    1. An email arrives from a specified email address with an attachment that contains a specific phrase in the attachment subject line
    2. Move the Attachment to the Inbox (same action as dragging it from the message to the Inbox)
    3. Open the moved message (that was the original attachment)
    4. Forward that message to a specified email address with a specified subject line
    5. Move the original message and attachment message that was moved to the inbox both into a specified folder


    The order and action of the steps are critical.

    Is this possible?

    Thanks!!

  2. #2
    While you can achieve the same ends with a rule and VBA you would not do so by duplicating the manual steps as described. In order to do that you would create a rule that identifies the incoming messages. However you would need to describe the attachment in order to process the correct attachment as Outlook messages contain elements VBA considers attachments but which would not be the attachment in question e.g. a graphic in a signature, emoticons etc. Attachments don't have subject lines. They are a feature of messages, so what do you mean by 'that contains a specific phrase in the attachment subject line'? What is the file format of the required attachment?
    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
    VBAX Newbie
    Joined
    Feb 2020
    Posts
    5
    Location
    Quote Originally Posted by gmayor View Post
    While you can achieve the same ends with a rule and VBA you would not do so by duplicating the manual steps as described. In order to do that you would create a rule that identifies the incoming messages. However you would need to describe the attachment in order to process the correct attachment as Outlook messages contain elements VBA considers attachments but which would not be the attachment in question e.g. a graphic in a signature, emoticons etc. Attachments don't have subject lines. They are a feature of messages, so what do you mean by 'that contains a specific phrase in the attachment subject line'? What is the file format of the required attachment?
    Thanks!

    - The File Type is a .msg that contains a .ics.
    - The "specific phrase" would simply be a set of words we control that could be very unique so that the attachment was easily identifiable

    There is a 3rd party software that I am using that generates this attachment and sends it to me using a server email address. It sends the attachment (.msg with .ics) as if it was generated/sent by me. Outlook detects this and when I manually open this .msg, it sees it as an appointment and says I don't need to accept it because I am the organizer. The problem is that it doesn't show up in my calendar. After a various tests, I found that if (and only if) I drag the the .msg attachment into my inbox, open it, and press forward, outlook immediately puts it in my calendar. Ultimately, that is what I want to achieve: the appointment appearing in my calendar

    Does that make sense?
    Last edited by mbilash; 02-20-2020 at 08:14 AM.

  4. #4
    VBAX Newbie
    Joined
    Feb 2020
    Posts
    5
    Location
    Capture.JPG
    Quote Originally Posted by mbilash View Post
    Thanks!

    - The File Type is a .msg that contains a .ics.
    - The "specific phrase" would simply be a set of words we control that could be very unique so that the attachment was easily identifiable

    There is a 3rd party software that I am using that generates this attachment and sends it to me using a server email address. It sends the attachment (.msg with .ics) as if it was generated/sent by me. Outlook detects this and when I manually open this .msg, it sees it as an appointment and says I don't need to accept it because I am the organizer. The problem is that it doesn't show up in my calendar. After a various tests, I found that if (and only if) I drag the the .msg attachment into my inbox, open it, and press forward, outlook immediately puts it in my calendar. Ultimately, that is what I want to achieve: the appointment appearing in my calendar

    Does that make sense?

  5. #5
    While it makes sense now, it is not so easy to achieve, however the following may work. It extracts the attachment to a temporary message file on the local hard drive, then sends that file to the recipient of the original message (presumably you) whereupon it should arrive back in your inbox. Unfortunately I cannot find a way to skip the sending.

    Select a message with the attachment and run the test macro and let's see what happens. If it works you can run the main macro from a script associated with a rule that identifies the original message.

    Option Explicit
    
    Sub TestMacro()
    Dim olMsg As MailItem
        'On Error Resume Next
        Set olMsg = ActiveExplorer.Selection.Item(1)
        SaveAttachment olMsg
    lbl_Exit:
        Exit Sub
    End Sub
    
    Sub SaveAttachment(Item As Outlook.MailItem)
    Dim olAtt As Attachment
    Dim olMail As MailItem
    Dim FSO As Object
    Dim tmpPath As String, strName As String
        Set FSO = CreateObject("Scripting.FileSystemObject")
        tmpPath = FSO.GetSpecialFolder(2)
        If Item.Attachments.Count > 0 Then
            For Each olAtt In Item.Attachments
                If olAtt.Type = 5 Then
                    strName = olAtt.fileName
                    MsgBox tmpPath & "\" & strName
                    olAtt.SaveAsFile tmpPath & "\" & strName
                    Set olMail = Session.OpenSharedItem(tmpPath & "\" & strName)
                    With olMail
                        .Display
                        .To = Item.To
                        .Send
                    End With
                    Kill tmpPath & "\" & strName
                    Exit For
                End If
            Next olAtt
        End If
    lbl_Exit:
        Set olAtt = Nothing
        Set olMail = Nothing
        Set FSO = Nothing
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  6. #6
    VBAX Newbie
    Joined
    Feb 2020
    Posts
    5
    Location
    Thank you! I ran a test and this is what happened

    Capture.JPG

  7. #7
    VBAX Newbie
    Joined
    Feb 2020
    Posts
    5
    Location
    I just thought of another detail that may make this more challenging

    I would need this to work whether or not my outlook or computer is open or on. I am using a Microsoft Exchange Server to host my email.

  8. #8
    Change the line
    Dim olMail As MailItem
    to
    Dim olMail As Object
    If it now works delete the message box line which is there only for testing.
    This is an Outlook Macro. It can only work when Outlook is running.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Posting Permissions

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