VBA code for checking attachments in Outlook

Braveheart

Registered User.
Local time
Today, 03:28
Joined
Jun 11, 2014
Messages
10
Could someone please help me with the below code. I know this is a access forum but im sure the VBA code for access will be similar and the knowledge of people on here will solve this no problem.

I have seen this code on another site and coded it into all the correct places on outlook but i cant get it to work when my signature image is there. The code is ment to deal with the signature but i just wont work. Can anyone help ? as this would be a savior for my business.

Here is the code




Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
If InStr(1, Item.Body, "attach", vbTextCompare) > 0 Then
If Item.Attachments.Count = 0 Then
answer = MsgBox("There's no attachment, send anyway?", vbYesNo)
If answer = vbNo Then Cancel = True
End If

If Item.Attachments.Count > 0 Then
For Each oAtt In Item.Attachments
Debug.Print oAtt.Size
If oAtt.Size < 5200 Then
GoTo NextAtt

Else
answer = MsgBox("There's no attachment, send anyway?", vbYesNo)
If answer = vbNo Then Cancel = True
End If
NextAtt:
Next oAtt

End If
End If
End Sub


This code is basically a failsafe to check for missing attachments before sending your email.




Thanks for any assistance
 
Last edited:
Shot in the dark, but presumably it sees the image as an attachment, so try testing for greater than 1 instead of 0?
 
Shot in the dark, but presumably it sees the image as an attachment, so try testing for greater than 1 instead of 0?

That kind of works. Still a problem though, Now no matter how many attachments i have it always prompts me as if there is no attachment. :banghead:

Thanks for the reply though
 
What is the code now? I'm not sure you need the test of size. I'm a little out of my depth; I've automated Outlook from Access, but never worked from within Outlook.
 
Code looks like this now.

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
If InStr(1, Item.Body, "attach", vbTextCompare) > 1 Then
If Item.Attachments.Count = 0 Then
answer = MsgBox("There's no attachment, send anyway?", vbYesNo)
If answer = vbNo Then Cancel = True
End If

If Item.Attachments.Count > 1 Then
For Each oAtt In Item.Attachments
Debug.Print oAtt.Size
If oAtt.Size < 5200 Then
GoTo NextAtt

Else
answer = MsgBox("There's no attachment, send anyway?", vbYesNo)
If answer = vbNo Then Cancel = True
End If
NextAtt:
Next oAtt

End If
End If
End Sub
 
What if you change this line:

If Item.Attachments.Count = 1 Then
 
What if you change this line:

If Item.Attachments.Count = 1 Then

Tried changing it to the above this morning with no joy. I wonder if Outlook only sees attachments as a size in KB/MB and not how many attachments.
 
Tried changing it to the above this morning with no joy. I wonder if Outlook only sees attachments as a size in KB/MB and not how many attachments.

It knows how many attachments there are in a message.

The code below is part of a sub that I use to save and delete attachments from emails and it will save them all if I need to.

Code:
    Set oAttachments = aMail.Attachments
    iCount = oAttachments.Count
 
It knows how many attachments there are in a message.

The code below is part of a sub that I use to save and delete attachments from emails and it will save them all if I need to.

Code:
    Set oAttachments = aMail.Attachments
    iCount = oAttachments.Count

Thanks for that but unsure as to how it may help me in this instance. Searched the web high and low with no joy. Really frustrating this :banghead:
 
Well I was only confirming that Outlook is aware of the number of attachments in response to your statement.

In regard to the code, could you please explain what it is meant to do?
I am having a hard time putting the If and Endifs together?

Here is my attempt. I always start off small and then add to it.
Code:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
If InStr(1, Item.Body, "attach", vbTextCompare) > 1 Then
	If Item.Attachments.Count = 0 Then
		answer = MsgBox("There's no attachment, send anyway?", vbYesNo)
		If answer = vbNo Then Cancel = True
	Else
		For Each oAtt In Item.Attachments
			Debug.Print oAtt.Size
		Next oAtt		
	End If
		

End If
End Sub
 
The original code is hard to read because it is not indented. Putting indents in, then part of it reads
Code:
For Each oAtt In Item.Attachments
            Debug.Print oAtt.Size
            If oAtt.Size < 5200 Then
                GoTo NextAtt

            Else
                answer = MsgBox("There's no attachment, send anyway?", vbYesNo)
                If answer = vbNo Then Cancel = True
            End If
            NextAtt:
        Next oAtt

This gives the message there is no attachment whenever there is an attachment with size less than 5.2K

