ms-access - 从 Access DB 发送包含动态名称附件的电子邮件

标签 ms-access vba

我不知道如何让这个东西继续工作。 下面的代码发送一封包含 MS Access 2010 附件的电子邮件。

问题是,如果它需要固定的文件名,我的文件名会随着我使用每个文件末尾的日期而改变。示例:green_12_04_2012.csv。我也不知道如果文件夹为空或目录发生更改,如何使其不会失败。跳到下一个子程序而不是崩溃就太好了。

我的代码:

Dim strGetFilePath As String
Dim strGetFileName As String

strGetFilePath = "C:\datafiles\myfolder\*.csv"

strGetFileName = Dir(strGetFilePath)

Dim appOutLook As Outlook.Application
Dim MailOutLook As Outlook.MailItem
Set appOutLook = CreateObject("Outlook.Application")
Set MailOutLook = appOutLook.CreateItem(olMailItem)
With MailOutLook
    .BodyFormat = olFormatRichText
    .To = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="32505d507250475b5e5657401c515d5f" rel="noreferrer noopener nofollow">[email protected]</a>"
    ''.cc = ""
    ''.bcc = ""
    .Subject = "text here"
    .HTMLBody = "text here"
    .Attachments.Add (strGetFileName & "*.csv")
    .Send
End With
End Sub

我想我已经到达那里了。

最佳答案

我找到了合适的解决方案,除了发布的解决方案之外,我还想添加此解决方案,以防有人正在寻找解决方案。我一直熬到凌晨 3 点,这是一个非常受欢迎的问题,但对于循环附加特定文件夹中的所有文件还没有任何解决方案。

这是代码:

Public Sub sendEmail()
    Dim appOutLook As Outlook.Application
    Dim MailOutLook As Outlook.MailItem
    Dim strPath As String
    Dim strFilter As String
    Dim strFile As String

    strPath = "C:\Users\User\Desktop\"      'Edit to your path
    strFilter = "*.csv"
    strFile = Dir(strPath & strFilter)

    If strFile <> "" Then

        Set appOutLook = CreateObject("Outlook.Application")
        Set MailOutLook = appOutLook.CreateItem(olMailItem)

        With MailOutLook
            .BodyFormat = olFormatRichText
            .To = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="97f5f8f5d7f5e2fefbf3f2e5b9f4f8fa" rel="noreferrer noopener nofollow">[email protected]</a>"
            ''.cc = ""
            ''.bcc = ""
            .Subject = "text here"
            .HTMLBody = "text here"
            .Attachments.Add (strPath & strFile)
            .Send
            '.Display    'Used during testing without sending (Comment out .Send if using this line)
        End With
    Else
        MsgBox "No file matching " & strPath & strFilter & " found." & vbCrLf & _
                "Processing terminated.
        Exit Sub    'This line only required if more code past End If
    End If

End Sub

关于ms-access - 从 Access DB 发送包含动态名称附件的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13713327/

相关文章:

ms-access - 为什么VBA CurrentProject.Connection语句导致我的所有 Access 数据库崩溃?

sql-server - 使用SSIS迁移和规范化数据库

vba - 从VBA向Powershell提供参数

java - 登录系统并有权 Access

excel - 替换字符串中奇怪或特殊的字符

vba - 阻止 Excel 在 VBA 制作的公式中将逗号更改为分号

excel - Worksheet_Change 事件给出运行时错误 13 - 插入行时类型不匹配

vba - 如何将所有列中的内容从 Excel 中获取到文本文件中?

java - 更新我的表时出错

string - vb 宏字符串宽度(以像素为单位)