简体   繁体   中英

How to open a folder to choose which files to attach to email using Excel VBA?

Having an issue figuring out how to prompt to choose the files I want to email.

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

You can try this out:

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

I hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM