想要发送某些详细信息,同时附加某个目录中的图像

时间:2018-03-26 02:33:20

标签: excel vba excel-vba

我正在创建一个工作簿,我想创建一个电子邮件按钮。这不仅可以邮寄工作簿的内容,还可以将某个目录附加到作为附件的电子邮件中。

该目录由具有以下编码的批处理文件自动创建。

mkdir %date:~-4,4%"-"%date:~-10,2%"-"%date:~-7,2%


此“创建文件夹”,其名称为当前日期。 This is How the Folder Structure Looks like when it is created.

我研究了很多并创建了一个代码,通过点击一个按钮直接发送我的EXCEL文件的上下文。但是也无法使代码适用于附件。

以下代码用于发送我的邮件。

Sub EmailRange()
Dim WorkRng As Range
On Error Resume Next
xTitleId = "Excalibur Mail"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
Application.ScreenUpdating = False
WorkRng.Select
ActiveWorkbook.EnvelopeVisible = True
With ActiveSheet.MailEnvelope
    .Introduction = "This is an automated Email. Please do not respond"
    .Item.To = "" 'Senders Email ID
    .Item.Subject = "Daily Counts"
    .Item.Send
End With
Application.ScreenUpdating = True
End Sub

“准则”工作正常并向我询问范围选择,并向该特定客户发送电子邮件。我现在需要的是能够附加显示当前日期的文件夹中的所有图像。

对于Eg。如果今天的日期是26/03/2018。它将创建一个名为2018-03-26的文件夹。我需要一些帮助来发送我的内容以及当前日期文件夹中的所有图像。

1 个答案:

答案 0 :(得分:0)

我创建了一个“aaa.txt”& “bbb.txt”在新文件夹中进行测试。您可以查看此链接,了解如何将文件中的所有项目添加为附件:https://www.experts-exchange.com/questions/27319804/Excel-VBA-Attach-All-Files-in-a-Directory.html

Option Explicit

Sub test()

Dim tdy As String
tdy = Format(Date, "yyyy-mm-dd")

Dim filePath As String
filePath = "YouPath\" & tdy & "\"

Dim strFileName As String

strFileName = Dir(filePath & "*.*")

ActiveWorkbook.EnvelopeVisible = True
With ActiveSheet.MailEnvelope
    .Introduction = "This is an automated Email. Please do not respond"
    .Item.To = "" 'Any mail Id You need
    .Item.Subject = "Daily Counts"
    .Item.Display
    If FolderExists(filePath) Then
        Do While Len(strFileName) > 0
            .Item.attachments.Add filePath & strFileName
            strFileName = Dir
        Loop
        Else
            MsgBox "Folder " & tdy & " not Found!"
    End If
End With

End Sub

Function FolderExists(ByVal path As String) As Boolean

    FolderExists = False
    Dim objFSO As Object

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    If objFSO.FolderExists(path) Then FolderExists = True
End Function