Sim, confesso ter escrito inúmeras vezes sob este tópico e, se continuo fazendo isso, é porque observo uma procura constante pela utilização deste recurso tão simples,mas tão necessário.
Se tudo o que deseja fazer é enviar a planilha, pode usar ThisWorkbook.SendMail. No entanto, se deseja incluir um texto no corpo da mensagem ou incluir arquivos adicionais como anexos, precisará de algum código VBA.
Procurei disponibilizar a função SendEmail por ser bem amigável.
Esse código prescinde da referência ao Microsoft CDO for Windows 2000 Library. Normalmente o localizamos em C:\Windows\system32\cdosys.dll. O GUID para este componente é {CD000000-8B95-11D1-82DB-00C04FB1625D}, para Maior = 1 e Menor = 0.
Function SendEMail (Subject As String, _FromAddress As String, _ToAddress As String, _MailBody As String, _SMTP_Server As String, _BodyFileName As String, _Optional Attachments As Variant = Empty) As Boolean
Dim MailMessage As CDO.MessageDim N As LongDim FNum As IntegerDim S As StringDim Body As StringDim Recips() As StringDim Recip As StringDim NRecip As Long' ensure required parameters are present and valid.If Len(Trim(Subject)) = 0 ThenSendEMail = FalseExit FunctionEnd IfIf Len(Trim(FromAddress)) = 0 ThenSendEMail = FalseExit FunctionEnd IfIf Len(Trim(SMTP_Server)) = 0 ThenSendEMail = FalseExit FunctionEnd If' Clean up the addressesRecip = Replace(ToAddress, Space(1), vbNullString)If Right(Recip, 1) = ";" ThenRecip = Left(Recip, Len(Recip) - 1)End IfRecips = Split(Recip, ";")For NRecip = LBound(Recips) To UBound(Recips)On Error Resume Next' Create a CDO Message object.Set MailMessage = CreateObject("CDO.Message")If Err.Number <> 0 ThenSendEMail = FalseExit FunctionEnd IfErr.ClearOn Error GoTo 0With MailMessage.Subject = Subject.From = FromAddress.To = Recips(NRecip)If MailBody <> vbNullString Then.TextBody = MailBodyElseIf BodyFileName <> vbNullString ThenIf Dir(BodyFileName, vbNormal) <> vbNullString Then' import the text of the body from file BodyFileNameFNum = FreeFileS = vbNullStringBody = vbNullStringOpen BodyFileName For Input Access Read As #FNumDo Until EOF(FNum)Line Input #FNum, SBody = Body & vbNewLine & SLoopClose #FNum.TextBody = BodyElse' BodyFileName not found.SendEMail = FalseExit FunctionEnd IfEnd If ' MailBody and BodyFileName are both vbNullString.End IfIf IsArray(Attachments) = True Then' attach all the files in the array.For N = LBound(Attachments) To UBound(Attachments)' ensure the attachment file exists and attach it.If Attachments(N) <> vbNullString ThenIf Dir(Attachments(N), vbNormal) <> vbNullString Then.AddAttachment Attachments(N)End IfEnd IfNext NElse' ensure the file exists and if so, attach it to the message.If Attachments <> vbNullString ThenIf Dir(CStr(Attachments), vbNormal) <> vbNullString Then.AddAttachment AttachmentsEnd IfEnd IfEnd IfWith .Configuration.Fields' set up the SMTP configuration.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = SMTP_Server.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25.UpdateEnd WithOn Error Resume NextErr.Clear' Send the message.SendIf Err.Number = 0 ThenSendEMail = TrueElseSendEMail = FalseExit FunctionEnd IfEnd WithNext NRecipSendEMail = TrueEnd Function
Caso deseje anexar algum objeto, adicione:
ThisWorkbook.SaveThisWorkbook.ChangeFileAccess xlReadOnlyB = SendEmail( _... parameters ...Attachments:=ThisWorkbook.FullName)ThisWorkbook.ChangeFileAccess xlReadWrite
Nenhum comentário:
Postar um comentário