-1

I have a tendency to, out of habit, send firmware files over email and new policy demands we do otherwise.

How can I generate a pop-up message in Outlook to remind me to place these files on the network rather than sending them through email, based on file extension (.S usually)?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
audiFanatic
  • 2,296
  • 8
  • 40
  • 56
  • 1
    That's a rather broad question... I figure the answer you're looking for isn't `If MsgBox("It looks like you're attaching a .s file, would you like me to move this file to a network drive instead?", vbYesNo) = vbYes Then ...`, right? Could a more specific question be along the lines of "How do I handle the `MailItem.AttachmentAdd` event"? – Mathieu Guindon Apr 06 '18 at 16:54
  • @MathieuGuindon It is not a broad question, it is very clear what he wants – Ibo Apr 06 '18 at 17:00
  • 4
    @Ibo indeed, it's clear - hence *too broad* as opposed to *unclear what you're asking*. And how is it any different than a 1-rep user asking "I need some code that does XYZ, can someone write me a blog post tutorial with working code I can just copy-paste and run with"? – Mathieu Guindon Apr 06 '18 at 17:02
  • @Mathieu, the first snippet is exactly what I want.. I'm a C coder, not a VBA coder, so I have no idea how to accomplish this. If I click "yes" it'll send the message, if I click No, it won't send. Simple – audiFanatic Apr 06 '18 at 17:03
  • @audiFanatic see, I was under the impression that the stumbling part was handling the `MailItem.AttachmentAdd` event, since `MsgBox` isn't typically where people hit a wall. Editing your post to show what you've got so far would help clarify that =) – Mathieu Guindon Apr 06 '18 at 17:05
  • I've got... Nothing. Which is why it's not there lol – audiFanatic Apr 06 '18 at 17:29
  • 3
    I recommend reading [*Why is “Can someone help me?” not an actual question?*](https://meta.stackoverflow.com/q/284236/1188513) on meta... you'll want to do a bit of research - [here](https://stackoverflow.com/q/15476986/1188513) would be a good starting point. – Mathieu Guindon Apr 06 '18 at 17:51

1 Answers1

0

The challenge here is getting the handle of the new item (untitled email) while using the Outlook UI rather than creating a new item via VBA. You need to first set the inspectors collection to a user-defined object, which will eventually contain the parent (new inspector) of our new item (untitled email). That can be done by an application level event such as Startup.

Then, we can use a NewInspector event to see if the new inspector contains a new message or not; if so, we set it to a module level MailItem object that we have defined on top.

Now, we are set to use a BeforeAttachment event to check the extension of the file that is being attached, if the extension is a banned extension, it will prompt a message and will cancel attaching.

You can still improve this by making extension comparison better and more accurate or copying the file with banned extensions to the location you want without making you to do that manually or even opening the folder you need to place the files there using windows explorer.

to place the code: ALT + F11, double click on ThisOutlookSession, paste the code and CTRL + S to save

I hope this helped! :)

Option Explicit

Dim WithEvents myItem As Outlook.MailItem
Private WithEvents myOlInspectors As Outlook.Inspectors

Private Sub Application_Startup()
    Set myOlInspectors = Application.Inspectors
End Sub


Private Sub myOlInspectors_NewInspector(ByVal Inspector As Outlook.Inspector)
    Dim msg As Outlook.MailItem
    If Inspector.CurrentItem.Class = olMail Then
        Set msg = Inspector.CurrentItem

        If msg.Size = 0 Then
            'MsgBox "New message" 'uncomment to test this routine
            Set myItem = msg
        End If
    End If
End Sub

Private Sub myItem_BeforeAttachmentAdd(ByVal myAttachment As Attachment, Cancel As Boolean)
        Dim sExtension As String
        Dim sBannedExtension As String
        Dim arr As Variant

        sBannedExtension = "xlsx,frm,docx,jpg,png"

        arr = Split(myAttachment.FileName, ".")
        sExtension = arr(UBound(arr))

        If InStr(UCase(sBannedExtension), UCase(sExtension)) > 0 Then
            MsgBox "Sorry, you cannot send a file with a(n)" & sExtension & " extension as an attachment according to the new policy."
            Cancel = True
        End If
End Sub
Ibo
  • 4,081
  • 6
  • 45
  • 65
  • @0m3r I am exactly doing the same thing,I have tested the file and it is working perfectly, thanks for the link tho – Ibo Apr 08 '18 at 17:05
  • @0m3r I edited the code as you said, but I never got an error before anyway – Ibo Apr 09 '18 at 15:29
  • The edited code works perfectly on multiple office application 1+ – 0m3r Apr 09 '18 at 20:46