Browse for attachment to email (1 Viewer)

MooTube

Registered User.
Local time
Today, 12:01
Joined
Jul 2, 2015
Messages
31
Hello,

I have been able to send emails and send attachments if the path is static, but I am looking to create a browse function to attach items to the email. So far my code is...

for the selection of files:

Code:
Dim f As Office.FileDialog

    Set f = Application.FileDialog(msoFileDialogFilePicker)
    f.AllowMultiSelect = True
    
    If f.Show Then
        MsgBox f.SelectedItems.Count & " file(s) were chosen."
        Dim i As Integer
        For i = 1 To f.SelectedItems.Count
        MsgBox f.SelectedItems(i)
        Next i
    End If

and for the adding selected attachment to the email:

Code:
With oMail
    .To = Email
    .Subject = Subject
    .Body = Body
    For i = 1 To f.SelectedItems.Count
        .Attachments.Add = """f.SelectedItems(i)"""
    Next i
    .Display
End With

so far I can get the email to send without attachments and I can attach using ".Attachments.add" and then stating the name of the file to add, but I need to be able to add attachments that the user selects.

Any help would be greatly appreciated!
 

James Deckert

Continuing to Learn
Local time
Today, 14:01
Joined
Oct 6, 2005
Messages
189
looks like mine except I don't allow multiselect.
You could remove this and see if it works

also try

stop the code in the debugger at .attachments.add and look at the selected items to see if it looks right
 

MooTube

Registered User.
Local time
Today, 12:01
Joined
Jul 2, 2015
Messages
31
Thanks for the help, I am able to attach 1 file to the email, but I really need to be able to attach 4/5 at a time!

I am currently experiencing a bug where I end a "With", but do not start it even though it is started earlier...

Code:
With oMail
    .To = Email
    .Subject = Subject
    .Body = Body
    
    
    Set f = Application.FileDialog(msoFileDialogFilePicker)
    f.AllowMultiSelect = True
    
    If f.Show Then
        Dim i As Integer
        For i = 1 To f.SelectedItems.Count
        .Attachments.Add = f.SelectedItems(i)
        Next i
        
    .Display
End With

brings up "End With without With" error somehow.

If anyone has any idea how to do this I would be really appreciative!!!
 

Gasman

Enthusiastic Amateur
Local time
Today, 20:01
Joined
Sep 21, 2011
Messages
14,464
No End If?

I would walk through the code for the SelectedItems.count and check out the contents, is it what you think it should be ?
 

Users who are viewing this thread

Top Bottom