I have a user form that mimics an email form (To, CC, attachment, body, subject etc) with the addition of a list box populated with a list of names. User selects a name and the "To:" list box gets populated with the appropriate email addresses. Next the FileDialogOpen pops up and user selects attachments (attachement box popluates with names of files selected) . Here's where it gets tricky for me.
After selecting attachments the user then can fill in the "Subject" and "Body" and click OK button that calls the SendEmail sub, however the actual attachments paths can't be passed because that sub (GetFiles) has already completed. How do I store the file paths to be recalled later for the SendEmail sub? Here's my snippets of what I have already.
Sub GetFiles() 'Multi File picker dialog box
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
Dim vSelectedItems As Variant
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = True
GetFile = Application.FileDialog(msoFileDialogOpen).Show
If GetFile <> 0 Then
For i = 1 To Application.FileDialog(msoFileDialogOpen).SelectedItems.Count
strPath = Application.FileDialog(msoFileDialogOpen).SelectedItems(i)
Next i
End If
With fd
For Each vSelectedItems In .SelectedItems
Items = vrtSelectedItems
Me.AttachBox.AddItem vSelectedItems
Next vSelectedItems
End With
and the SendEmail attached to the OK button click after user has verified information to be emailed is correct.
Private Sub OKButton_Click()
Dim OutApp As Object
Dim OutMail As Object
Dim sEmail As Variant
Dim myArray As Variant
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
myArray = ListBox1.List(ListBox1.ListIndex, 0) 'retrieving name selected and adding email addresses
Set found = Cells.Find(What:=myArray, After:=ActiveCell, LookIn:=xlFormulas, Lookat:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False)
Set sEmail = Range(found.Offset(0, 1), found.End(xlToRight))
sEmail.Copy
sRecipient = ""
For Each Item In sEmail
sRecipient = sRecipient & ";" & Item.Value
Next
On Error Resume Next
With OutMail
.To = sRecipient
.CC = CC.Value
.BCC = ""
'.FROM = ""
.Subject = Subject.Value
.Body = Body.Value
For Each vSelectedItems In GetFile 'no passing of file paths :(
.Attachments.Add Item
Next vSelectedItems
.Send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
Range("A1").Activate
Call CancelButton_Click
End Sub
Answer could be staring me in the face but I'm no expert nor new. Thanks in advance.