473,416 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Sending mail to Excel via CDO of a given range

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.

Expand|Select|Wrap|Line Numbers
  1. Sub Send_Mail()
  2.     Const CDO_Cnf = "http://schemas.microsoft.com/cdo/configuration/"
  3.     Dim oCDOCnf As Object, oCDOMsg As Object
  4.     Dim SMTPserver As String, sUsername As String, sPass As String, sMsg As String
  5.     Dim sTo As String, sFrom As String, sSubject As String, sBody As String, sAttachment As String
  6.     On Error Resume Next
  7.     'sFrom - как правило совпадает с sUsername
  8.     SMTPserver = [B10]    ' SMTPServer: для Mail.ru "smtp.mail.ru"; для Яндекса "smtp.yandex.ru"; для Рамблера "mail.rambler.ru"
  9.     sUsername = [B11]   ' Учетная запись на сервере
  10.     sPass = [B12]    ' Пароль к почтовому аккаунту
  11.  
  12.     If Len(SMTPserver) = 0 Then MsgBox "Не указан SMTP сервер", vbInformation, "www.Excel-VBA.ru": Exit Sub
  13.     If Len(sUsername) = 0 Then MsgBox "Не указана учетная запись", vbInformation, "www.Excel-VBA.ru": Exit Sub
  14.     If Len(sPass) = 0 Then MsgBox "Не указан пароль", vbInformation, "www.Excel-VBA.ru": Exit Sub
  15.  
  16.     sTo = [B2]    'Кому
  17.     sFrom = [B3]    'От кого
  18.     sSubject = [B4]    'Тема письма
  19.     sBody = [B5]    'Текст письма
  20.     sAttachment = [B6]    'Вложение(полный путь к файлу)
  21.  
  22.     'Назначаем конфигурацию CDO
  23.     Set oCDOCnf = CreateObject("CDO.Configuration")
  24.     With oCDOCnf.Fields
  25.         .Item(CDO_Cnf & "sendusing") = 2
  26.         .Item(CDO_Cnf & "smtpauthenticate") = 1
  27.         .Item(CDO_Cnf & "smtpserver") = SMTPserver
  28.         'если необходимо указать SSL
  29.         '.Item(CDO_Cnf & "smtpserverport") = 465 'для Яндекса и Gmail 465
  30.         '.Item(CDO_Cnf & "smtpusessl") = True
  31.         '=====================================
  32.         .Item(CDO_Cnf & "sendusername") = sUsername
  33.         .Item(CDO_Cnf & "sendpassword") = sPass
  34.         .Update
  35.     End With
  36.     'Создаем сообщение
  37.     Set oCDOMsg = CreateObject("CDO.Message")
  38.     With oCDOMsg
  39.         Set .Configuration = oCDOCnf
  40.         .BodyPart.Charset = "koi8-r"
  41.         .From = sFrom
  42.         .To = sTo
  43.         .Subject = sSubject
  44.         .TextBody = sBody
  45.         'Проверка наличия файла по указанному пути
  46.         If Len(sAttachment) > 0 Then
  47.             If Dir(sAttachment, 16) <> "" Then
  48.                 .AddAttachment sAttachment
  49.             End If
  50.         End If
  51.         .Send
  52.     End With
  53.  
  54.     Select Case Err.Number
  55.     Case -2147220973: sMsg = "Нет доступа к Интернет"
  56.     Case -2147220975: sMsg = "Отказ сервера SMTP"
  57.     Case 0: sMsg = "Письмо отправлено"
  58.     Case Else: sMsg = "Ошибка номер: " & Err.Number & vbNewLine & "Описание ошибки: " & Err.Description
  59.     End Select
  60.     MsgBox sMsg, vbInformation, "www.Excel-VBA.ru"
  61.     Set oCDOMsg = Nothing: Set oCDOCnf = Nothing
  62. End Sub
  63.  
  64. '---------------------------------------------------------------------------------------
  65. ' Procedure : Get_File_Path
  66. ' Purpose   : Процедура выбора файла
  67. '---------------------------------------------------------------------------------------
  68. Sub Get_File_Path()
  69.     Dim sPath
  70.     sPath = Application.GetOpenFilename("All Files(*.*),*.*", , "Выбрать файлы", "Выбрать", False)
  71.     If sPath = False Then Exit Sub
  72.     [B6] = sPath
  73. End Sub
Mar 4 '24 #1
0 11199

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: >>Shailesh | last post by:
hi, my script(IMAP email client) is sending mail using imap_mail() function. imap_mail($To, $Subject, $Body, $headers); $headers = "From: <$From>\n"; $headers .= "X-Sender:...
3
by: HoustonComputerGuy | last post by:
I am working on getting my web applications moved to .Net 2.0 and am having some problems with System.Net.Mail. I get the following error when sending the mail: System.Net.Mail.SmtpException was...
3
by: at | last post by:
Hi, I need to test a mail function through my IPS, who doesn't allow sending mail without a password. Where do I specify this password? In php.ini I can specify SMTP and sender, but apparently...
2
by: HK | last post by:
In VB.NET, I'm getting the exception "failure sending mail". I'm running VS 2005 on XP Home. This is a new install on a new PC. I've never had email problems with VS 2003, and there I could...
5
by: Zile | last post by:
I am trying to send mail from web page in asp.net 2.0/VB 2005: Dim Poruka As New System.Web.Mail.MailMessage myMessage.From = "matematic@gmail.com" myMessage.To = "matematic@hotmail.com"...
2
by: mkadasi | last post by:
Hello Everyone, I facing a problem in sending mail through vb.net. The code written below sends the mail to the respective person specified in the 'TO' address. But the problem is that the...
2
by: satnamsarai | last post by:
Using System.Net.Mail: Sometimes I get error 'failure sending mail. Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host.' Not sure how...
2
by: Danny | last post by:
Hi all, Trying to send mail with System.Net.SmtpClient, using very simple code just for testing: SmtpClient smtp = new SmtpClient("mail.server.com", 25); smtp.Credentials = new...
9
by: JoeP | last post by:
Hi All, How can I find the reason for such an error: Failure sending mail. Some Code... oMailMessage.IsBodyHtml = False oMailMessage.Body = cEmailBody Dim oSMTP As New SmtpClient...
0
by: TanuLamba15 | last post by:
Hi All, Can anyone guide me I want to send attachment with my mail and I'm using http://demo.tutorialzine.com/2013/05/mini-ajax-file-upload-form/ plugin instead of simple <input type="file"/>, the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.