Attribute VB_Name = "Schedulatore" Dim exportScheduler As exportScheduler Sub ScheduleMacro() ' Inizializza la schedulazione delle esportazioni InitializeExportScheduler ' Imposta l'orario per l'esecuzione giornaliera delle altre macro Dim scheduledTime As Date scheduledTime = TimeValue("15:00:00") ' Imposta l'orario desiderato Dim lastRunDate As Date Dim runToday As Boolean Dim userResponse As Integer ' Leggi l'ultima data di esecuzione dal file di testo lastRunDate = GetLastRunDate() ' Controlla se la macro è già stata eseguita oggi runToday = (lastRunDate = Date) ' Chiedi conferma all'utente solo se la macro non è stata ancora eseguita oggi If Not runToday Then userResponse = MsgBox("Outlook: vuoi procedere con le altre schedulazioni? Questo processo può durare molto e potrebbe bloccare Outlook.", vbYesNo + vbQuestion, "Conferma Schedulazione") If userResponse = vbYes Then If Now < scheduledTime Then ' Usa il Timer di Windows per schedulare l'esecuzione Shell "cmd /c start /b timeout /t " & DateDiff("s", Now, scheduledTime) & " && outlook.exe /runmacro ExecuteOtherTasks", vbHide ElseIf Now >= scheduledTime Then Call ExecuteOtherTasks End If End If End If End Sub Sub InitializeExportScheduler() ' Crea un'istanza del modulo di classe ExportScheduler Set exportScheduler = New exportScheduler ' Inizializza l'oggetto Items per monitorare gli eventi Set exportScheduler.olItems = Application.Session.GetDefaultFolder(olFolderInbox).Items ' Imposta il prossimo tempo di esecuzione exportScheduler.nextExportTime = DateAdd("n", 30 - (Minute(Now) Mod 30), DateAdd("n", -Minute(Now), Now)) End Sub Sub ExecuteOtherTasks() Call SavePowerPointAttachments Call SaveCategorizedEmailsFromAllFolders SaveLastRunDate Date ' Aggiorna la data di esecuzione End Sub Function GetLastRunDate() As Date ' Recupera l'ultima data di esecuzione dal file di testo Dim filePath As String filePath = "C:\AGENTE_VBA\OUTLOOK\LastRunDate.txt" Dim fileContent As String If Dir(filePath) <> "" Then Open filePath For Input As #1 Line Input #1, fileContent Close #1 GetLastRunDate = CDate(fileContent) Else GetLastRunDate = 0 End If End Function Sub SaveLastRunDate(d As Date) ' Salva l'ultima data di esecuzione in un file di testo Dim filePath As String filePath = "C:\AGENTE_VBA\OUTLOOK\LastRunDate.txt" Open filePath For Output As #1 Print #1, Format(d, "yyyy-mm-dd") Close #1 End Sub