Outlook Attachment: Object variable or With block variable not set.
I am trying to use this block of code to save attachemnts from Outlook into a folder on my harddrive.
VB Code:
Imports Microsoft.Office.Interop
''' <summary>
''' Saves Outlook attachment to the
''' client computer.
''' </summary>
''' <remarks></remarks>
Public Class SlurpEmail
Private moApp As Outlook.Application
Public Sub SlurpEmail()
Dim objOL As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim objFolder As Outlook.Folders
Dim myItems As Outlook.Items
Dim x As Int16
objOL = New Outlook.Application()
objNS = objOL.GetNamespace("MAPI")
Dim olfolder As Outlook.MAPIFolder
olfolder = objOL.GetNamespace("MAPI").PickFolder
myItems = olfolder.Items
Dim filename As String
Dim Atmt As Outlook.Attachment
Dim Item As Object
For Each Atmt In Item.Attachments
filename = "C:\reports\" + Atmt.FileName
Atmt.SaveAsFile(filename)
Next Atmt
End Sub
It is erroring on this line For Each Atmt In Item.Attachments with this error Object variable or With block variable not set.
Re: Outlook Attachment: Object variable or With block variable not set.
You have declared Item, but have not given it any kind of value (ie: you have not Set it).
Presumably you want to do this for all emails, so a loop like this is probably what you want:
Code:
For Each Item In MyItems
For Each Atmt In Item.Attachments
Re: Outlook Attachment: Object variable or With block variable not set.
It makes sense now that you point it out. Thanks!