
Sub PromemoriaInvioFatture()
'dati base
Dim RagioneSociale As String
Dim IndirizzoEmail As String
Dim BodyMsg As String
Dim Oggetto As String
Dim PercorsoFile As String
'dati utili con clienti monofatture
Dim x As Variant
Dim CodClie As Variant
Dim NrFattura As String
Dim ImpFattura As Currency
Dim DataFattura As Date
'dati utili con clienti con più fatture
Dim y As Variant
Dim CodClie2 As Integer
Dim NrFattura2 As String
Dim ImpFattura2 As Currency
Dim DataFattura2 As Date
For x = 2 To 10
y = x + 1
RagioneSociale = Worksheets("Foglio1").Cells(x, 2)
IndirizzoEmail = Worksheets("Foglio1").Cells(x, 6)
CodClie = Worksheets("Foglio1").Cells(x, 1)
NrFattura = Worksheets("Foglio1").Cells(x, 3)
ImpFattura = Worksheets("Foglio1").Cells(x, 4)
DataFattura = Worksheets("Foglio1").Cells(x, 5)
CodClie2 = Worksheets("Foglio1").Cells(y, 1)
NrFattura2 = Worksheets("Foglio1").Cells(y, 3)
ImpFattura2 = Worksheets("Foglio1").Cells(y, 4)
DataFattura2 = Worksheets("Foglio1").Cells(y, 5)
If CodClie <> "" Then
If CodClie = CodClie2 Then
BodyMsg = "Gentile Cliente," _
& vbCrLf & "con la presente siamo a ricordarLe che oggi sono in scadenza le fatture:" _
& vbCrLf & "-Nr. " & NrFattura & " del " & DataFattura & " per € " & ImpFattura & ";" _
& vbCrLf & "-Nr. " & NrFattura2 & " del " & DataFattura2 & " per € " & ImpFattura2 & "." _
& vbCrLf & "Cordiali saluti."
Oggetto = "Promemoria scadenza fatture"
If IndirizzoEmail <> "" Then
Shell "C:Program Files (x86)Mozilla Thunderbird hunderbird - compose" _
& Chr$(34) & "to='" & IndirizzoEmail & "', subject='" & Oggetto & "', body '" & BodyMsg _
& "', attachment = '" & PercorsFile & "'" & Chr$(34), vbNormalFocus
Application.Wait Now + TimeValue("00:00:03")
End If
Else
BodyMsg = "Gentile Cliente," _
& vbCrLf & "con la presente siamo a ricordarLe che oggi è in scadenza la fattura nr. " & NrFattura & " del " & DataFattura & " per € " & ImpFattura & ";" _
& vbCrLf & "Cordiali saluti."
Oggetto = "Promemoria scadenza fatture"
If IndirizzoEmail <> "" Then
Shell "C:Program Files (x86)Mozilla Thunderbird hunderbird - compose" _
& Chr$(34) & "to='" & IndirizzoEmail & "', subject='" & Oggetto & "', body '" & BodyMsg _
& "', attachment = '" & PercorsFile & "'" & Chr$(34), vbNormalFocus
Application.Wait Now + TimeValue("00:00:03")
End If
End If
End If
Next x
End Sub |
Option Explicit
Sub PromemoriaInvioFatture_VF()
Dim dict As Object
Dim v As Variant
Dim cel As Range
Dim s As String, p As String
Set dict = CreateObject("Scripting.Dictionary")
For Each cel In Range("A1").CurrentRegion.Rows
If cel.Row > 1 Then
s = CStr(cel.Cells(1))
v = Range(Cells(cel.Row, "B"), Cells(cel.Row, "F"))
v = Application.Transpose(Application.Transpose(v))
If Not dict.exists(CStr(s)) Then
dict.Add CStr(s), Join(v, "|")
p = ""
Else
p = dict(s) & "^|" & Join(v, "|") & "^|"
dict(s) = Left(p, Len(p) - 2)
End If
End If
Next
For Each v In dict
s = compose_mail(dict(v))
Next
End Sub
Private Function compose_mail(s As String)
Dim v As Variant, vv As Variant, k As Variant
Dim bodymsg As String
Dim oggetto As String
Dim destinatary As String
Dim percorsofile As String
v = Split(s, "^|")
If UBound(v) > 0 Then 'più fatture
bodymsg = "Gentile Cliente,§con la presente siamo a ricordarLe che oggi sono in scadenza le fatture:§"
For Each vv In v
k = Split(vv, "|")
bodymsg = bodymsg & "- Nr. " & k(1) & " del " & k(3) & " per " & k(2) & ";§"
destinatary = destinatary & k(4) & ";"
Next
Else
vv = Split(s, "|")
bodymsg = "Gentile Cliente,§con la presente siamo a ricordarLe che oggi è in scadenza la fattura nr. " & _
vv(1) & " del " & vv(3) & " per " & vv(2) & ";§"
destinatary = vv(4)
End If
bodymsg = bodymsg & "§Cordiali saluti."
bodymsg = Replace(bodymsg, "§", vbNewLine)
oggetto = "Promemoria scadenza fatture"
percorsofile = ""
If destinatary <> "" Then
s = "C:Program Files (x86)Mozilla Thunderbird hunderbird - compose" _
& Chr$(34) & "to='" & destinatary & "', subject='" & oggetto & "', body '" & bodymsg _
& "', attachment = '" & percorsofile & "'" & Chr$(34)
MsgBox "Invio mail" & vbNewLine & s, vbInformation
'Shell s, vbNormalFocus
'Application.Wait Now + TimeValue("00:00:03")
End If
End Function
|
& oggetto & "', body='" & bodymsgperchè manca il segno di uguale analogamente a quanto fatto per to, subject, eccetera.
v = application.transpose(range("A1:A2"))
?v(1), v(2)
a aa
v = application.transpose(application.transpose(range("A1:C1")))
?v(1), v(2), v(3)
a b c
v1 = Application.Transpose(Application.Transpose(Range("A1:C1")))
v2 = Range("A1:C1")
|
Option Explicit
Sub PromemoriaInvioFatture_VF()
Dim dict As Object
Dim v As Variant
Dim cel As Range
Dim s As String, p As String
Set dict = CreateObject("Scripting.Dictionary")
For Each cel In Range("A1").CurrentRegion.Rows
If cel.Row > 1 Then
s = CStr(cel.Cells(1))
v = Range(Cells(cel.Row, "B"), Cells(cel.Row, "F"))
v = Application.Transpose(Application.Transpose(v))
If Not dict.exists(CStr(s)) Then
dict.Add CStr(s), Join(v, "|")
p = ""
Else
p = dict(s) & "^|" & Join(v, "|") & "^|"
dict(s) = Left(p, Len(p) - 2)
End If
End If
Next
For Each v In dict
s = compose_mail(dict(v))
Next
End Sub
Private Function compose_mail(s As String)
Dim v As Variant, vv As Variant, k As Variant
Dim bodymsg As String
Dim oggetto As String
Dim Destinatario As String
Dim percorsofile As String
v = Split(s, "^|")
If UBound(v) > 0 Then 'più fatture
bodymsg = "Gentile Cliente,"con la presente siamo a ricordarLe che oggi sono in scadenza le fatture:""
For Each vv In v
k = Split(vv, "|")
bodymsg = bodymsg & "- Nr. " & k(1) & " del " & k(3) & " per € " & k(2) & ";""
Destinatario = k(4)
Next
Else
vv = Split(s, "|")
bodymsg = "Gentile Cliente,"con la presente siamo a ricordarLe che oggi è in scadenza la fattura nr. " & _
vv(1) & " del " & vv(3) & " per € " & vv(2) & ";""
Destinatario = vv(4)
End If
bodymsg = bodymsg & ""Cordiali saluti."
bodymsg = Replace(bodymsg, """, vbNewLine)
oggetto = "Promemoria scadenza fatture"
percorsofile = ""
If Destinatario <> "" Then
Shell "C:Program Files (x86)Mozilla Thunderbird hunderbird -compose " _
& Chr$(34) & "to='" & Destinatario & "',subject='" & oggetto & "',body='" & bodymsg _
& "',attachment='" & percorsofile & "'" & Chr$(34), vbNormalFocus
Application.Wait Now + TimeValue("00:00:03")
SendKeys "^{ENTER}"
End If
End Function
|
bodymsg = "Gentile Cliente,"con la presente siamo a ricordarLe che oggi sono in scadenza le fatture:""ci sono due virgolette fuori posto: vanno sostituite con un segno di paragrafo """ (o qualsiasi altro carattere strano va bene!) che poi in fase di resa nel messaggio HTML viene sostituito da replace in un carattere di new line:
bodymsg = Replace(bodymsg, """, vbNewLine)
