2016-06-29 38 views
0

我已成立了一個窗體,其將數據保存到事件的詳細信息的數據表,也暫時將數據保存到一個名爲「電子郵件表」工作表並像表單一樣佈置,以便將'電子郵件表單'複製到MS Outlook電子郵件的正文中。的Excel 2013 VBA - 設置電子郵件reciepients列表(TO和CC)填充Oulook電子郵件

這完美的作品,並與我提供的電流編碼發送一個電子郵件 1收件人和抄送給另一個,但我需要發送相同的電子郵件給多個收件人。 我創建了一個稱爲「電子郵件收件人列表(同一工作簿),因爲我想爲需要它很容易更新列表中的另一片(沒有用戶將能夠在VBA編輯硬編碼)。 列A包含TO收件人列表,列B包含CC收件人列表。

我已經搜索並查看了幾個視頻和網站,但我一直無法鍛鍊如何從「電子郵件收件人列表」工作表中提取相應的列表,並在不影響現有操作的情況下填充Outlook電子郵件。我不想讓用戶點擊宏按鈕,因爲代碼打開了Outlook電子郵件。

這是我的現有代碼:

Sub log_send_reset() 
'THIS OPENS OUTLOOK WITH DETAILS OF FORM 

'WORKS with "Email Form" 
Dim SecIncNo As String 

'This bit emails the current worksheet in the body of an email as HTML 
'#If 0 Then 
Dim rng As Range 
Dim OutApp As Object 
Dim OutMail As Object 

Set rng = Nothing 
On Error Resume Next 

Set rng = Sheets("Email Form").Range("A1:AB119") 
On Error GoTo 0 

With Application 
    .EnableEvents = False 
    .ScreenUpdating = True 'ShyButterfly set this to TRUE (it was false) 
End With 

Set OutApp = CreateObject("Outlook.Application") 
Set OutMail = OutApp.CreateItem(0) 

On Error Resume Next 
With OutMail 

'This bit tells it where to send the email to, what the subject line is etc 

.to = "[email protected]" 

.CC = "[email protected]" 
.BCC = "" 
.Subject = Range("H6").value & " - " & "SAC" & Range("G12").value & " - " & Range("G14").value & " - " & Range("H8").value 
    .HTMLBody = RangetoHTML(rng) 
    'Shybutterfly changed from.Send to .Display to see what it does 
    .Display 
'or use .Display if you want to edit/add text before sending 

End With 
On Error GoTo 0 

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

Set OutMail = Nothing 
Set OutApp = Nothing 


ThisWorkbook.Save 

'ThisWorkbook.Close 

'Application.Quit 


End Sub 

enter image description here

我會很感激的任何援助。

回答

0

這會給你創建收件人列表你。

EmailTo = getRecipients(1)

EmailCC = getRecipients(2)

Function getRecipients(vColumn As Variant) As String 
    Dim rListColumn As Range 
    Dim c As Range 
    Dim s As String 
    With Worksheets("Email Recipient List") 

     Set rListColumn = .Range(.Cells(2, vColumn), .Cells(Rows.Count, vColumn).End(xlUp)) 

     For Each c In rListColumn 
      s = s & c.Text & ";" 
     Next 

     getRecipients = Left(s, Len(s) - 1) 
    End With 

End Function 

我沒看到,你會得到一個變量沒有定義的錯誤。如果getRecipients在一個私人模塊中,你會得到一個子或函數未定義的錯誤。

