2017-05-24 32 views
2

我已經編寫了代碼,將製造商名稱的數據導出到爲製造商命名的新書中。將相關excel文件附加到自動發送電子郵件

現在我已經調整了一個電子郵件宏來自動給製造商發郵件。

我想它會自動從我的文檔

附上我的文件,這裏是我的,但它重視什麼。

Sub BacklogEmail() 
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("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 = "Obsolescence Report for Manufacturer(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("Manufacturer Name").Index) 
          lineCounter = lineCounter + 1 
          ' bodyline = "Manufacturer " & tb.DataBodyRange.Cells(Nrow, tb.ListColumns("Manufacturer Name").Index) & ", Manufacturer Item Number " & tb.DataBodyRange.Cells(Nrow, tb.ListColumns("Manufacturer Item Number").Index) 
         Else: 
          subjectLine = subjectLine 
          'bodyline = bodyline & vbNewLine & "Manufacturer " & tb.DataBodyRange.Cells(Nrow, tb.ListColumns("Manufacturer Name").Index) & ", Manufacturer Item Number " & tb.DataBodyRange.Cells(Nrow, tb.ListColumns("Manufacturer Item 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 lineCounter As Long 
Dim myArray1, arrayCounter As Long, tempNumb As Long 
Dim nameCounter As Long 
ReDim myArray1(1 To 1) 
arrayCounter = 0 
nameCounter = 1 

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

      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 
         Nrow = C.Row - 1 
         If lineCounter = 1 Then 
         Set OutMail = OutApp.CreateItem(0) 
     On Error Resume Next 
     With OutMail 

      .To = emAddress 
      .Subject = subjectLine 
      .Body = "Hello, attached is an excel file that we require you to complete. " & _ 
        "This is required by as we must know when parts are going to become obsolete. " & _ 
        "We appriciate your contribution to keeping our databases current. " & _ 
        "Thank you for your timely response." 
          .Attachments.Add "U:\\\\" & tb.DataBodyRange.Cells(Nrow, tb.ListColumns("Manufacturer Name").Index) & ".xlsx" 
          lineCounter = lineCounter + 1 

      .Display 


    On Error GoTo 0 
     Set OutMail = Nothing 


End With 
End If 
End If 
End With 
End Function 
+1

你在做什麼'Format(Now(),「********」)'?這完全沒有結果? – CLR

+1

您的程序正試圖將一個名爲'C:\ Users \ dmack \ my Documents \ Manufacturer Name.xlsx'的文件附加到每封郵件。據推測,這是不存在的,因此無法附加它。 – CLR

+0

...並且您沒有收到錯誤,因爲您使用了「On Error Resume Next」來禁用它們。 – CLR

回答

0

繼承人答案完全正常工作,並能夠通過電子郵件列表循環併發送所需的excel文件。它會在5分鐘內發送200封電子郵件。正確。歡呼所有幫助!

Sub BacklogEmail() 
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("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 = "Update Required For on 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("Manufacturer Name").Index) 
          lineCounter = lineCounter + 1 
          bodyline = "Order " & tb.DataBodyRange.Cells(Nrow, tb.ListColumns("Manufacturer Name").Index) & ", Manufacturer Item Number " & tb.DataBodyRange.Cells(Nrow, tb.ListColumns("Manufacturer Item Number").Index) 
         Else: 
          subjectLine = subjectLine 
          bodyline = bodyline & vbNewLine & "Order " & tb.DataBodyRange.Cells(Nrow, tb.ListColumns("Manufacturer Name").Index) & ", Manufacturer Item Number " & tb.DataBodyRange.Cells(Nrow, tb.ListColumns("Manufacturer Item Number").Index) 
         End If 

         Set C = .FindNext(C) 
         Debug.Print vbNewLine 
         Debug.Print emAddress 
         Debug.Print "Subject: " & subjectLine 
         Debug.Print "Body:" & vbNewLine; bodyline 
        Loop While Not C Is Nothing And C.Address <> firstaddress 
       End If 

         Run SendMailFunction(emAddress, subjectLine, 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 lineCounter As Long 
Dim myArray1, arrayCounter As Long, tempNumb As Long 
Dim nameCounter As Long 
ReDim myArray1(1 To 1) 
arrayCounter = 0 
nameCounter = 1 

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

      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 
         Nrow = C.Row - 1 
         If lineCounter = 1 Then 

     Set OutMail = OutApp.CreateItem(0) 
     On Error Resume Next 
     With OutMail 

      .To = emAddress 
      .Subject = subjectLine 
      .Body = "Hello, attached is an excel file that we require you to complete. " & _ 
        "This is required by as we must know when parts are going to become obsolete. " & DNL & _ 
        "We appriciate your contribution to keeping our databases current. " & DNL & _ 
        "Thank you for your timely response." 
      .Attachments.Add ":\\\\\" & tb.DataBodyRange.Cells(Nrow, tb.ListColumns("Manufacturer Name").Index) & ".xlsx" 
          lineCounter = lineCounter + 1 

      .Display 

     End With 
    On Error GoTo 0 
     Set OutMail = Nothing 


End If 
End If 
End With 
End Function 
0

attach.add行更改爲:

Debug.Print "C:\users\dmack\my documents\" & tb.DataBodyRange.Cells(Nrow, tb, ListColumns("Manufacturer Name").Index) 

,如果你開始看到正確的文件路徑\在立即窗口的文件名,然後再改變它,到:

.Attachments.Add "C:\users\dmack\my documents\" & tb.DataBodyRange.Cells(Nrow, tb, ListColumns("Manufacturer Name").Index) 
+0

所以我讓它自動指向正確的文件,但它不會附加文件。當我將debug.print更改爲attachments.add時,它也將不再打開電子郵件。編輯代碼見上面 –

+0

當你使用debug.print時,什麼被寫入立即窗口?它是一個有效的路徑/文件名? – CLR

+0

是的,我更新了代碼。現在重視。但打破了我的循環發送多封電子郵件。它現在只生成一個,你能看到返回到第一部分的哪裏出錯了嗎? –