Private Sub SendToAll_Click()
Dim strErrMsg As String 'For Error Handling
Dim olApp As New Outlook.Application
Dim olNameSpace As Outlook.NameSpace
Dim olMail As Outlook.MailItem
Dim oleGrf As Object
Dim strFileName As String
Dim MySQL As String, j, i As Integer
On Error GoTo MailError
Set olNameSpace = olApp.GetNamespace("MAPI")
For j = 0 To CustList.ListCount - 1
'=================循环处理客户,每客户一个EXCEL表,命名为客户名+日期
For i = 0 To TabList.ListCount - 1 '============循环处理表
MySQL = "select * from TempExcelTab_" & TabList.Column(0, i) &
" WHERE 客户='" & CustList.Column(0, j) & "'"
'=======导出到EXCEL
If HasQuery(TabList.Column(1, i)) Then DoCmd.DeleteObject acQuery, TabList.Column(1, i)
CurrentDb.CreateQueryDef TabList.Column(1, i), MySQL
DoCmd.TransferSpreadsheet transfertype:=acExport, tablename:=CurrentDb.QueryDefs(TabList.Column(1, i)).Name, _
FileName:=tFilePath & CustList.Column(0, j) & Format(Date, "YYYYMMDD") & ".xls", Hasfieldnames:=True, SpreadsheetType:=5
'DoCmd.Close acQuery, TabList.Column(1, i), acSaveNo
Next i
'==================循环处理完一个表
SysCmd acSysCmdSetStatus, "正在发送邮件给" & CustList.Column(0, j) & "..."
Me.LabState.Caption = "正在发送邮件给" & CustList.Column(0, j) & "...": Me.Repaint
'===========自动Mail过程开始﹍
Set olMail = olApp.CreateItem(olMailItem)
strFileName = tFilePath & CustList.Column(0, j) &
Format(Date, "YYYYMMDD") & ".xls"
With olMail
.To = Trim(CustList.Column(1, j))
If Not ((Nz(CustList.Column(2, j), "NO") = "NO") And
(Trim(CustList.Column(2, j)) <> "")) Then
.CC = CustList.Column(2, j)
End If
.Subject = "Customer's automation docs from TaiShan ELECTRONIC CO.,LTD"
.Body = CustList.Column(3, j) & vbNewLine & vbNewLine & "Hello," & vbNewLine _
& "The attachments are made by automation,if you find any errors,please submit your advises." _& vbNewLine & vbNewLine & "Best RGS!"
.Attachments.Add strFileName
.ReadReceiptRequested = False
.Send
End With
'Kill strFileName ' 处理完后删除文件
Set olMail = Nothing
Set oleGrf = Nothing
StateList.RowSource = CustList.Column(0, j) & "; 成功发送!;" & StateList.RowSource
SysCmd acSysCmdSetStatus, "正在发送邮件给" & CustList.Column(0, j) & "......成功"
Me.LabState.Caption = "正在发送邮件给" & CustList.Column(0, j) &
"......成功": Me.Repaint
Me.LabState.Caption = " ": Me.Repaint
'===========Email过程结束
Next j '====================处理完一个客户
MailError: Set olApp = Nothing
Set olApp = Nothing
SysCmd acSysCmdClearStatus
End Sub
==================================================
==================================================
'SendNotesMail "Test send lotus mail subject", "",
"james.wong@tais.com", "OK,test it", True
'============Public Sub SendNotesMail(Subject as string, attachment as string,
'recipient as string, bodytext as string,saveit as Boolean)
'This public sub will send a mail and attachment if neccessary to the
'recipient including the body text.
'Requires that notes client is installed on the system.
Function SendNotesMail(Subject As String, Attachment As String,
Recipient As String, BodyText As String, SaveIt As Boolean)
'Set up the objects required for Automation into lotus notes
Dim Maildb As Object 'The mail database
Dim UserName As String 'The current users notes name
Dim MailDbName As String 'THe current users notes mail database name
Dim MailDoc As Object 'The mail document itself
Dim AttachME As Object 'The attachment richtextfile object
Dim Session As Object 'The notes session
Dim EmbedObj As Object 'The embedded object (Attachment)
'Start a session to notes
Set Session = CreateObject("Notes.NotesSession")
'Next line only works with 5.x and above. Replace password with your password
'Session.Initialize ("password")
'Get the sessions username and then calculate the mail file name
'You may or may not need this as for MailDBname with some systems you
'can pass an empty string or using above password you can use other mailboxes.
UserName = Session.UserName
MailDbName = Left$(UserName, 1) & Right$(UserName,
(Len(UserName) - InStr(1, UserName, " "))) & ".nsf"
'Open the mail database in notes
Set Maildb = Session.GETDATABASE("", MailDbName)
If Maildb.ISOPEN = True Then
'Already open for mail
Else
Maildb.OPENMAIL
End If
'Set up the new mail document
Set MailDoc = Maildb.CREATEDOCUMENT
MailDoc.Form = "Memo"
MailDoc.sendto = Recipient
MailDoc.Subject = Subject
MailDoc.Body = BodyText
MailDoc.SAVEMESSAGEONSEND = SaveIt
'Set up the embedded object and attachment and attach it
If Attachment <> "" Then
Set AttachME = MailDoc.CREATERICHTEXTITEM("Attachment")
Set EmbedObj = AttachME.EMBEDOBJECT(1454, "", Attachment, "Attachment")
MailDoc.CREATERICHTEXTITEM ("Attachment")
End If
'Send the document
MailDoc.PostedDate = Now() 'Gets the mail to appear in the sent items folder
MailDoc.Send 0, Recipient
'Clean Up
Set Maildb = Nothing
Set MailDoc = Nothing
Set AttachME = Nothing
Set Session = Nothing
Set EmbedObj = Nothing
End Function