如何使用Excel VBA打开文件夹以选择要附加到电子邮件的文件?

时间:2018-11-20 18:12:07

标签: excel vba outlook-vba

在确定如何提示选择我要通过电子邮件发送的文件时遇到问题。

Dim olApp As Outlook.Application
Dim diaFolder As FileDialog
Set diaFolder = Application.FileDialog(msoFileDialogFolderPicker)
Set olApp = CreateObject("Outlook.Application")
Dim olMail As Outlook.MailItem
Set olMail = olApp.CreateItem(olMailItem)

olMail.To = "email@email.com"
olMail.Subject = "Subject Line"
olMail.Body = "Body of the Email"

diaFolder.AllowMultiSelect = True
diaFolder.Show

MsgBox diaFolder.SelectedItems

Set diaFolder = Nothing

olMail.Send

1 个答案:

答案 0 :(得分:1)

您可以尝试一下:

Sub sendAttachment()

    Dim olApp As Outlook.Application
    Dim diaFolder As FileDialog
    Set diaFolder = Application.FileDialog(msoFileDialogFolderPicker)
    Set olApp = CreateObject("Outlook.Application")
    Dim olMail As Outlook.MailItem
    Set olMail = olApp.CreateItem(olMailItem)
    Dim lngCount As Long

    olMail.To = "email@email.com"
    olMail.Subject = "Subject Line"
    olMail.Body = "Body of the Email"


    ' Open the file dialog
    With Application.FileDialog(msoFileDialogOpen)
        .AllowMultiSelect = True
        .Show

        ' Display paths of each file selected
        For lngCount = 1 To .SelectedItems.Count
            attFilePath = Application.FileDialog(msoFileDialogOpen).SelectedItems(lngCount)
            olMail.Attachments.Add (attFilePath)
        Next lngCount

    End With

    olMail.Display
    'olMail.Send

End Sub

我希望这会对您有所帮助。