PDA

View Full Version : Use VBA to move an attachment to the Inbox, then open it, then forward it to .....



mbilash
02-19-2020, 03:50 PM
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:



An email arrives from a specified email address with an attachment that contains a specific phrase in the attachment subject line
Move the Attachment to the Inbox (same action as dragging it from the message to the Inbox)
Open the moved message (that was the original attachment)
Forward that message to a specified email address with a specified subject line
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!!

gmayor
02-19-2020, 09:59 PM
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?

mbilash
02-20-2020, 07:57 AM
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?

mbilash
02-20-2020, 02:40 PM
26048
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?

gmayor
02-20-2020, 10:47 PM
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. : pray2: 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

mbilash
02-21-2020, 08:54 AM
Thank you! I ran a test and this is what happened

26058

mbilash
02-21-2020, 09:13 AM
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.

gmayor
02-21-2020, 09:53 PM
Change the line
Dim olMail As MailItemto
Dim olMail As ObjectIf 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.