我不知道如何让这个东西继续工作。 下面的代码发送一封包含 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/