# Checking SenderName in Outlook VBA



## lionelnz (Jan 29, 2010)

Hi all.

I am woking on a macro that Iwant to check for Sendername & attachment and if both of these criteria are true then save attachments. The code I have below is only checking at the moment as I have code to save but it saves all attachmenst irrespective of the sender.

I am getting error 91 Object variable or with block variable not set I think the problem is with this line "sn = MailItem.SenderName" .

So here is my code

```
Sub ChkSender()
'29/1/10 - Testing for senders name
' http://www.your-save-time-and-improve-quality-technologies-online-resource.com/
'save-attachments-from-outlook-using.html
'& http://www.mrexcel.com/forum/showthread.php?t=50334 Ivan F Moala
   Dim OutApp As Object  'Outlook.Application
   Dim ns As NameSpace
   Dim fld2SaveAtt As MAPIFolder
   Dim MailItem As Object
   'Dim MItem As Object 'Outlook.MailItem
   Dim sn As Object
   Dim Att As Attachment
   Dim APath As String, FileName As String
   Dim intFiles As Integer
   Dim ctr As Integer
   ctr = 0
   On Error GoTo HandleError
   'Path = "C:\Attachments\" 'Not needed for this test
   
   
   Set OutApp = CreateObject("Outlook.Application")
   Set ns = OutApp.GetNamespace("MAPI")
   Set fld2SaveAtt = ns.GetDefaultFolder(olFolderInbox).Folders("Business")
   intFiles = 0
   sn = MailItem.SenderName:confused:
   If fld2SaveAtt.Items.Count = 0 Then
       MsgBox "There were no messages found in your Inbox."
       Exit Sub 'there are no messages, so Exit the Sub
   End If
   'Loop through Mail Items
   For Each MailItem In fld2SaveAtt.Items
       'Loop through any attachments
       For Each Att In MailItem.Attachments
           If sn = "accounts.receivable@abc.co.nz" Then
           ctr = ctr + 1
           End If
        Next
   Next
' Show summary message
   If intFiles > 0 Then
       MsgBox ctr & " Msgs with attachments were found."
   Else
       MsgBox "No attachments were found"
   End If
 
   Set Att = Nothing
   Set MailItem = Nothing
   Set ns = Nothing
   Exit Sub
HandleError:
   MsgBox "Error: " & Err.Number & vbCrLf & _
           "Description: " & Err.Description & vbCrLf
   Resume Next 'Continue counting
End Sub
```


----------



## lionelnz (Jan 30, 2010)

*Re: Checking SenderName in Outlook VBA - Resolved*

So here is my resolved code

```
Sub SaveOrconAtts()
'29/1/10
'http://www.your-save-time-and-improve-quality-technologies-online-resource.com/
'save-attachments-from-outlook-using.html
   Dim ns As NameSpace
   Dim fld2SaveAtt As MAPIFolder
   Dim MailItem As Object
   Dim Att As Attachment
   Dim APath As String, FileName As String
   Dim sn As String, sDate As String
   
   Dim intFiles As Integer
 
   On Error GoTo HandleError
   APath = "C:\Attachments\"
   Set ns = GetNamespace("MAPI")
   Set fld2SaveAtt = ns.GetDefaultFolder(olFolderInbox).Folders("Business")
   intFiles = 0
     If fld2SaveAtt.Items.Count = 0 Then
       MsgBox "There were no messages found in your Inbox."
       Exit Sub 'there are no messages, so Exit the Sub
   End If
   'Loop through Mail Items
   For Each MailItem In fld2SaveAtt.Items
        sn = MailItem.SenderName
       'Loop through any attachments
       For Each Att In MailItem.Attachments
        If sn = "accounts.receivable@ABC.co.nz" Then
            FileName = Trim(Att.FileName)
            FileName = sDate & Att.FileName
            Att.SaveAsFile APath & FileName
            intFiles = intFiles + 1
            Else
        End If
       Next
   Next
' Show summary message
   If intFiles > 0 Then
       MsgBox intFiles & " attachments were saved to " ^ _
       "C:\Attachments."
   Else
       MsgBox "No attachments were found"
   End If
 
   Set Att = Nothing
   Set MailItem = Nothing
   Set ns = Nothing
   Exit Sub
HandleError:
   MsgBox "Error: " & Err.Number & vbCrLf & _
           "Description: " & Err.Description & vbCrLf & _
           "The file's name is " & FileName
           intFiles = intFiles - 1
   Resume Next 'Continue saving attachments
End Sub
```


----------

