0

So here's an interesting problem I stumbled upon on. I’m running into issues by sending emails out to SpiceWorks and Mac users.

When a user has a problem they will email Help Desk. We setup a personal Outlook email to handle Help Desk tickets. Once the ticket hits the outlook mailbox it will automatically be sent to our SpiceWorks site.

Now all of our emails have signatures and there are certain signatures with small png image logos (Youtube, LinkedIn, Facebook, and Twitter). When the email hits SpiceWorks it uploads those png images as attachments. These attachments cause most of the problems because some email threads get very long before they even get submitted as an help desk ticket. They would end up with maybe 20+ attachments of the same four logo png's.

I coded to remove all attachments to that specific address but some users send actual attachments. I tried remove the specific attachments by name but if there are duplicates of same .png image they would just iterate. (img001 through img004 is now img005 through img009)

I found the current VBA script in the HelpDesk Outlook. I was told that Outlook has to be running all the time in order for it to work... sometimes.

I started writing my own script where it checks if the current email is going to HelpDesk email address then remove the attachemnts. No luck yet.

Current Code

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim msg As Outlook.MailItem
Dim recips As Outlook.Recipients
Dim str As String
Dim emailAddress As String
Dim prompt As String

Dim msgbody As String
msgbody = Item.Body   

  Set msg = Item 'Subject Message
  Set recips = msg.Recipients

  str = "HelpDesk"


  For x = 1 To GetRecipientsCount(recips)
    str1 = recips(x)
    If str1 = str Then
      'MsgBox str1, vbOKOnly, str1 'For Testing

      prompt = "Are you sure you want to send to " & str1 & "?" 'For Testing

      If MsgBox(prompt, vbYesNo + vbQuestion, "Sample") = vbNo Then 'For Testing
        Cancel = True
      End If

      'if attachments are there
    If Item.Attachments.Count > 0 Then

        'for all attachments
        For i = Item.Attachments.Count To 1 Step -1  

            'if the attachment's filename is similar to "image###.png", remove
            If InStr(Item.Attachments(i).FileName, "image") > 0 And Right(Item.Attachments(i).FileName, 4) = ".png" Then
                MsgBox ("Item Removed " + Item.Attachments(i))
                Item.Attachments.Remove (i)
            End If

        Next
    End If   

    End If
  Next x
End Sub

Public Function GetRecipientsCount(Itm As Variant) As Long
' pass in a qualifying item, or a Recipients Collection
Dim obj As Object
Dim recips As Outlook.Recipients
Dim types() As String

  types = Split("MailItem, AppointmentItem, JournalItem, MeetingItem, TaskItem", ",")

  Select Case True
    ' these items have a Recipients collection
    Case UBound(Filter(types, TypeName(Itm))) > -1
      Set obj = Itm
      Set recips = obj.Recipients
    Case TypeName(Itm) = "Recipients"
      Set recips = Itm
  End Select

  GetRecipientsCount = recips.Count
End Function

A few questions:

1.) Is there a way to set rules in outlook(Looked at numerous possibilities) or do something with the Exchange Server to stop this from happening?

2.) With Vba is there a way to remove or not allow a signature when the email is sent?

If anything, my ultimate goal is just to prevent those .png's being uploaded as images to Mac users and SpiceWorks.

I'm sure there is more to this but I will gladly answer any questions given to me.

Thank you for any help or directions!

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
JayBec
  • 45
  • 2
  • 10

1 Answers1

1

If I understand you correctly, you're trying to remove .png files being sent to SpiceWorks. If so, use the macro below from the Outlook mailbox sending to SpiceWorks. On the ItemSend event, this will check the filename of all attachments and remove those with .png extensions. If this is not what you're trying to do, post back here. Thanks.

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

    'if attachments are there
    If Item.Attachments.count > 0 Then

        'for all attachments
        For i = Item.Attachments.count To 1 Step -1

            'if the attachment's extension is .png, remove
            If Right(Item.Attachments(i).FileName, 4) = ".png" Then
                Item.Attachments.Remove (i)
            End If
        Next
    End If
End Sub

----- updated to only remove attachments that look like "image###.png" -----

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

    'if attachments are there
    If Item.Attachments.count > 0 Then

        'for all attachments
        For i = Item.Attachments.count To 1 Step -1

            'if the attachment's filename is similar to "image###.png", remove
            If InStr(Item.Attachments(i).FileName, "image") > 0 And Right(Item.Attachments(i).FileName, 4) = ".png" Then
                Item.Attachments.Remove (i)
            End If

        Next
    End If
End Sub

----- updated to only remove attachments that are <10kb and look like "image###.png"-----

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

    'if attachments are there
    If Item.Attachments.count > 0 Then

        'for all attachments
        For i = Item.Attachments.count To 1 Step -1

            'if attachment size is less than 10kb
            If Item.Attachments(i).Size < 10000 Then
                'if the attachment's filename is similar to "image###.png", remove
                If InStr(Item.Attachments(i).FileName, "image") > 0 And Right(Item.Attachments(i).FileName, 4) = ".png" Then
                    Item.Attachments.Remove (i)
                End If
            End If
        Next
    End If
End Sub
phillip3196772
  • 102
  • 1
  • 9
  • This does work to an extent. The problem is that users like to use the snip tool and upload images (most are .png) of their errors so this would completely remove all attachments with .png's. – JayBec Feb 24 '14 at 14:49
  • See updated code above. This solution will only remove attachments that look like "image###.png". – phillip3196772 Feb 24 '14 at 15:46
  • 1
    Also, if you know the range of filesizes that you'd like to filter out, you can use `Item.Attachments(i).Size` in your conditional statement to remove images as well. – phillip3196772 Feb 24 '14 at 15:58
  • I tried your new code and it removes all .png's in the body text and any images that have the word "image" in the attachments bar. (Thanks Btw!!! Almost there :) ) If im reading right it loops through all images and if they contain the word "image" and ".png" on the right remove it. If that is correct why is it removing non "image###.png" names? – JayBec Feb 24 '14 at 17:02
  • Ah I figured it out! Any image that is a ".png" seems to default to "image###.png" That's why they all get deleted. Is there anything that can prevent the renaming of images inside the body text? – JayBec Feb 24 '14 at 17:33
  • I also found out that the `Item.Attachments(i).Size` only works for things that were attached with the outlook ribbon. It doesn't work for the images that are copied/pasted into the body text – JayBec Feb 24 '14 at 18:50
  • According to the link below, this is the built-in functionality of Outlook. http://answers.microsoft.com/en-us/mac/forum/macoffice2011-macoutlook/why-does-outlook-2011-convert-embedded-jpegs-into/b42dba11-8da0-40bb-beb7-6c81136b6568 – phillip3196772 Feb 24 '14 at 18:56
  • `Item.Attachments(i).Size` worked to find inline images pasted into the email body. I've also updated to code to search for only images that are less than 10kb in size. – phillip3196772 Feb 24 '14 at 19:13
  • It works for forwarding messages but not reply/sending. I did some debugging and all the images in the body come back with a size of 0? – JayBec Feb 26 '14 at 14:35
  • To get the file size, first run Item.save, then check the file size. – phillip3196772 Feb 26 '14 at 17:08