Checking SenderName in Outlook VBA

lionelnz

Well-known Member
Joined
Apr 6, 2006
Messages
571
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" :confused:.

So here is my code
Rich (BB 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
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Re: Checking SenderName in Outlook VBA - Resolved

So here is my resolved code
Rich (BB 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
 
Upvote 0

Forum statistics

Threads
1,215,197
Messages
6,123,585
Members
449,108
Latest member
rache47

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top