从Access数据库发送的电子邮件包含的附件​​与动态名称 [英] Sending Emails from Access DB containing attachment with dynamic name

查看:493
本文介绍了从Access数据库发送的电子邮件包含的附件​​与动态名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何得到这个东西的工作超出了这一点。 我的code以下发送包含附件出MS Access 2010中的邮件

I do not know how to get this thing to work beyond this point. My code below sends an email containing an attachment out of MS Access 2010.

现在的问题是,如果它需要一个固定的文件名,因为我使用的日期在每个文件的末尾我的文件名称更改。例如:green_12_04_2012.csv。我也不知道如何使这个没有失败,如果该文件夹为空或目录更改。这将是伟大的它只是跳到下一子,而不是崩溃。

The problem is if it requires a fixed file name, my file name changes as I am using the date at the end of each file. example: green_12_04_2012.csv. I also do not know how to make this not fail if the folder is empty or the directory changes. It would be great for it to just skip to the next sub rather than crashing.

我的code:

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 = "bob@builder.com"
    ''.cc = ""
    ''.bcc = ""
    .Subject = "text here"
    .HTMLBody = "text here"
    .Attachments.Add (strGetFileName & "*.csv")
    .Send
End With
End Sub

我想我到那里。

I think I am getting there.

推荐答案

我找到了一个合适的分辨率,除了张贴的解决方案,我想补充这一点,万一有人正在寻找解决方案。我直到凌晨3点,这是一个非常受欢迎的问题,而是有没有的问候循环的任何决议的附着在一个特定的文件夹中的所有文件。

I found a suitable resolution and in addition to the solution posted, I wanted to add this in-case anyone is searching for the solution. I was up until 3am, this is a very popular question but there was not any resolution in regards to looping an attaching all files in a specific folder.

下面是code:

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 = "bob@builder.com"
            ''.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

这篇关于从Access数据库发送的电子邮件包含的附件​​与动态名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