Il giorno martedì 13 gennaio 2015 09:54:58 UTC+1, Marco67 ha scritto:
> Salve a tutti, ho trovato su post precedent i metodi per l'invio
> di messaggi di posta tramite VBA.
> Ho pero' l'esigenza di scaricare tramite VBA i messaggi di posta
> per archiviarli nella mia tabella...
> Sapreste darmi qualche suggerimento in tal senso?
Si, come no. Ho scritto questa routine per lavoro e funziona che
e' una bellezza, pero' la devi adattare :
Public Sub Legge_Mails()
On Error GoTo ERRORE
Dim OTLK As Outlook.Application
Dim Mail As Outlook.MailItem
Dim Session As Outlook.Namespace
Dim Folder As Outlook.MAPIFolder
Dim Max_Mails As Long
Dim Allegati As New ADODB.Recordset
Dim POSTA As New ADODB.Recordset
Dim Mittente As String
Dim Oggetto As String
Dim Anomalie As String
Dim M As Long
Dim A As Long
Dim Allegato As String
Dim N_Allegati As Long
Dim S As Form
' ------------- routine -----------------------------------------------------------------------------------
Set OTLK = GetObject(, "outlook.application")
Set Session = OTLK.Session
Set Folder = Session.GetDefaultFolder(olFolderInbox)
Set S = G_Sys_Stato.Sentinella
If Folder.Items.Count = 0 Then Set Folder = Nothing: Set Session = Nothing: Exit Sub
Max_Mails = 10
POSTA.LockType = adLockOptimistic: POSTA.CursorType = adOpenStatic: POSTA.CursorLocation = adUseClient
Allegati.LockType = adLockOptimistic: Allegati.CursorLocation = adUseClient: Allegati.CursorType = adOpenStatic
POSTA.Open "select * from posta_in where 1=0;", G_Sys_Stato.CN_LS
Allegati.Open "select * from allegati_in where 1=0;", G_Sys_Stato.CN_LS
If Max_Mails > Folder.Items.Count Then Max_Mails = Folder.Items.Count
For M = Max_Mails To 1 Step -1
Set Mail = Folder.Items(M)
' controllo già letta
If Not Mail.UnRead Then GoTo Skip_Mail
Mail.UnRead = False
'controllo esistenza indirizzo
Mittente = Trim(Mail.SenderEmailAddress)
G_Indirizzi.MoveFirst
G_Indirizzi.Find "indirizzo_='" & Mittente & "'"
If G_Indirizzi.EOF Then GoTo Skip_Mail
' controllo indirizzo esterno : in questo caso nel corpo deve essere presente la password
' e deve essere inserita ALL' INIZIO del corpo della mail
If Not IsNull(G_Indirizzi.Fields("esterno_").Value) Then
If G_Indirizzi.Fields("esterno_").Value Then
If Not (InStr(Mail.Body, G_Password) = 1) Then Anomalie = "indirizzo esterno ma password mancante.;"
End If
End If
'controllo esistenza funzione
Oggetto = Trim(Replace(Mail.Subject, " ", ""))
G_Funzioni.MoveFirst
G_Funzioni.Find "funzione_='" & Oggetto & "'"
If (G_Funzioni.EOF) Then Anomalie = Anomalie & "funzione non esistente.;"
' controllo funzione benformata
Anomalie = Anomalie & Richiesta_Ben_Formata(Mail.Subject, Mail.Body)
'controllo abilitazione indirizzo -> funzione
G_Indirizzi_Funzioni.MoveFirst
G_Indirizzi_Funzioni.Find "indirizzo_='" & Mittente & "'"
If G_Indirizzi_Funzioni.EOF Then
Anomalie = Anomalie & "indirizzo non esistente nelle abilitazioni.;"
Else
If IsNull(G_Indirizzi_Funzioni.Fields("abilitato_").Value) Then
Anomalie = Anomalie & "indirizzo esistente nelle abilitazioni ma non abilitato alla funzione.;"
Else
If Not (G_Indirizzi_Funzioni.Fields("abilitato_").Value) Then
Anomalie = Anomalie & "indirizzo esistente nelle abilitazioni ma non abilitato alla funzione.;"
End If
End If
End If
K = "{" & Kiave() & "}"
POSTA.AddNew
POSTA.Fields("kiave_").Value = K
POSTA.Fields("numero_allegati_").Value = Mail.Attachments.Count
POSTA.Fields("cc_nascosti_").Value = Mail.Bcc
POSTA.Fields("corpo_").Value = Mail.Body
POSTA.Fields("cc_").Value = Mail.CC
POSTA.Fields("id_").Value = Mail.EntryID
POSTA.Fields("oggetto_").Value = Oggetto
POSTA.Fields("mittente_").Value = Mittente
POSTA.Fields("dt_ricezione_").Value = Mail.ReceivedTime
POSTA.Fields("elaborata_").Value = False
POSTA.Fields("Anomalie_").Value = Anomalie
POSTA.Update
DoEvents
' registra e salva tutti gli allegati (solo se non sono state riscontrate anomalie)
N_Allegati = Mail.Attachments.Count
If (N_Allegati > 0) And (Anomalie = "") Then
KK = K
For A = 1 To N_Allegati
Allegato = G_ALLEGATI_IN & KK & Mail.Attachments.Item(i).FileName
Allegati.AddNew
Allegati("Kiave_").Value = KK: Allegati("Kiave_Posta_").Value = K: Allegati("Nome_").Value = Allegato
Allegati.Update
Mail.Attachments.Item(i).SaveAsFile Allegato
DoEvents
KK = "{" & Kiave() & "}"
Next A
End If
Skip_Mail:
Anomalie = ""
Next M
Allegati.Close: Set Allegati = Nothing: POSTA.Close: Set POSTA = Nothing: DoEvents
Exit Sub
ERRORE:
Select Case Err.Number
Case 429 ' outlook chiuso
S.Sentinella.stato_outlook_.Value = "Chiuso": S.Refresh: DoEvents: Exit Sub
Case Else
GoTo Skip_Mail
End Select
End Sub