If the signature is really being included in the attachments ( I would have thought it being embedded in the html body), then maybe testing the attachments for filetype, filename or fileurl properties might help excluding the signature attachment.
 
The original code is hard to read because it is not indented. Putting indents in, then part of it reads
Code:
For Each oAtt In Item.Attachments
            Debug.Print oAtt.Size
            If oAtt.Size < 5200 Then
                GoTo NextAtt

            Else
                answer = MsgBox("There's no attachment, send anyway?", vbYesNo)
                If answer = vbNo Then Cancel = True
            End If
            NextAtt:
        Next oAtt

This gives the message there is no attachment whenever there is an attachment with size less than 5.2K

If the signature is really being included in the attachments ( I would have thought it being embedded in the html body), then maybe testing the attachments for filetype, filename or fileurl properties might help excluding the signature attachment.

I would have thought that message is only shown when the size is not < 5.2K?
 
Thanks for the responses...

i found this code online and copy and pasted it into outlook. Its designed to search the body of an email for the word "Attached" and if it see it and there is no attachment then it should flag up a warning that there is no attachment found.

It works perfectly when no image is in my signature. I have changed my signature image to a code hosted on the web but still no joy :confused:.

This would really save my staff a lot of time as we send a lot of emails during the working day and sometimes attachments get forgotten.

Really appreciate all the help.
 
See if this would help you out.
We send large attachments as if there is no tomorrow, and it soon fills our mailboxes.
This code saves all attachments to a folder and replaces them with links.
I too got most of this from the net and amended it to suit and it was saving my signature picture all the time at the start of use. So I put in that check for pic types and size and that handles that problem.

So I would try and incorporate the logic here into your code.

HTH
Code:
 For Each aMail In oSelection
  
    ' This code only strips attachments from mail items.
    ' If aMail.class=olMail Then
    ' Get the Attachments collection of the item.
    Set oAttachments = aMail.Attachments
    iCount = oAttachments.Count
      
        
    If iCount > 0 Then
      
        ' We need to use a count down loop for removing items
        ' from a collection. Otherwise, the loop counter gets
        ' confused and only every other item is removed.
          
        For i = iCount To 1 Step -1
            blnSaveFile = True
            ' This code looks at the last 4 characters in a filename
            sFile = oAttachments.Item(i).FileName
            strFileType = LCase$(Right$(sFile, 4))

            Select Case strFileType
            ' Add additional file types below
            Case ".jpg", ".png", ".gif"
                If oAttachments.Item(i).Size < 5200 Then
                    blnSaveFile = False
                End If
            End Select

            If blnSaveFile Then
                ' Save attachment before deleting from item.
                ' Get the file name.
                ' sFile = oAttachments.Item(i).FileName
              
                ' Combine with the path to the Temp folder. Add date and time strings as filenames can be the same
                 strDate = Format(aMail.ReceivedTime, "yyyymmdd")
                 strTime = Format(aMail.ReceivedTime, "hhmmss")
                 sFile = sFolderPath & "\" & strDate & "_" & strTime & "_" & sFile
              
                ' Save the attachment as a file.
                oAttachments.Item(i).SaveAsFile sFile
              
                ' Delete the attachment.
                oAttachments.Item(i).Delete
              
                'write the save as path to a string to add to the message
                'check for html and use html tags in link
                If aMail.BodyFormat <> olFormatHTML Then
                    sDeletedFiles = sDeletedFiles & vbCrLf & "<file://" & sFile & ">"
                Else
                    sDeletedFiles = sDeletedFiles & "<br>" & "<a href='file://" & _
                    sFile & "'>" & sFile & "</a>"
                End If
              End If
                          
        Next i
        'End If
              
       ' Adds the filename string to the message body and save it
       ' Check for HTML body
       If aMail.BodyFormat <> olFormatHTML Then
           aMail.Body = aMail.Body & vbCrLf & _
           "The file(s) were saved to " & sDeletedFiles
       Else
           aMail.HTMLBody = aMail.HTMLBody & "<p>" & _
           "The file(s) were saved to " & sDeletedFiles & "</p>"
       End If
       
       ' Now flag the email so we know which emails had attachments

       aMail.Categories = aMail.Categories & ", Attached"
       aMail.FlagStatus = olFlagMarked
       'aMail.FlagIcon = 6
        
       aMail.Save
       'sets the attachment path to nothing before it moves on to the next message.
       sDeletedFiles = ""
     
       End If
    Next 'end aMail
 

Users who are viewing this thread

Back
Top Bottom