|
楼主 |
发表于 2016-8-9 12:57
|
显示全部楼层
附件代码:
- Sub SendEmail_All(EmailPort, MailServer, shtname)
- 'MsgBox shtname
- 'Unload UserForm3
- r1 = Worksheets("UK").[a65536].End(xlUp).Row ' 开始全部客户占的最后行数
- c = Range("iv1").End(xlToLeft).Column
- Dim NameSpace$, Email As Object, ns$, n%
- NameSpace = "http://schemas.microsoft.com/cdo/configuration/"
- Set Email = CreateObject("CDO.Message")
- ' UserForm1.Show 0 '激活进度条
- ' '设置进度条控件
- ' With UserForm1.ProgressBar1
- ' .Min = 1 '设置进度条控件的最小值
- ' .Max = r1 '设置进度条控件的最大值
- ' .Scrolling = 0
- ' End With
- ' For i = 2 To r1 '按照准备发送的客户数
- 's2 = hbdyg(Range(Cells(i, 3), Cells(i, c)), "|")
- ''发送准备工作
- Email.From = Worksheets("查询按钮").Range("b2") 'WorkSheets("查询按钮").Range("g2") & "@qq.com" '发件人QQ邮箱=“辅助工作表”G2
- Email.To = Mail_Arr(d_Mail(shtname), 2) ' Worksheets("查询按钮").Range("b9") '要发往的地址=“辅助工作表”D列
- Email.cc = Mail_Arr(d_Mail(shtname), 3) ' "jianming.tang@chukou1.com" ' Mail_Arr(d_Mail(shtname), 3)
- If shtname = "各仓汇总" Then
- If Mail_Arr(d_Mail(shtname), 5) <> "" Then
- Email.Subject = Mail_Arr(d_Mail(shtname), 5)
- Else
- Email.Subject = Format(Now() - 1, "m.d") & "海外仓内部结算日报表"
- End If
- If Mail_Arr(d_Mail(shtname), 6) <> "" Then
- Email.Htmlbody = "<p>" & Worksheets("查询按钮").Range("F9") & "</p>" & getval(shtname) ' Worksheets("查询按钮").Range("F5") & "<br/>" & getval(shtname) 'getVal(i) 'HTML形式正文(加入表格部分行)
- Else
- Email.Htmlbody = "<p>" & "Dear All: " & "</p><p> " & " 附件是截止" & Format(Now() - 1, "yy年m月d日") & "海外仓内部结算收入日报表(含各仓数据、汇总数据),请查收,谢谢!" & "</p><p>" _
- & " 备注:线下登记的FBA出库数据截止" & Format(Now() - 1, "d日") & ",专线数据截止" & Format(Now() - 1, "d日") & "。" & "</p>" & getval(shtname)
- End If
- Email.AddAttachment ThisWorkbook.Path & "\海外仓内部结算收入日报" & Format(Now() - 1, "yy年m月") & "内部结算收入-" & Format(Now() - 1, "m.d") & ".xlsx" '添加附件
- Else
- If Mail_Arr(d_Mail(shtname), 5) <> "" Then
- Email.Subject = Mail_Arr(d_Mail(shtname), 5)
- Else
- Email.Subject = Format(Now() - 1, "m.d") & shtname & " 仓海外仓内部结算日报表" '标题=“辅助工作表”G4
- End If
- If Mail_Arr(d_Mail(shtname), 6) <> "" Then
- Email.Htmlbody = "<p>" & Worksheets("查询按钮").Range("F9") & "</p>" & getval(shtname) ' Worksheets("查询按钮").Range("F5") & "<br/>" & getval(shtname) 'getVal(i) 'HTML形式正文(加入表格部分行)
- Else
- Email.Htmlbody = "<p>" & "Dear All: " & "</p><p> " & " 附件是截止" & Format(Now() - 1, "yy年m月d日") & "海外仓内部结算收入日报表(币种:人民币),请查收,谢谢!" & "</p>" & getval(shtname)
- ' Email.Htmlbody = "Dear All: " & vbCrLf & " 附件是截止" & Format(Now() - 1, "yy年m月d日") & "海外仓内部结算收入日报表(币种:人民币),请查收,谢谢!" & vbCrLf & getval(shtname)
- End If
- Email.AddAttachment ThisWorkbook.Path & "\海外仓内部结算收入日报" & shtname & "仓" & Format(Now() - 1, "m月") & "内部结算收入-" & Format(Now() - 1, "m.d") & ".xlsx" '添加附件
- End If
- 'Email.Textbody =WorkSheets("UK").Range("a" & i) & Chr(10) & s1 & Chr(10) & s2 '文本形式正文
- If shtname = "UK" Then
- Email.Subject = Format(Now() - 1, "m.d") & "UK warehouse internal settlement daily report "
- Email.Htmlbody = "<p>" & "Hi Steve: " & "</p><p> " & " Attachment is on" & Format(Now() - 1, "mmmm dd") & ", income from settlement of warehouse internal daily report , thank you!" & "</p>" & getval(shtname)
- End If
- With Email.Configuration.Fields
- .Item(NameSpace & "smtpusessl") = 1
- .Item(NameSpace & "sendusing") = 2
- .Item(NameSpace & "smtpserver") = MailServer '"smtp.qq.com" '发送邮件服务器
- .Item(NameSpace & "smtpserverport") = EmailPort ' "465"
- .Item(NameSpace & "smtpauthenticate") = 1
- .Item(NameSpace & "sendusername") = Worksheets("查询按钮").Range("b2") ' WorkSheets("查询按钮").Range("g2") & "@qq.com" '发件人QQ邮箱
- .Item(NameSpace & "sendpassword") = Worksheets("查询按钮").Range("b3") '发件人QQ密码
- .Update
- End With
- Email.send ''发送
- ' '进度条
- ' UserForm1.ProgressBar1.Value = 1
- ' UserForm5.Caption = Worksheets("UK").Range("c" & i) & " 的邮件(含附件)已发送。请稍候!"
- DoEvents '把控制权交给VBA
- ' Next i
- Unload UserForm1
- 'MsgBox "文件已经全部发送完成。"
- '
- 100:
- If Err.Number <> 0 Then '处理错误
- If Err.Number = -2147220977 Then
- MsgBox "收信人地址 “" & Add & "” 错误! "
- ElseIf Err.Number = -2147220980 Then
- MsgBox "错误! 收信人地址未填写"
- ElseIf Err.Number = -2147220973 Then
- MsgBox "网络未连接! "
- Else
- MsgBox "其他错误!(超时、附件太大、邮箱已满) "
- End If
- End
- End If
- End Sub
- Sub SendEmail(ByVal shtname As String)
- Dim sht$, i%, EmailType$, EmailPort$, MailServer$, Arr
- Set d_Mail = CreateObject("scripting.dictionary")
- With Worksheets("查询按钮")
- Mail_Arr = .Range("a9:f16")
- For i = 1 To UBound(Mail_Arr)
- d_Mail(Mail_Arr(i, 1)) = i
- Next
- EmailType = .Range("b1")
- Select Case EmailType
- Case "qq"
- EmailPort = "465"
- MailServer = "smtp.qq.com"
- Case "企业邮箱"
- EmailPort = "465"
- MailServer = "smtp.exmail.qq.com"
- Case "126"
- EmailPort = "465"
- MailServer = "smtp.126.com"
- Case "163"
- EmailPort = "465"
- MailServer = "smtp.163.com"
- Case "gmail"
- EmailPort = "465"
- MailServer = "smtp.gmail.com"
- End Select
- ' shtname = "UK"
- Call SendEmail_All(EmailPort, MailServer, shtname)
- End With
- End Sub
- Sub 发送邮件_最终()
- ' Dim myForm As 发送邮件
- ' Set myForm = New 发送邮件
- ' myForm.show
- 发送邮件.show
- ' Set myForm = Nothing
- End Sub
复制代码 |
|