Consulting

Results 1 to 5 of 5

Thread: Check for attachment before sending a Email from Outlook!!Help!!

  1. #1

    Check for attachment before sending a Email from Outlook!!Help!!

    I triuse this code in outlook to check for unfilled subject before sending a mail. i wanted to try to improve it with attachments too but it is not working. the code that i use is below:
    [vba]Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    Dim strSubject As String
    Dim strAttach As String
    strSubject = Item.Subject
    'strAttach = Item.Attachment.Count
    'If strAttach = 0 Then
    ' Prompt$ = "are there no attachment to this mail"
    ' If MsgBox(Prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check for Subject") = vbNo Then
    ' Cancel = True
    ' End If
    'End If
    If Len(Trim(strSubject)) = 0 Then
    Prompt$ = "Subject is Empty. Are you sure you want to send the Mail?"
    If MsgBox(Prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check for Subject") = vbNo Then
    Cancel = True
    End If
    End If
    End Sub[/vba]

    Can any one suggest me a solution

  2. #2
    VBAX Mentor skatonni's Avatar
    Joined
    Jun 2006
    Posts
    347
    Location
    Try this.

    [vba]
    Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    Dim strSubject As String
    Dim lngAttach As Long
    Dim strPrompt As String
    strSubject = Item.Subject
    lngAttach = Item.Attachments.count
    If lngAttach = 0 Then
    strPrompt = "are there no attachment to this mail - Yes we have no attachments today"
    If MsgBox(strPrompt, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check for Attachment") = vbNo Then
    Cancel = True
    End If
    End If
    If Len(Trim(strSubject)) = 0 Then
    strPrompt = "Subject is Empty. Are you sure you want to send the Mail?"
    If MsgBox(strPrompt, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check for Subject") = vbNo Then
    Cancel = True
    End If
    End If
    End Sub
    [/vba]

  3. #3
    Quote Originally Posted by skatonni
    Try this.

    [vba]
    Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    Dim strSubject As String
    Dim lngAttach As Long
    Dim strPrompt As String
    strSubject = Item.Subject
    lngAttach = Item.Attachments.count
    If lngAttach = 0 Then
    strPrompt = "are there no attachment to this mail - Yes we have no attachments today"
    If MsgBox(strPrompt, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check for Attachment") = vbNo Then
    Cancel = True
    End If
    End If
    If Len(Trim(strSubject)) = 0 Then
    strPrompt = "Subject is Empty. Are you sure you want to send the Mail?"
    If MsgBox(strPrompt, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check for Subject") = vbNo Then
    Cancel = True
    End If
    End If
    End Sub
    [/vba]
    Thanks skatonni, but still the macro checks only for the subject and not the attachment

  4. #4
    VBAX Mentor skatonni's Avatar
    Joined
    Jun 2006
    Posts
    347
    Location
    Try this to see the attachment count.

    [vba]
    Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    Dim strSubject As String
    Dim lngAttach As Long
    Dim strPrompt As String
    strSubject = Item.Subject
    lngAttach = Item.Attachments.count

    If lngAttach = 0 Then

    strPrompt = "are there no a
    ttachment to this mail - Yes we have no attachments today"
    If MsgBox(strPrompt, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check for Attachment") = vbNo Then
    Cancel = True
    End If

    Else

    Debug.Print "Attachments: " & lngAttach
    ' or
    MsgBox "Attachments: " & lngAttach

    End If

    If Len(Trim(strSubject)) = 0 Then
    strPrompt = "Subject is Empty. Are you sure you want to send the Mail?"
    If MsgBox(strPrompt, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check for Subject") = vbNo Then
    Cancel = True
    End If
    End If
    End Sub
    [/vba]

    I would guess you have an attachment in your signature.
    If so, either remove it or account for it with

    [vba]
    If lngAttach = 1 Then
    [/vba]

  5. #5
    Worked a charm :-). you were right on the signature. it counted for 3.


    Thanks skatonni

Posting Permissions

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