Attribute VB_Name = "ExportSinistri" '================================================================ ' Gestione Sinistri - Export email Outlook con numero sinistro ' Genera un CSV nella cartella DOWNLOADS con: ' Data, Mittente, Sinistro, Oggetto, Anteprima, Casella ' per le email il cui OGGETTO contiene il numero sinistro nel ' formato Unipol: X-XXXX-YYYY-NNNNNNN (es. 1-8001-2025-0123456). ' ' Supporta MULTI-ACCOUNT: chiede quale casella esportare se ne hai ' più di una configurata in Outlook. ' ' USO: ' 1) Outlook -> ALT+F11 (apre VBA editor) ' 2) File -> Importa file... -> seleziona questo .bas ' 3) Premi F5 (oppure ALT+F8 -> ExportSinistri -> Esegui) ' 4) A fine export si apre automaticamente la cartella Downloads ' 5) Carica il CSV nel tab Email del plugin ' ' Configurazione: cambia daysBack per allargare o restringere ' il periodo (default ultimi 30 giorni). '================================================================ Option Explicit Public Sub ExportSinistri() Const olFolderInbox As Long = 6 Dim ns As Object, allStores As Object, store As Object Dim folder As Object, itms As Object, mail As Object Dim i As Long, s As Long, cnt As Long Dim daysBack As Integer, minDate As Date, sentDate As Date Dim regex As Object, mObj As Object Dim sinistro As String, subj As String, mittente As String Dim dateStr As String, body As String, preview As String Dim outDir As String, outFile As String, fnum As Integer Dim lineOut As String Dim storeChoice As String Dim storesArr() As String, storesCount As Long Dim msg As String, chosenIdx As Long, processAll As Boolean daysBack = 30 ' --- Lista caselle --- Set ns = Application.GetNamespace("MAPI") Set allStores = ns.Stores storesCount = allStores.Count If storesCount = 0 Then MsgBox "Nessuna casella email trovata in Outlook.", vbCritical, "Errore" Exit Sub End If ReDim storesArr(1 To storesCount) msg = "Quale casella vuoi esportare?" & vbCrLf & vbCrLf For s = 1 To storesCount storesArr(s) = allStores.Item(s).DisplayName msg = msg & s & ") " & storesArr(s) & vbCrLf Next s msg = msg & (storesCount + 1) & ") TUTTE le caselle" & vbCrLf & vbCrLf & "Inserisci il numero:" If storesCount = 1 Then chosenIdx = 1 processAll = False Else storeChoice = InputBox(msg, "Esporta email Sinistri", "1") If Len(storeChoice) = 0 Then Exit Sub On Error Resume Next chosenIdx = CLng(storeChoice) On Error GoTo 0 If chosenIdx < 1 Or chosenIdx > storesCount + 1 Then MsgBox "Scelta non valida.", vbCritical, "Errore" Exit Sub End If processAll = (chosenIdx = storesCount + 1) End If ' --- Regex sinistro Unipol --- Set regex = CreateObject("VBScript.RegExp") regex.Pattern = "\b(\d-\d{4}-\d{4}-\d{6,8})\b" regex.IgnoreCase = False regex.Global = False minDate = DateAdd("d", -daysBack, Now) ' --- Path output: cartella Downloads dell'utente --- outDir = Environ("USERPROFILE") & "\Downloads" ' Verifica/crea la cartella Downloads (di solito esiste già) If Dir(outDir, vbDirectory) = "" Then On Error Resume Next MkDir outDir On Error GoTo 0 End If outFile = outDir & "\email_sinistri_" & Format(Now, "yyyy-mm-dd_hhnn") & ".csv" fnum = FreeFile Open outFile For Output As #fnum Print #fnum, Chr(239) & Chr(187) & Chr(191) & "Data,Mittente,Sinistro,Oggetto,Anteprima,Casella" cnt = 0 For s = 1 To storesCount If processAll Or s = chosenIdx Then Set store = allStores.Item(s) Set folder = Nothing On Error Resume Next Set folder = store.GetDefaultFolder(olFolderInbox) On Error GoTo 0 If Not folder Is Nothing Then Set itms = folder.items On Error Resume Next itms.Sort "[ReceivedTime]", True On Error GoTo 0 For i = 1 To itms.Count Set mail = itms.Item(i) If TypeName(mail) = "MailItem" Then sentDate = mail.ReceivedTime If sentDate < minDate Then Exit For subj = "" On Error Resume Next subj = mail.Subject On Error GoTo 0 If Len(subj) > 0 Then If regex.Test(subj) Then Set mObj = regex.Execute(subj) sinistro = mObj(0).SubMatches(0) mittente = "" On Error Resume Next mittente = mail.SenderName If Len(mail.SenderEmailAddress) > 0 Then mittente = mittente & " <" & mail.SenderEmailAddress & ">" End If On Error GoTo 0 dateStr = Format(sentDate, "yyyy-mm-dd hh:nn") body = "" On Error Resume Next body = mail.body On Error GoTo 0 body = Replace(body, vbCrLf, " ") body = Replace(body, vbCr, " ") body = Replace(body, vbLf, " ") body = Replace(body, vbTab, " ") body = Trim(body) Do While InStr(body, " ") > 0 body = Replace(body, " ", " ") Loop If Len(body) > 200 Then preview = Left(body, 200) & "..." Else preview = body End If lineOut = CsvEscape(dateStr) & "," & _ CsvEscape(mittente) & "," & _ CsvEscape(sinistro) & "," & _ CsvEscape(subj) & "," & _ CsvEscape(preview) & "," & _ CsvEscape(storesArr(s)) Print #fnum, lineOut cnt = cnt + 1 End If End If End If Next i End If End If Next s Close #fnum ' Apri esplora risorse con il file selezionato On Error Resume Next Shell "explorer.exe /select,""" & outFile & """", vbNormalFocus On Error GoTo 0 MsgBox "Esportate " & cnt & " email con numero sinistro (ultimi " & daysBack & " giorni)." & vbCrLf & vbCrLf & _ "File salvato in:" & vbCrLf & outFile & vbCrLf & vbCrLf & _ "Si e' aperta automaticamente la cartella Downloads con il file evidenziato.", _ vbInformation, "Export completato" End Sub ' Escape CSV: racchiude in virgolette doppie e raddoppia le " interne Private Function CsvEscape(ByVal s As String) As String Dim t As String t = Replace(s, """", """""") CsvEscape = """" & t & """" End Function