2015-08-29 39 views
0

我正在嘗試設置一個VB代碼,可以根據表1中列「L」中給出的特定電子郵件地址發送電子郵件。我面臨的挑戰是添加「.CC」一行。我想要的'抄送'列表的'電子郵件'地址可用在恥辱excel表「Sheet01」的列M用於發送電子郵件的Excel VB代碼:將電子郵件添加到CC

有人可以建議適當的編碼將電子郵件拉到CC線嗎?

注意:CC列表(列M)的長度不是靜態的或不同的。

謝謝

Sub CDO_Personalized_Mail_Body() 
Dim iMsg As Object 
Dim iConf As Object 
Dim cell As Range 
    Dim Flds As Variant 

With Application 
    .ScreenUpdating = False 
    .EnableEvents = False 
End With 

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/smtpusessl") = True 
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "[email protected]" 
    .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "[email protected]" 
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com" 
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465 
    .Update 
End With 

For Each cell In Sheets("Sheet1").Columns("L").Cells 
     If cell.Value Like "?*@?*.?*" Then 
      Set iMsg = CreateObject("CDO.Message") 
      With iMsg 
       Set .Configuration = iConf 
       .To = cell.Value 
       .From = """Test User"" <[email protected]>" 
       .CC = Sheets("Sheet1").Columns("M").Cells ' **here i want Insert CC line Email ID** 
       .Subject = "***Important - Email Alert***" 
       .TextBody = "Hi " & vbNewLine & vbNewLine & _ 
       "This is Auto genrated email " & cell.Offset(0, 2).Value & vbNewLine & vbNewLine & _ 
          "Thank You" 
       .Send 
      End With 
      Set iMsg = Nothing 
    End If 
Next cell 

With Application 
    .EnableEvents = True 
    .ScreenUpdating = True 
End With 

末次

回答

0

一系列具有返回其第一行的行號.Row財產。所以對於一個單元格,cell.Row給出了它的行。

Set sh = ActiveWorkbook.Worksheets("Sheet1") 

For Each cell In sh.UsedRange.Columns("L").Cells 
    ' CC cell: same row, column M 
    Set cellCC = sh.Cells(cell.Row, "M") 
    Debug.Print cell.Row, cellCC.Value 
Next cell 

請注意,您應該有一個Sheet.UsedRange。在那裏,或者For Each將循環遍及紙張的整個1,048,576行。

對於這種類型的任務,我寧願不使用For Each,而是像以下這樣的行上的「標準」循環,恕我直言,它更好的可讀性。

For y = 1 To sh.Columns("L").SpecialCells(xlCellTypeLastCell).Row 
    Debug.Print y, sh.Cells(y, "L").Value, sh.Cells(y, "M").Value 
Next y 
+0

謝謝你的回覆@ Andre451 ..我會測試這個並讓你知道 – Tayyab

相關問題