Advertisement
Guest User

Untitled

a guest
Oct 11th, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Public Sub EnviarEmail()
  2.  
  3.     'Referencia a tabela de emails
  4.    Dim tabelaEmails As ListObject
  5.     Dim mailConfig As Object
  6.     Dim fields As Variant
  7.     Dim msConfigURL As String
  8.    
  9.     Set tabelaEmails = Emails.ListObjects("ListaEmails")
  10.     Set mailConfig = CreateObject("CDO.Configuration")
  11.     msConfigURL = "http://schemas.microsoft.com/cdo/configuration"
  12.    
  13.     ' load all default configurations
  14.    mailConfig.Load -1
  15.     Set fields = mailConfig.fields
  16.    
  17.     'Declaracao dos objetos utilizados para enviar email pelo Outlook
  18.    Dim olApp As Object
  19.     Dim olMailItm As Object
  20.    
  21.     'Itera para cada linha do Data Body Range (valores da tabela, sem cabecalho)
  22.    Dim Linha As Variant
  23.     For Each Linha In tabelaEmails.DataBodyRange.Rows
  24.         'Alocacao do objeto para envio do email
  25.        Set olMailItm = CreateObject("CDO.Message")
  26.         'Se der erro, ele vai para a proxima linha
  27.        On Error GoTo Finaliza
  28.         'Verifica se a linha deve ser enviada ou nao
  29.        If Linha.Cells(1, 6).Value = "Sim" And Linha.Cells(1, 7).Value <> "Enviado" Then
  30.             'Se for para enviar, pega os campos para montar o email
  31.            With olMailItm
  32.                 .BCC = Linha.Cells(1, 1).Value
  33.                 .From = """Contas"" <controle.nf@superbac.com.br>"
  34.                 .Subject = Linha.Cells(1, 4).Value
  35. '                .Attachments.Add "C:\Users\Visagio\Downloads\imagem.png", 1, 0
  36.                .HTMLBody = .HTMLBody & "<img src='cid:imagem.png' height=240 width=180 />"
  37.                 .HTMLBody = .HTMLBody & Linha.Cells(1, 5).Value
  38.             End With
  39.            
  40.             With fields
  41.                 'Enable SSL Authentication
  42.                .Item(msConfigURL & "/smtpusessl") = True
  43.                
  44.                 'Make SMTP authentication Enabled=true (1)
  45.                .Item(msConfigURL & "/smtpauthenticate") = 1
  46.                
  47.                 'Set the SMTP server and port Details
  48.                'To get these details you can get on Settings Page of your Gmail Account
  49.                .Item(msConfigURL & "/smtpserver") = "smtp.office365.com"
  50.                 .Item(msConfigURL & "/smtpserverport") = 25
  51.                 .Item(msConfigURL & "/sendusing") = 2
  52.                
  53.                 'Set your credentials of your Gmail Account
  54.                .Item(msConfigURL & "/sendusername") = "controle.nf@superbac.com.br"
  55.                 .Item(msConfigURL & "/sendpassword") = "notas@123"
  56.                
  57.                 'Update the configuration fields
  58.                .Update
  59.             End With
  60.            
  61.             olMailItm.Configuration = mailConfig
  62.             olMailItm.Send
  63.            
  64.             'Por fim, atualiza o status para nao enviar da proxima vez
  65.            Linha.Cells(1, 6).Value = "Não"
  66.             Linha.Cells(1, 7).Value = "Enviado"
  67.         End If
  68. Finaliza:
  69.     Next
  70.    
  71.     'Mensagem de sucesso
  72.    MsgBox "E-mails enviados com sucesso!"
  73.    
  74. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement