2017-05-09 70 views
-1

我試圖通過電子郵件發送到表格中的所有電子郵件地址,主題行是相應的訂單號碼或數字。通過Excel表格發送電子郵件

表中有五列 - 「行號」,「訂單號」,「Suppler/Manf.Item號」,「供應商名稱」和「電子郵件地址」

有可能是重複的,但主題每個PO只能包含一次。

沒有CC,BCC或需要

的電子郵件的正文是列出PO與他們相關聯的項目。

您好,我們需要更新爲以下幾點:

EX
PO86001763
行項目2
行項目1

請發送更新爲這些線路的狀態項目。 提供以下內容:裝箱單,追蹤號碼和更新的發貨日期。

(這些能夠被編輯將是一個福音)

該表是從進口和格式宏觀製成,它總是會在相同的格式,但會包含不同的數據。數據量可以增加或減少,具體取決於一週。

這裏是我的嘗試。

Private Sub CommandButton2_Click() 
Dim subjectLine As String 
Dim bodyline As String 
Dim tb As ListObject 
Dim lineCounter As Long 
Dim myArray1, arrayCounter As Long, tempNumb As Long 
Dim nameCounter As Long 
Dim emAddress As String 
ReDim myArray1(1 To 1) 
arrayCounter = 0 
nameCounter = 1 
Dim I As Integer 
Dim X As Integer 
Dim C As Object 
Dim firstaddress As Variant 
Dim Nrow As Boolean 

Set tb = ActiveSheet.ListObjects("Table10") 

For I = 1 To ActiveSheet.ListObjects("Table10").ListRows.Count 
    emAddress = tb.DataBodyRange.Cells(I, tb.ListColumns("Email Address").Index) 
    For X = LBound(myArray1) To UBound(myArray1) 
     On Error Resume Next 
     If emAddress = myArray1(X) Then GoTo goToNext 
    Next X 
    On Error GoTo 0 
    subjectLine = "Order(s) # " 
    ReDim Preserve myArray1(1 To nameCounter) 
    myArray1(nameCounter) = emAddress 
    nameCounter = nameCounter + 1 
    lineCounter = 1 
    With tb.ListColumns("Email Address").Range 
     Set C = .Find(emAddress, LookIn:=xlValues) 
     If Not C Is Nothing Then 
      firstaddress = C.Address 
      Beep 
      arrayCounter = arrayCounter + 1 
      Do 
       Nrow = C.Row - 1 
       If lineCounter = 1 Then 
        subjectLine = subjectLine & tb.DataBodyRange.Cells (Nrow, tb.ListColumns("Order Number").Index) 
        lineCounter = lineCounter + 1 
        bodyline = "Order " & tb.DataBodyRange.Cells(Nrow, tb.ListColumns("Order Number").Index) & ", Line Number " & tb.DataBodyRange.Cells(Nrow, tb.ListColumns("Line Number").Index) 
       Else: 
        subjectLine = subjectLine & ", " & tb.DataBodyRange.Cells(Nrow, tb.ListColumns("Order Number").Index) 
        bodyline = bodyline & vbNewLine & "Order " & tb.DataBodyRange.Cells(Nrow, tb.ListColumns("Order Number").Index) & ", Line Number " & tb.DataBodyRange.Cells(Nrow, tb.ListColumns("Line Number").Index) 
       End If 

       Set C = .FindNext(C) 
      Loop While Not C Is Nothing And C.Address <> firstaddress 
     End If 
     Run SendMailFunction(emAddress, subjectLine, bodyline) 
'      Debug.Print vbNewLine 
'      Debug.Print emAddress 
'      Debug.Print "Subject: " & subjectLine 
'      Debug.Print "Body:" & vbNewLine; bodyline 
    End With 
goToNext: 
Next I 
Set C = Nothing 
End Sub 


Function SendMailFunction(emAddress As String, subjectLine As String, bodyline As String) 
Dim OutApp As Object 
Dim OutMail As Object 
Dim cell As Range 
Dim tb As ListObject 
Dim NL As String 
Dim DNL As String 
Dim I As Integer 

