Results 1 to 15 of 15
  1. #1
    Gold Lounger
    Join Date
    Jan 2004
    Location
    Italy
    Posts
    3,245
    Thanks
    0
    Thanked 0 Times in 0 Posts
    From other project in vba for excel i want to read a speciphic address of email similar gssitaly@iol.it and save the attached file in a dir of c:\.

    you must know:

    1) i recived at every day an email from gssitaly@iol.it
    2) the attched fiel is a zip file

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    Try code like this:

    Code:
    Sub ReadEmail()
      Dim olApp As Object
      Dim olNsp As Object
      Dim olFld As Object
      Dim olFld2 As Object
      Dim olItm As Object
      Dim olAtt As Object
      Dim blnStart As Boolean
    
      On Error Resume Next
      Set olApp = GetObject(, "Outlook.Application")
      If olApp Is Nothing Then
    	Set olApp = CreateObject("Outlook.Application")
    	If olApp Is Nothing Then
    	  MsgBox "Failed to start Outlook!", vbExclamation
    	  Exit Sub
    	End If
    	blnStart = True
      End If
      On Error GoTo ErrHandler
    
      Set olNsp = olApp.GetNamespace("MAPI")
      Set olFld = olNsp.GetDefaultFolder(6) ' 6 = Inbox
      Set olItm = olFld.Items.Find("[SenderName]='gssitaly@iol.it'")
      If olItm Is Nothing Then
    	MsgBox "Item not found!", vbExclamation
    	GoTo ExitHandler
      End If
      If olItm.Attachments.Count = 0 Then
    	MsgBox "Attachment not found!", vbExclamation
    	GoTo ExitHandler
      End If
      Set olAtt = olItm.Attachments.Item(1)
      olAtt.SaveAsFile "C:\" & olAtt.Filename
      Set olFld2 = olNsp.GetDefaultFolder(3) ' 3 = Deleted Items
      olItm.Move olFld2
    
    ExitHandler:
      On Error Resume Next
      If blnStart Then
    	olApp.Quit
      End If
      Exit Sub
    
    ErrHandler:
      MsgBox Err.Description, vbExclamation
      Resume ExitHandler
    End Sub
    The code will move the item to the Deleted Items folder after saving the attachment, to prevent the item from being processed again. You can move it to another folder if desired.

    Note: edited to correct error - I switched the codes for Inbox and Deleted Items by accident

  3. #3
    Gold Lounger
    Join Date
    Jan 2004
    Location
    Italy
    Posts
    3,245
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [quote name='HansV' post='799963' date='26-Oct-2009 17:15']Try code like this:

    Code:
    Sub ReadEmail()
      Dim olApp As Object
      Dim olNsp As Object
      Dim olFld As Object
      Dim olFld2 As Object
      Dim olItm As Object
      Dim olAtt As Object
      Dim blnStart As Boolean
    
      On Error Resume Next
      Set olApp = GetObject(, "Outlook.Application")
      If olApp Is Nothing Then
    	Set olApp = CreateObject("Outlook.Application")
    	If olApp Is Nothing Then
    	  MsgBox "Failed to start Outlook!", vbExclamation
    	  Exit Sub
    	End If
    	blnStart = True
      End If
      On Error GoTo ErrHandler
    
      Set olNsp = olApp.GetNamespace("MAPI")
      Set olFld = olNsp.GetDefaultFolder(3) ' 3 = Inbox
      Set olItm = olFld.Items.Find("[SenderName]='gssitaly@iol.it'")
      If olItm Is Nothing Then
    	MsgBox "Item not found!", vbExclamation
    	GoTo ExitHandler
      End If
      If olItm.Attachments.Count = 0 Then
    	MsgBox "Attachment not found!", vbExclamation
    	GoTo ExitHandler
      End If
      Set olAtt = olItm.Attachments.Item(1)
      olAtt.SaveAsFile "C:\" & olAtt.Filename
      Set olFld2 = olNsp.GetDefaultFolder(6) ' 6 = Deleted Items
      olItm.Move olFld2
    
    ExitHandler:
      On Error Resume Next
      If blnStart Then
    	olApp.Quit
      End If
      Exit Sub
    
    ErrHandler:
      MsgBox Err.Description, vbExclamation
      Resume ExitHandler
    End Sub
    The code will move the item to the Deleted Items folder after saving the attachment, to prevent the item from being processed again. You can move it to another folder if desired.[/quote]

    Hi Hans the code work perfect... tk.
    But in inbox i acan have many email with sender gss.italy@iol.it!
    The only distinguish case to save the zip attached is the name of zipped file and is it composed: lavorazione_nnnnnnn.zip when nnnnnn is the date when the file is created...
    Is possible, befaore to save atteched, loop in all email and find lavorazione_nnnnnn.zip
    i think you have understand me.
    Tks as usual

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    Try the version in the attached text file.
    Attached Files Attached Files

  5. #5
    2 Star Lounger
    Join Date
    Jan 2007
    Location
    Gray, Louisiana, USA
    Posts
    289
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I tried the first code posted here. I changed the recipient and I got an error that states 'I don't have permission to save the attachment'. No error number.

    How do I grant myself permission???

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    The code tries to save the attachment to C:\, i.e. the root of the C: drive.
    On some computers writing to C:\ is not allowed; you might try changing it to another folder, for example C:\Documents and Settings\<yourusername>\My Documents.

  7. #7
    2 Star Lounger
    Join Date
    Jan 2007
    Location
    Gray, Louisiana, USA
    Posts
    289
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by HansV View Post
    The code tries to save the attachment to C:\, i.e. the root of the C: drive.
    On some computers writing to C:\ is not allowed; you might try changing it to another folder, for example C:\Documents and Settings\<yourusername>\My Documents.
    That saved it to the folder, but it only save the file information, not the actual attachement.

    thanks

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    Quote Originally Posted by stans View Post
    That saved it to the folder, but it only save the file information, not the actual attachement.
    No idea why that happens, the code should save the attachment itself...

  9. #9
    Gold Lounger
    Join Date
    Jan 2004
    Location
    Italy
    Posts
    3,245
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by HansV View Post
    Try the version in the attached text file.
    naturally your code work fantastic!
    But...
    but how to' crreAte a new box in "posta in arrivo" and move the zip file insted to' move in deleted item?

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    You can create the subfolder of Inbox (I assume that's the equivalent of "Posta in Arrivo") manually. Let's say that you name it Processed.
    Change the line

    Set olFld2 = olNsp.GetDefaultFolder(3) ' 3 = Deleted Items

    to

    Set olFld2 = olNsp.GetDefaultFolder(6).Folders("Processed")



  11. #11
    Gold Lounger
    Join Date
    Jan 2004
    Location
    Italy
    Posts
    3,245
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by HansV View Post
    Try the version in the attached text file.
    Hans... based the code in attached is possible to reneme the Zip file before to move in the "processed" folder?

    Similar:

    original name file.zip move with file_date.zip where date is the value of varibale Date

    This tips permit me to have a History of processed files

  12. #12
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    Change the line

    olAtt.SaveAsFile "C:\" & olAtt.Filename

    to

    Code:
    Dim strFilename As String
    strFilename = olAtt.Filename
    strFilename = Left(strFilename, Len(strFilename) - 4) & "_" & _
    Format(Date, "yyyymmdd") & ".zip"
    olAtt.SaveAsFile "C:\" & strFilename

  13. #13
    Gold Lounger
    Join Date
    Jan 2004
    Location
    Italy
    Posts
    3,245
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by HansV View Post
    Try the version in the attached text file.
    ... end if i want to delete the email with the zip attached?
    I think in this case not possible to read the already procesed Zip...

  14. #14
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    If you want to delete the e-mail, change

    olItm.Move olFld2

    to

    olItm.Delete


  15. #15
    Gold Lounger
    Join Date
    Jan 2004
    Location
    Italy
    Posts
    3,245
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by HansV View Post
    If you want to delete the e-mail, change

    olItm.Move olFld2

    to

    olItm.Delete
    nice tks.

Posting Permissions

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