|
原题:
问题十: 一个生产企业客户有很多经销商,每个月都有必须把对账单发给他经销商,他们使用Access作了一套程序,操作员每个月都要把每一个经销商的对账单手工复制—粘贴到邮件中,在发送出去。能不能编写一个程序,自动把每一个客户的对账单在Access中导出后调用MS Outlook发送出去。重要提示:您可以把数据导出为Word, Excel或者Text,也可以是直接把数据嵌到邮件内容中。[可在下载数据库的基础上完成]
分析:
此题涉及到access(准备和组织数据)和outlook(分发数据)。
access中有customers(客户基本信息)和order(定单)两张表。
首先需要你将这两张表做一个关联。因为customers里没有客户的购货信息但有基本信息,而定单里只有购货信息但没有客户的基本信息,所以必须将两张表做一个关联(通过customers id)。我的解决办法是做一个view。这样数据就准备完毕了。
下面是程序代码:
Sub Send_Order_Mail()
Dim cnn As ADODB.Connection
Dim rst_cusid, rst_order_list As ADODB.Recordset
Dim olkapp As Outlook.Application
Dim newmail As MailItem
Set olkapp = CreateObject("outlook.application")
Set cnn = New ADODB.Connection
cnn.Open CurrentProject.Connection
Set rst_cusid = New ADODB.Recordset
rst_cusid.Open "select distinct CustomerID,CompanyName,Email from v_order_list", cnn, adOpenKeyset, adLockReadOnly
If rst_cusid.RecordCount < 1 Then Exit Sub
Set rst_order_list = New ADODB.Recordset
For i = 1 To rst_cusid.RecordCount
rst_order_list.Open "select * from v_order_list where CustomerID = " + "'" + rst_cusid.Fields(0) + "'", cnn, adOpenKeyset, adLockReadOnly
With rst_order_list
para = "Dear " + .Fields(1) + ":" + Chr(10)
para = para + Space(3) + "Your Company " + .Fields(0) + " has Order those Good:" + Chr(10)
For j = 1 To .RecordCount
para = para + Space(3) + "Good Name :" + .Fields(2) + " Order Date :" + CStr(.Fields(3)) + " Price:" + CStr(.Fields(4)) + Chr(10)
Next
End With
rst_order_list.Close
para = para + Space(30) + "Yours Loadhigh" 'para为信件内容
Set newmail = olkapp.CreateItem(olMailItem)
With newmail
.To = rst_cusid.Fields(2) '接收邮件的信箱
.Subject = rst_cusid.Fields(1) + " Order List" '信件标题
.Body = para
.Send '发送
End With
rst_cusid.MoveNext
para = ""
Next
rst_cusid.Close
Set rst_cusid = Nothing
Set rst_order_list = Nothing
cnn.Close
Set cnn = Nothing
End Sub |
|