我重構我們的代碼。請在代碼模塊中自行運行ComposeEmail。

 
    Option Explicit 
    Public Sub ComposeEmail() 

     ToggleEvents False 

     Dim EmailTo As String, CC As String, BCC As String, Subject As String, HTMLBody As String, ShowEmail As Boolean 
     Dim rng As Range 

     ToggleEvents False 

     Set rng = Sheets("Email Form").Range("A1:AB119") 

     EmailTo = getRecipients(1) 
     CC = getRecipients(2) 
     'BCC = getRecipients(2) 
     Subject = Range("H6").Value & " - " & "SAC" & Range("G12").Value & " - " & Range("G14").Value & " - " & Range("H8").Value 
     HTMLBody = RangetoHTML2(rng) 
     ShowEmail = True 

     SendMail EmailTo, CC, BCC, Subject, HTMLBody, ShowEmail 

     ' ThisWorkbook.Close True 'This Line save and Closes the workbook 

     ToggleEvents True 

    End Sub 

    Function getRecipients(vColumn As Variant) As String 
     Dim rListColumn As Range 
     Dim c As Range 
     Dim s As String 
     With Worksheets("Email Recipient List") 

      Set rListColumn = .Range(.Cells(2, vColumn), .Cells(Rows.Count, vColumn).End(xlUp)) 

      For Each c In rListColumn 
       s = s & c.Text & ";" 
      Next 

      getRecipients = Left(s, Len(s) - 1) 
     End With 

    End Function 

    Public Sub SendMail(EmailTo As String, CC As String, BCC As String, Subject As String, HTMLBody As String, ShowEmail As Boolean) 
     Dim OutApp As Object 
     Dim OutMail As Object 
     Set OutApp = CreateObject("Outlook.Application") 
     Set OutMail = OutApp.CreateItem(0) 
     With OutMail 
      .to = EmailTo 
      .CC = CC 
      .BCC = BCC 
      .Subject = Subject 
      .HTMLBody = HTMLBody 

      If ShowEmail Then 
       .Display 
      Else 
       .Send 
      End If 

     End With 

     Set OutMail = Nothing 
     Set OutApp = Nothing 
     Exit Sub 
    EmailCouldNotBeCreated: 
     MsgBox "Email could not be created", vbCritical, "Error in Sub SendMail" 
    End Sub 

    Sub ToggleEvents(bEnableEvents As Boolean) 
     With Application 
      .EnableEvents = bEnableEvents 
      .ScreenUpdating = bEnableEvents 
     End With 
    End Sub 


    ' https://msdn.microsoft.com/en-us/library/ff519602%28v=office.11%29.aspx?f=255&MSPPError=-2147217396 

    Function RangetoHTML2(rng As Range) 
    ' Works in Excel 2000, Excel 2002, Excel 2003, Excel 2007, Excel 2010, Outlook 2000, Outlook 2002, Outlook 2003, Outlook 2007, and Outlook 2010. 
     Dim fso As Object 
     Dim ts As Object 
     Dim TempFile As String 
     Dim TempWB As Workbook 

     TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm" 

     ' Copy the range and create a workbook to receive the data. 
     rng.Copy 
     Set TempWB = Workbooks.Add(1) 
     With TempWB.Sheets(1) 
      .Cells(1).PasteSpecial Paste:=8 
      .Cells(1).PasteSpecial xlPasteValues, , False, False 
      .Cells(1).PasteSpecial xlPasteFormats, , False, False 
      .Cells(1).Select 
      Application.CutCopyMode = False 
      On Error Resume Next 
      .DrawingObjects.Visible = True 
      .DrawingObjects.Delete 
      On Error GoTo 0 
     End With 

     ' Publish the sheet to an .htm file. 
     With TempWB.PublishObjects.Add(_ 
      SourceType:=xlSourceRange, _ 
      Filename:=TempFile, _ 
      Sheet:=TempWB.Sheets(1).Name, _ 
      Source:=TempWB.Sheets(1).UsedRange.Address, _ 
      HtmlType:=xlHtmlStatic) 
      .Publish (True) 
     End With 

     ' Read all data from the .htm file into the RangetoHTML subroutine. 
     Set fso = CreateObject("Scripting.FileSystemObject") 
     Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2) 
     RangetoHTML = ts.ReadAll 
     ts.Close 
     RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _ 
           "align=left x:publishsource=") 

     ' Close TempWB. 
     TempWB.Close savechanges:=False 

     ' Delete the htm file. 
     Kill TempFile 

     Set ts = Nothing 
     Set fso = Nothing 
     Set TempWB = Nothing 
    End Function 

+0

謝謝你的快速反應。我將函數添加到模塊中,並將子項中的To和CC替換爲EmailTo和EmailCC,但我無法使其工作。我收到錯誤「變量未定義」。我試圖通過Dim EmailTo將其定義爲字符串(等),但沒有奏效。我有點困惑如何做到這一點。請指教。乾杯 – TheShyButterfly

0

無論打電話MailItem.Recipients.Add爲每個收件人(它返回Recipient對象,適當地設定其Recipient.Type屬性olTo/OLCC/olBCC)到To/CC/BCC屬性設置爲 「;」分開的地址列表。

+0

嗨德米特里,謝謝你的迴應。對不起,但這讓我有些困惑。根據我現有的代碼,這將如何設置?這是基於使用@ThomasInzina函數嗎?不需要定義變量嗎? (我仍然在努力把握VBA--對我來說這是一個緩慢的過程)。 – TheShyButterfly

+0

是的,你會需要遍歷該範圍內的值並調用Recipients.Add每個值。或者建立一個由「;」分隔的地址串。 –