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
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
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