473,544 Members | 2,398 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sending mail to Excel via CDO of a given range

1 New Member
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 11265

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

Similar topics

0
386
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: <$U@$DOMAIN>\n";
3
57601
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 unhandled by user code Message="Failure sending mail." Source="System" StackTrace: at System.Net.Mail.SmtpClient.Send(MailMessage message) at...
3
1655
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 not password. Jakob
2
10130
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 modify the SMTP settings in IIS. But with this install, there is no IIS (at least I can't find an IIS with MMC browsing the snap-ins). I suspect that...
5
2152
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" myMessage.Subject = "Automatic sending mail" myMessage.Body = "Success!" Mail.SmtpMail.Send(myMessage)
2
1337
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 recipient recieves the mail in Spam. Can anybody help me out. Dim mail1 As System.Web.Mail.MailMessage = New System.Web.Mail.MailMessage ...
2
6812
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 to fix this error. I am able to send messages sometimes both other times randomly following error appear EXCEPTION:
2
6056
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 System.Net.NetworkCredential("user", "password"); try {
9
3439
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 oSMTP.Send(oMailMessage) (in this line I am getting the above err)
0
1263
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 issue is occurring when I'm sending mail using above given plugin I'm not receiving attachment and when I'm simply put <input type="file"/> it works...
0
7447
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7638
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7400
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7732
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5314
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4935
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3429
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1000
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.