【问题标题】:How to email query result in csv format in Access如何在 Access 中以 csv 格式通过电子邮件发送查询结果
【发布时间】:2020-01-12 06:41:11
【问题描述】:

我有一个访问数据库,想以 CSV 格式保存查询结果并通过 Outlook 发送电子邮件。有办法吗?

我尝试了DoCmd.TransferText 命令,但找不到具体的

Public Sub export_query()
    DoCmd.TransferText acExportDelim, "Specific", "Query1", "C:\test\Query1.txt", True
End Sub

【问题讨论】:

    标签: vba email ms-access export-to-csv


    【解决方案1】:

    如果要使用规范,首先需要通过向导手动导出为文本。您可能没有保存您的规范,或者您使用了错误的名称。

    1. 打开您的查询。
    2. 在功能区上,选择External Data 选项卡。
    3. 选择Export to text 选项。 (或者,您可以在导航窗格中右键单击查询并导出为文本)
    4. 当向导出现时,不要勾选任何提供给您的选项,然后单击OK
    5. 在助手界面,点击Advanced
    6. 根据自己的喜好设置规范。
    7. 单击Save as 保存您的规范并为其命名。

    确保在 DoCmd.TransferText 的规范字段中使用此名称。此外,将输出文件名参数的扩展名更改为 .csv 而不是 .txt

    您可以使用以下功能导出您的.csv 并通过电子邮件发送您的.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
    

    【讨论】:

      猜你喜欢
      • 2013-10-29
      • 2021-08-29
      • 1970-01-01
      • 1970-01-01
      • 2013-10-28
      • 2016-04-12
      • 1970-01-01
      • 2020-11-08
      • 2018-12-21
      相关资源
      最近更新 更多