Results 1 to 3 of 3

Thread: Outlook Attachment: Object variable or With block variable not set.

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    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:
    1. Imports Microsoft.Office.Interop
    2. ''' <summary>
    3. ''' Saves Outlook attachment to the
    4. ''' client computer.
    5. ''' </summary>
    6. ''' <remarks></remarks>
    7. Public Class SlurpEmail
    8.     Private moApp As Outlook.Application
    9.  
    10.     Public Sub SlurpEmail()
    11.         Dim objOL As Outlook.Application
    12.         Dim objNS As Outlook.NameSpace
    13.         Dim objFolder As Outlook.Folders
    14.         Dim myItems As Outlook.Items
    15.         Dim x As Int16
    16.  
    17.         objOL = New Outlook.Application()
    18.         objNS = objOL.GetNamespace("MAPI")
    19.         Dim olfolder As Outlook.MAPIFolder
    20.         olfolder = objOL.GetNamespace("MAPI").PickFolder
    21.         myItems = olfolder.Items
    22.  
    23.         Dim filename As String
    24.         Dim Atmt As Outlook.Attachment
    25.         Dim Item As Object
    26.         For Each Atmt In Item.Attachments
    27.             filename = "C:\reports\" + Atmt.FileName
    28.             Atmt.SaveAsFile(filename)
    29.         Next Atmt
    30.     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.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Outlook Attachment: Object variable or With block variable not set.

    It makes sense now that you point it out. Thanks!

Posting Permissions

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



Click Here to Expand Forum to Full Width