Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data can be sent from cells, for example C2:H5, and the recipient receives a letter with the rows arranged like this:
how they are located and at the sender, and not in one line.
Thank you.
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data can be sent from cells, for example C2:H5, and the recipient receives a letter with the rows arranged like this:
how they are located and at the sender, and not in one line.
Thank you.
Code:
Sub Send_Mail()
Const CDO_Cnf = "http://schemas.microsoft.com/cdo/configuration/"
Dim oCDOCnf As Object, oCDOMsg As Object
Dim SMTPserver As String, sUsername As String, sPass As String, sMsg As String
Dim sTo As String, sFrom As String, sSubject As String, sBody As String, sAttachment As String
On Error Resume Next
'sFrom - как правило совпадает с sUsername
SMTPserver = [B10] ' SMTPServer: для Mail.ru "smtp.mail.ru"; для Яндекса "smtp.yandex.ru"; для Рамблера "mail.rambler.ru"
sUsername = [B11] ' Учетная запись на сервере
sPass = [B12] ' Пароль к почтовому аккаунту
If Len(SMTPserver) = 0 Then MsgBox "Не указан SMTP сервер", vbInformation, "www.Excel-VBA.ru": Exit Sub
If Len(sUsername) = 0 Then MsgBox "Не указана учетная запись", vbInformation, "www.Excel-VBA.ru": Exit Sub
If Len(sPass) = 0 Then MsgBox "Не указан пароль", vbInformation, "www.Excel-VBA.ru": Exit Sub
sTo = [B2] 'Кому
sFrom = [B3] 'От кого
sSubject = [B4] 'Тема письма
sBody = [B5] 'Текст письма
sAttachment = [B6] 'Вложение(полный путь к файлу)
'Назначаем конфигурацию CDO
Set oCDOCnf = CreateObject("CDO.Configuration")
With oCDOCnf.Fields
.Item(CDO_Cnf & "sendusing") = 2
.Item(CDO_Cnf & "smtpauthenticate") = 1
.Item(CDO_Cnf & "smtpserver") = SMTPserver
'если необходимо указать SSL
'.Item(CDO_Cnf & "smtpserverport") = 465 'для Яндекса и Gmail 465
'.Item(CDO_Cnf & "smtpusessl") = True
'=====================================
.Item(CDO_Cnf & "sendusername") = sUsername
.Item(CDO_Cnf & "sendpassword") = sPass
.Update
End With
'Создаем сообщение
Set oCDOMsg = CreateObject("CDO.Message")
With oCDOMsg
Set .Configuration = oCDOCnf
.BodyPart.Charset = "koi8-r"
.From = sFrom
.To = sTo
.Subject = sSubject
.TextBody = sBody
'Проверка наличия файла по указанному пути
If Len(sAttachment) > 0 Then
If Dir(sAttachment, 16) <> "" Then
.AddAttachment sAttachment
End If
End If
.Send
End With
Select Case Err.Number
Case -2147220973: sMsg = "Нет доступа к Интернет"
Case -2147220975: sMsg = "Отказ сервера SMTP"
Case 0: sMsg = "Письмо отправлено"
Case Else: sMsg = "Ошибка номер: " & Err.Number & vbNewLine & "Описание ошибки: " & Err.Description
End Select
MsgBox sMsg, vbInformation, "www.Excel-VBA.ru"
Set oCDOMsg = Nothing: Set oCDOCnf = Nothing
End Sub
'---------------------------------------------------------------------------------------
' Procedure : Get_File_Path
' Purpose : Процедура выбора файла
'---------------------------------------------------------------------------------------
Sub Get_File_Path()
Dim sPath
sPath = Application.GetOpenFilename("All Files(*.*),*.*", , "Выбрать файлы", "Выбрать", False)
If sPath = False Then Exit Sub
[B6] = sPath
End Sub