我有一个访问数据库,想要将查询结果保存为CSV格式并通过Outlook通过电子邮件发送。有办法吗?
我已经尝试过DoCmd.TransferText
命令,但找不到“ Specific”
Public Sub export_query()
DoCmd.TransferText acExportDelim, "Specific", "Query1", "C:\test\Query1.txt", True
End Sub
答案 0 :(得分:1)
如果要使用规范,则需要首先通过向导手动导出为文本。您可能没有保存规范,或者使用了错误的名称。
External Data
标签。Export to text
的选项。
(或者,您可以在导航窗格中右键单击查询,然后导出为文本)OK
Advanced
。Save as
保存您的规范并为其命名。确保在DoCmd.TransferText
的规范字段中使用此名称。另外,将输出文件名参数的扩展名更改为.csv
,而不是.txt
。
您可以使用以下功能导出和通过电子邮件发送.csv
:
Sub export_query()
Dim objOutlook As Outlook.Application
Dim objMail As Outlook.MailItem
Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(olMailItem)
' Export to CSV
DoCmd.TransferText acExportDelim, "Specific", "Query1", "C:\test\Query1.csv", True
' You could also try it without any specifications. (Untested)
' DoCmd.TransferText acExportDelim, "", "Query1", "C:\test\Query1.csv", True
' Send the email
With objMail
.To = "John@Doe.com"
.Subject = "This is the subject."
.Body = "This is the body."
.Attachments.Add ("C:\test\Query1.csv")
.Display '(If you want to see the email and send it yourself)
'.Send '(If you want to send the email without seeing it)
End With
' Clean up the objects
Set objMail = Nothing
Set objOutlook = Nothing
End Sub