NL = vbNewLine 
DNL = vbNewLine & vbNewLine 
Application.ScreenUpdating = False 
Set OutApp = CreateObject("Outlook.Application") 
Set tb = ActiveSheet.ListObjects("Table10") 

For I = 1 To ActiveSheet.ListObjects("Table10").ListRows.Count 
    Set OutMail = OutApp.CreateItem(0) 
    On Error Resume Next 
    With OutMail 
     .To = emAddress 
     .Subject = subjectLine 
     .Body = "Hello, We require an update as to the following:" & DNL & bodyline _ 
       & DNL & _ 
       "Please Send an update as to the status of these line items " & _ 
       "providing the following: Packing Slips, Tracking Numbers and Updated Ship Dates." 
     .Display 
    End With 
    On Error GoTo 0 
    Set OutMail = Nothing 
Next I 

End Function 

Generated Email

TABLE IMAGE

+2

歡迎來到SO。 * IT不起作用*並不能真正幫助我們提供幫助。什麼不行?你期望發生什麼事情沒有發生?請讓我們更容易幫助你。你也可能喜歡直接循環訪問'ListObject',就像在[這裏]一樣(http://stackoverflow.com/questions/12495678/how-do-i-loop-an-excel-2010-table-by-using -his-name-column-reference#12497229) –

+1

註釋掉'On Error Resume Next' - 你有錯誤嗎? –

+0

對不起,斯科特。一行一行我得到一個表的參考錯誤。我相信我錯了。但我不確定如何正確定義它。 –

回答

0

以下代碼使用電子郵件腳本作爲函數,該函數從頂部宏調用。請點擊答案,如果這能解決您的問題

Sub findMethodINtable() 
Dim subjectLine As String 
Dim bodyline As String 
Dim tb As ListObject 
Dim lineCounter As Long 
Dim myArray1, arrayCounter As Long, tempNumb As Long 
Dim nameCounter As Long 
Dim emAddress As String 
ReDim myArray1(1 To 1) 
arrayCounter = 0 
nameCounter = 1 

Set tb = ActiveSheet.ListObjects("Table14") 


For i = 1 To ActiveSheet.ListObjects("Table14").ListRows.Count 
    emAddress = tb.DataBodyRange.Cells(i, tb.ListColumns("Email Address").Index) 
    For x = LBound(myArray1) To UBound(myArray1) 
     On Error Resume Next 
     If emAddress = myArray1(x) Then GoTo goToNext 
    Next x 
     On Error GoTo 0 
     subjectLine = "Order(s) # " 
     ReDim Preserve myArray1(1 To nameCounter) 
     myArray1(nameCounter) = emAddress 
     nameCounter = nameCounter + 1 
     lineCounter = 1 
      With tb.ListColumns("Email Address").Range 
       Set c = .Find(emAddress, LookIn:=xlValues) 
       If Not c Is Nothing Then 
        firstAddress = c.Address 
        Beep 
        arrayCounter = arrayCounter + 1 
        Do 
         nRow = c.Row - 1 
         If lineCounter = 1 Then 
          subjectLine = subjectLine & tb.DataBodyRange.Cells(nRow, tb.ListColumns("Order Number").Index) 
          lineCounter = lineCounter + 1 
          bodyline = "Order " & tb.DataBodyRange.Cells(nRow, tb.ListColumns("Order Number").Index) & ", Line Number " & tb.DataBodyRange.Cells(nRow, tb.ListColumns("Line Number").Index) 
         Else: 
          subjectLine = subjectLine & ", " & tb.DataBodyRange.Cells(nRow, tb.ListColumns("Order Number").Index) 
          bodyline = bodyline & vbNewLine & "Order " & tb.DataBodyRange.Cells(nRow, tb.ListColumns("Order Number").Index) & ", Line Number " & tb.DataBodyRange.Cells(nRow, tb.ListColumns("Line Number").Index) 
         End If 

         Set c = .FindNext(c) 
        Loop While Not c Is Nothing And c.Address <> firstAddress 
       End If 
         Run SendMailFunction(emAddress, subjectLine, bodyline) 
'      Debug.Print vbNewLine 
'      Debug.Print emAddress 
'      Debug.Print "Subject: " & subjectLine 
'      Debug.Print "Body:" & vbNewLine; bodyline 
      End With 
goToNext: 
Next i 
Set c = Nothing 
End Sub 


Function SendMailFunction(emAddress As String, subjectLine As String, bodyline As String) 
Dim OutApp As Object 
Dim OutMail As Object 
Dim cell As Range 
Dim tb As ListObject 
Dim NL As String 
Dim DNL As String 

NL = vbNewLine 
DNL = vbNewLine & vbNewLine 
Application.ScreenUpdating = False 
Set OutApp = CreateObject("Outlook.Application") 
Set tb = ActiveSheet.ListObjects("Table14") 


    Set OutMail = OutApp.CreateItem(0) 
     On Error Resume Next 
     With OutMail 
      .To = emAddress 
      .Subject = subjectLine 
      .Body = "Hello, We require an update as to the following:" & DNL & bodyline _ 
        & DNL & _ 
        "Please Send an update as to the status of these line items " & _ 
        "providing the following: Packing Slips, Tracking Numbers and Updated Ship Dates." 
      .Send 
     End With 
     On Error GoTo 0 
     Set OutMail = Nothing 



End Function 
+0

約翰,我不斷收到定義的可變錯誤,我解決每個人彈出一個,下面是未定義(儘管我已經定義了大多數)我,X,C,第一地址。 –

+0

我已經得到它輸出電子郵件。但它似乎正在使用這些數據。請參閱上面的圖片 –

+0

從頂部沒有選項的乾淨模塊中進行試用。當我嘗試去匆忙時,我通常不會花時間來定義變量。顯示在模塊頂部的選項要求所有變量在該模塊中「變暗」。或者你可以花時間讓自己變暗所有的變量。我做了很難的部分。 –

0

這對我的作品,定的表名是 「表14」

Sub wserlkug() 
Dim OutApp As Object 
Dim OutMail As Object 
Dim cell As Range 
Dim tb As ListObject 
Dim NL As String 
Dim DNL As String 

NL = vbNewLine 
DNL = vbNewLine & vbNewLine 
Application.ScreenUpdating = False 
Set OutApp = CreateObject("Outlook.Application") 
Set tb = ActiveSheet.ListObjects("Table14") 


For i = 1 To ActiveSheet.ListObjects("Table14").ListRows.Count 
    Set OutMail = OutApp.CreateItem(0) 
     On Error Resume Next 
     With OutMail 
      .To = ActiveSheet.ListObjects("Table14").DataBodyRange.Cells(i, tb.ListColumns("Email Address").Index) 
      .Subject = "Order # " & ActiveSheet.ListObjects("Table14").DataBodyRange.Cells(i, tb.ListColumns("Order Number").Index) 
      .Body = "Hello, We require an update as to the following:" & DNL & "Line #: " & ActiveSheet.ListObjects("Table14").DataBodyRange.Cells(i, tb.ListColumns("Line Number").Index) _ 
        & DNL & _ 
        "Please Send an update as to the status of these line items " & _ 
        "providing the following: Packing Slips, Tracking Numbers and Updated Ship Dates." 
      .Send 
     End With 
     On Error GoTo 0 
     Set OutMail = Nothing 
Next i 



End Sub 

實際上,你可以使用對象變量而不是ActiveSheet 「TB」。 ListObjects(「Table14」)....我放在那裏顯示如何引用表中的行和列。

+0

謝謝約翰!這工作!您是否認爲IF聲明能夠捕獲重複的採購訂單,並且能夠在一封電子郵件中包含該採購訂單的所有行號? –

+0

此外,會添加一個tblrange查找,以查找名稱確保該文件將始終找到正確的表名稱? –

+0

我會再看看它。 –

相關問題