
Option Explicit
Option Base 1
'matrice elenco scadenze
Public elencoScadenze()
'stabilisce il numero di tuple della matrice
Public righeSCADelenco As Integer
Sub aggiornaScadenze()
Dim i As Integer
Sheets("Elenco Scadenze").Select
'valuta la lunghezza della matrice scadenze in relazione alle righe della tabella
i = 1
Do While Range(Cells(i, 1), Cells(i, 1)).Value <> ""
i = i + 1
Loop
righeSCADelenco = i - 2
If righeSCADelenco > 0 Then
'formatta la dimensione della matrice in base al numero di righe della tabella
ReDim elencoScadenze(righeSCADelenco, 3)
'riempie la matrice
For i = 1 To righeSCADelenco
elencoScadenze(i, 1) = Range(Cells(i + 1, 1), Cells(i + 1, 1)).Value 'colonna TIPODOCUMENTO
elencoScadenze(i, 2) = Range(Cells(i + 1, 2), Cells(i + 1, 2)).Value 'colonna NOTEDOCUMENTO
elencoScadenze(i, 3) = Range(Cells(i + 1, 3), Cells(i + 1, 3)).Value 'colonna DATASCADENZA
Next i
'richiama la funzione letturaScadenze che leggerà i dati contenuti nella matrice
Call letturaScadenze
Else
MsgBox ("Non Vi sono scadenze da pianificare")
End If
End Sub
Sub letturaScadenze()
Dim i As Integer
'per ciascun documento presente nella lista richiama la funzione creaCalendar
For i = 1 To righeSCADelenco
Call creaCalendar(elencoScadenze(i, 1), elencoScadenze(i, 2), elencoScadenze(i, 3))
Next i
End Sub
Sub creaCalendar(tipoDocumento, noteDocumento, dataScadenza)
'verifica che il documento in analisi abbia una data scadenza
If IsNull(dataScadenza) Then
MsgBox "il documento " & tipoDocumento & " non ha una data scadenza", vbInformation, "campo obbligatorio"
Exit Sub
End If
Dim OutApp As Object
Dim OutCalendar As Object
Dim outCalendarFolder As Object
Dim targetCalendar As Object
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
'creare un nuovo appuntamento
Set OutCalendar = OutApp.CreateItem(1)
' 1 sta per appuntamento
OutCalendar.AllDayEvent = True
OutCalendar.Start = dataScadenza
OutCalendar.Subject = tipoDocumento
OutCalendar.Body = noteDocumento
OutCalendar.ReminderMinutesBeforeStart = 50
OutCalendar.ReminderSet = True
OutCalendar.Save
'chiude lo stream con Outlook
Set OutApp = Nothing
Set OutCalendar = Nothing
End Sub |
Set objApp = CreateObject("Outlook.Application")
Set NameSpace = objApp.GetNamespace("MAPI")
Set fdrCalendar = NameSpace.GetDefaultFolder(9) '9 = olFolderCalendar
For Each ItemAppt In fdrCalendar.Items 'use this line instead to consider ALL dates from Outlook
' For Each ItemAppt In fdrCalendar.items.Restrict("[Start] >= '01/01/2017'") 'consider dates from 1-1-2017 onwards from Outlook
'ora qui ciclare per tutti gli item di cui controllare il subject
'tipo: for each v in excel_table
' if itemappt.subject = v[colonna::tipodocumento] Then
' 'trovato subject uguale: verifico se il corpo è uguale, se diverso lo aggiorno
' if itemappt.body <> v[colonna::notedocumento] then itemappt.body = v[colonna::notedocumento]
' problema per la data di scadenza disuguale:
' if itemappt.start <> v[colonna::datascadenza] then ... ' ecco questo è il punto, la data di scadenza non coincide con l'appuntamento. va eliminato e ricostruito nella data corretta. Sicuro che è questa la verifica che vuoi fare?
' end if
'next
next
Sub aggiornaScadenze_VF()
Dim v As Variant
Dim OutApp As Object
Dim OutCalendar As Object
Dim i As Long
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
'creare un nuovo appuntamento
Set OutCalendar = OutApp.CreateItem(1)
' 1 sta per appuntamento
Sheets("Elenco Scadenze").Select
For Each v In Range("A1").CurrentRegion.Rows
If v.Row > 1 Then
If Trim(v.Cells(3)) = "" Then
MsgBox "il documento " & v.Cells(1) & " non ha una data scadenza", vbInformation, "campo obbligatorio"
Exit Sub
End If
With OutCalendar
.AllDayEvent = True
.Start = v.Cells(3) 'data scadenza
.Subject = v.Cells(1) 'tipo documento
.Body = v.Cells(2) 'note documento
.ReminderMinutesBeforeStart = 50
.ReminderSet = True
.Save
End With
i = i + 1
End If
Next
'chiude lo stream con Outlook
Set OutCalendar = Nothing
Set OutApp = Nothing
MsgBox "Ho inserito " & i & "scadenze"
End Sub
|
Sub aggiornaScadenze_VF()
Dim v As Variant
Dim OutApp As Object
Dim OutCalendar As Object
Dim i As Long
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
'creare un nuovo appuntamento
Set OutCalendar = OutApp.CreateItem(1)
' 1 sta per appuntamento
Sheets("Elenco Scadenze").Select
For Each v In Range("A1").CurrentRegion.Rows
If v.Row > 1 Then
If Trim(v.Cells(3)) = "" Then
MsgBox "il documento " & v.Cells(1) & " non ha una data scadenza", vbInformation, "campo obbligatorio"
Exit Sub
End If
With OutCalendar
.AllDayEvent = True
.Start = v.Cells(3) 'data scadenza
.Subject = v.Cells(1) 'tipo documento
.Body = v.Cells(2) 'note documento
.ReminderMinutesBeforeStart = 50
.ReminderSet = True
.Recipients.Add ("tizio@prova.it;caio@prova.it")
.Save
.Send
End With
i = i + 1
End If
Next
Set OutCalendar = Nothing
Set OutApp = Nothing
MsgBox "Ho inserito " & i & " scadenze"
End Sub
|
Sub aggiornaScadenze_VF()
Dim v As Variant
Dim OutApp As Object
Dim OutCalendar As Object
'n contatore nuovi appuntamenti
'm contatore appuntamenti modificati
Dim n, m As Long
'variabili di verifica sui calendar esistenti
Dim itemExist, itemModified As Boolean
Set OutApp = CreateObject("Outlook.Application")
Set Namespace = OutApp.GetNamespace("MAPI")
Set fdrCalendar = Namespace.GetDefaultFolder(9)
OutApp.Session.Logon
Sheets("Elenco Scadenze").Select
n = 0
m = 0
For Each v In Range("A1").CurrentRegion.Rows
If v.Row > 1 Then
esistente = False
If Trim(v.Cells(3)) = "" Then
MsgBox "il documento " & v.Cells(1) & " non ha una data scadenza", vbInformation, "campo obbligatorio"
Else
For Each Itemappt In fdrCalendar.Items
If Itemappt.Subject = v.Cells(1) Then
'trovato subject uguale
'verifico se data e tipo documento sono uguali, se diversi li aggiorno
If Itemappt.Start <> v.Cells(3) Then
Itemappt.Start = v.Cells(3)
itemModified = True
End If
'If Itemappt.Body <> (v.Cells(2) & vbCrLf) Then
' MsgBox(Itemappt.Body & " - " & v.Cells(2))
' MsgBox Len(Itemappt.Body)
' Itemappt.Body = v.Cells(2)
' itemModified = True
'End If
Itemappt.Send
Itemappt.Save
itemExist = True
If itemModified = True Then
m = m + 1
End If
End If
Next
If itemExist = False Then
'creare un nuovo appuntamento
Set OutCalendar = OutApp.CreateItem(1)
With OutCalendar
.AllDayEvent = True
.Start = v.Cells(3) 'data scadenza
.Subject = v.Cells(1) 'tipo documento
.Body = v.Cells(2) 'note documento
.ReminderMinutesBeforeStart = 50
.ReminderSet = True
.Recipients.Add ("tizio@prova.it;caio@prova.it")
.Send
.Save
End With
test = True
n = n + 1
End If
End If
End If
Next
Set OutCalendar = Nothing
Set OutApp = Nothing
MsgBox "VERIFICA APPUNTAMENTI TERMINATA:" & vbCrLf & "Nuove scadenze inserite: " & n & vbCrLf & "Scadenze aggiornate: " & m
End Sub |
Option Explicit
Sub aggiornaScadenze_VF()
Dim v As Variant
Dim OutApp As Object
Dim fdrCalendar As Object
Dim ItemAppt As Object
Dim i As Long, j As Long
Dim bFound As Boolean
Set OutApp = CreateObject("Outlook.Application")
Set fdrCalendar = OutApp.GetNamespace("MAPI").GetDefaultFolder(9) '9 = olFolderCalendar
OutApp.Session.Logon
Sheets("Elenco Scadenze").Select
For Each v In Range("A1").CurrentRegion.Rows
If v.Row > 1 Then
If Trim(v.Cells(3)) = "" Then
MsgBox "il documento " & v.Cells(1) & " non ha una data scadenza", vbInformation, "campo obbligatorio"
Else
'---- check
For Each ItemAppt In fdrCalendar.Items
If ItemAppt.Subject = v.Cells(1) Then
bFound = True
'trovato subject uguale: verifico se il corpo è uguale, se diverso lo aggiorno
If ItemAppt.Body <> v.Cells(2) Then ItemAppt.Body = v.Cells(2)
'data di scadenza non uguale all'appuntamento già inserito:
'cancella appuntamento esistente e lo reinserisce in nuova posizione
If ItemAppt.Start <> v.Cells(3) Then
ItemAppt.Delete
Call CreateItem(OutApp, v)
j = j + 1
End If
End If
Next
'--------------------
If Not bFound Then
Call CreateItem(OutApp, v)
i = i + 1
bFound = False
End If
End If
End If
Next
Set OutApp = Nothing
MsgBox "Ho inserito " & i & " scadenze, ho modificato " & j & " scadenze"
End Sub
Private Sub CreateItem(olApp As Object, v As Variant)
Dim OutCalendar As Object
Set OutCalendar = olApp.CreateItem(1) 'nuovo appuntamento
With OutCalendar
.AllDayEvent = True
.Start = v.Cells(3) 'data scadenza
.Subject = v.Cells(1) 'tipo documento
.Body = v.Cells(2) 'note documento
.ReminderMinutesBeforeStart = 50
.ReminderSet = True
.Recipients.Add ("tizio@prova.it;caio@prova.it")
.Save
'.Send
End With
Set OutCalendar = Nothing
End Sub
|
'trovato subject uguale: verifico se il corpo è uguale, se diverso lo aggiorno
If ItemAppt.Body <> v.Cells(2) Then
ItemAppt.Body = v.Cells(2)
ItemAppt.Save
j = j + 1
End If |
With OutCalendar
.AllDayEvent = True
.Start = v.Cells(3) 'data scadenza
.Subject = v.Cells(1) 'tipo documento
.Body = v.Cells(2) 'note documento
.ReminderMinutesBeforeStart = 50
.ReminderSet = True
.Recipients.Add ("tizio@prova.it;caio@prova.it")
.MeetingStatus = 1 ' 1 = olMeeting
.Recipients.ResolveAll
.Send
.Save
End With
|
e vediamo il risultato.
?Itemappt.Body & " - " & Len(Itemappt.body) & vbcrlf & v.Cells(2) & " - " & len(v.cells(2))
If ItemAppt.Body <> trim(worksheetfunction.Clean(v.Cells(2))) Then ... |
