2016-03-04 29 views
-4

发送邮件EXCEL我是很新的VBA。我有一个已经开发了一个Excel工作表,其中我有作为然后再进行附加任务:使用VBA

我需要创建一个电子邮件按钮,点击该按钮,整个工作表应邮寄到指定的收件人,也允许我添加一个附件。

+0

请发表您的代码,或者我们不能帮你 – TylerDurden

+0

我建议你这个网站:http://www.rondebruin.nl/win/section1.htm有很多例子来说明如何使用VBA在Excel中一个邮件发送。 –

+0

https://stackoverflow.com/search?q=[vba]+send+mail –

回答

0

你好Aakash Sehgal的,

Sendmail() 
     Dim OutApp As Object 
     Dim OutMail As Object 
     Dim cell As Range 
     Application.ScreenUpdating = False 
     Set OutApp = CreateObject("Outlook.Application") 

     On Error GoTo cleanup 
     For Each cell In Columns("B").Cells.SpecialCells(xlCellTypeConstants) 
      If cell.Value Like "?*@?*.?*" And _ 
       LCase(Cells(cell.Row, "C").Value) = "yes" Then 

       Set OutMail = OutApp.CreateItem(0) 
       On Error Resume Next 
       With OutMail 
        .To = cell.Value 
        .Subject = "Aakash Sehgal" 
        .Body = "Dear " & Cells(cell.Row, "A").Value _ 
          & vbNewLine & vbNewLine & _ 
          "Please contact us to discuss bringing " & _ 
          "your account up to date" 
        'You can add files also by use: 
        '.Attachments.Add ("C:\test.txt") 
        .Send 'Or use Display 
       End With 
       On Error GoTo 0 
       Set OutMail = Nothing 
      End If 
     Next cell 

    cleanup: 
     Set OutApp = Nothing 
     Application.ScreenUpdating = True 
    End Sub 

做以下几列的ActiveSheet列表:
在列A:人的名称
在B列:E-mail地址
在列C:没有(如果该值是它会创建一个邮件)

通过对Activesheet每一行和每一宏观循环,如果在B列
和“是”,在C列,将创建一个邮件的电子邮件地址中包含了一个每个人的提醒。
如果你有在列重复的地址看看这个例子。

这是一个例子,你可以怎么做,但如果你不是要手工添加的smtp的是,可能太看看这里:

Sub SMTP_Mail_SEND() 
    Dim iMsg As Object 
    Dim iConf As Object 
    Dim strbody As String 
    ' Dim Flds As Variant 

    Set iMsg = CreateObject("CDO.Message") 
    Set iConf = CreateObject("CDO.Configuration") 

    ' iConf.Load -1 ' CDO Source Defaults 
    ' Set Flds = iConf.Fields 
    ' With Flds 
    '  .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
    '  .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") _ 
    '      = "Fill in your SMTP server here" 
    '  .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 
    '  .Update 
    ' End With 

    strbody = "Hi there" & vbNewLine & vbNewLine & _ 
       "This is line 1" & vbNewLine & _ 
       "This is line 2" & vbNewLine & _ 
       "This is line 3" & vbNewLine & _ 
       "This is line 4" 

    With iMsg 
     Set .Configuration = iConf 
     .To = "[email protected]" 
     .CC = "" 
     .BCC = "" 
     .From = """daniel"" <[email protected]>" 
     .Subject = "This is a mail generated by use manually smtp mail" 
     .TextBody = strbody 
     .Send 
    End With 

End Sub 

来源: http://www.rondebruin.nl/win/s1/cdo.htm

干杯

DanielElmnäs