' === Pratica Obrigatoria 1 (ATIVO) ===
Option Explicit

Sub ProcessarFaturamentoCompletoOutlook()
    Dim objOutlook As Object
    Dim objMail As Object
    Dim ws As Worksheet
    Dim lngFinal As Long
    Dim i As Long
    Dim strCaminhoPDF As String
    Dim lngContador As Long
    
    Set ws = ThisWorkbook.Worksheets(1)
    lngFinal = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    
    If lngFinal < 2 Then Exit Sub
    
    Application.ScreenUpdating = False
    Set objOutlook = CreateObject("Outlook.Application")
    lngContador = 0
    
    For i = 2 To lngFinal
        If ws.Cells(i, 6).Value <> "ENVIADO" Then
            strCaminhoPDF = Environ("USERPROFILE") & "\Desktop\Fatura_" & ws.Cells(i, 1).Value & ".pdf"
            
            ws.Range("A" & i & ":E" & i).ExportAsFixedFormat Type:=xlTypePDF, Filename:=strCaminhoPDF
            
            Set objMail = objOutlook.CreateItem(0)
            With objMail
                .To = ws.Cells(i, 3).Value
                .BCC = "auditoria.faturamento@fastlog.com.br"
                .Subject = "FastLog Faturamento - " & ws.Cells(i, 1).Value & " - " & ws.Cells(i, 2).Value
                .HTMLBody = "<div style='font-family: Arial;'>" & _
                            "<p>Prezado cliente <b>" & ws.Cells(i, 2).Value & "</b>,</p>" & _
                            "<p>Segue anexa a fatura no valor de <b>" & FormatCurrency(ws.Cells(i, 4).Value) & "</b>.</p>" & _
                            "<p>Atenciosamente,<br/><b>FastLog S.A. Transportes</b></p>" & _
                            "</div>"
                .Attachments.Add strCaminhoPDF
                .Send
            End With
            
            ' Exclui arquivo temporário (Desafio de Fixação)
            On Error Resume Next
            Kill strCaminhoPDF
            On Error GoTo 0
            
            ws.Cells(i, 6).Value = "ENVIADO"
            ws.Cells(i, 6).Interior.Color = RGB(220, 255, 220)
            lngContador = lngContador + 1
            Set objMail = Nothing
        End If
    Next i
    
    Set objOutlook = Nothing
    Application.ScreenUpdating = True
    
    MsgBox "Lote de " & lngContador & " e-mails processado com sucesso!", vbInformation, "Faturamento Concluído"
End Sub

