Emailing in Outlook Multiple Attachments from a ListBox in a Userform

USFengBULLS

Board Regular
Joined
May 7, 2018
Messages
66
Office Version
  1. 365
Platform
  1. Windows
Hello, I am working on a code to send multiple files in a listbox on a userform as attachments in outlook. I have this linked to a commandButton click event but I need further guidance on it.
there is a command button that first populates the list box with the multiple files the user selects. The problem I have it with the Email Command button Click, I think I am close.
Really I just want it to open Outlook and have the files that are in the listbox be as attachments and then the user will fill the rest from there.

Right now it is giving me a Runtime error 424, Object Required and highlights the "Attachments.Add (lbxEmail.Selected(n))" Line in the code.
Here is the code so far:


VBA Code:
Private Sub cmdEmail_Click()

Dim oOutlook As Outlook.Application
Dim oEmailItem As MailItem

If oOutlook Is Nothing Then
    Set oOutlook = New Outlook.Application
End If
Set oEmailItem = oOutlook.CreateItem(olMailItem)
With oEmailItem
    .BodyFormat = olFormatHTML
    .To = vbNullString
    .CC = vbNullString
    .Subject = vbNullString
    
    For n = 0 To lbxEmail.ListCount - 1
        Attachments.Add (lbxEmail.Selected(n))
    Next n
    .Display
End With

End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Try this
Note: In the listbox the items must have the name of the folder, the name of the file and the extension.

VBA Code:
Private Sub cmdEmail_Click()
  Dim oOutlook As Outlook.Application
  Dim oEmailItem As MailItem
  Dim n As Long
  
  If oOutlook Is Nothing Then
      Set oOutlook = New Outlook.Application
  End If
  Set oEmailItem = oOutlook.CreateItem(olMailItem)
  With oEmailItem
    .BodyFormat = olFormatHTML
    .To = vbNullString
    .CC = vbNullString
    .Subject = vbNullString
    For n = 0 To lbxEmail.ListCount - 1
      .Attachments.Add lbxEmail.List(n)
    Next n
    .Display
  End With
End Sub
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,780
Messages
6,126,857
Members
449,345
Latest member
CharlieDP

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top