Macro to Allow User to Select Multiple PDF Files

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,595
Office Version
  1. 2021
Platform
  1. Windows
I have written code to allow a user to attach multilple PDF Files


I selected two diifferent Files in "C:\My documents"", but the same file was attached twice


It would be appreciated if someone could kindly amend theportion of my code to enable a user to attach multiple files

The rest of the code works 100%


It appears that it is only this portion of my code that needs to be amended to accomodate my request

Code:
 Dim fd As Office.FileDialog: Set fd = Application.FileDialog(msoFileDialogFilePicker)
Dim strFile As String

With fd
    .Filters.Clear
    .Filters.Add "PDF files", "*.pdf", 1
    .Title = "Choose the PDF file to attach"
    .AllowMultiSelect = True
.InitialFileName = "C:\my documents" 'Verify this is the correct path; will error if not
    If .Show = True Then
        strFile = .SelectedItems(2)
    End If
End With


See full code below

Code:
 Sub Email_andattachfiles()
 
Dim File As String, strBody As String, LR As Long

Dim TheFile As String: TheFile = Sheets("data").Range("A42")

With Application
          .ScreenUpdating = False
          .DisplayAlerts = False
End With

File = Environ$("temp") & "\" & Sheets("Data").Range("A42") & ".xlsx"
strBody = "Hi " & Sheets("Data").Range("AT1") & vbNewLine & vbNewLine & _
        "Attached, please find Journal Entries to be printed" & vbNewLine & vbNewLine & _
            "Regards" & vbNewLine & vbNewLine & _
            "Howard"

    Set rng = Nothing
  
   
   Sheets("Data").Range("Journals").Copy
   
Workbooks.Add
ActiveSheet.Range("a1").PasteSpecial xlPasteValues
ActiveSheet.Range("a1").PasteSpecial xlPasteFormats
ActiveSheet.Range("a1").PasteSpecial xlPasteColumnWidths

Sheets(1).Name = "Data"
  LR = Sheets(1).Cells(Rows.Count, "A").End(xlUp).Row
With ActiveWorkbook
With ActiveSheet.PageSetup
.PrintGridlines = True
.PrintArea = "A2:E" & LR + 10
.PrintTitleRows = "$1:$1"
.PrintTitleColumns = ""
.LeftHeader = "&D&T"
.CenterHeader = "Branch Interest Calcs"
.Orientation = xlLandscape
.FitToPagesWide = 1
End With

Sheets(1).Select
    ActiveWindow.View = xlPageBreakPreview

   .SaveAs Filename:=File, FileFormat:=51
   .Close SaveChanges:=False
End With

Dim fd As Office.FileDialog: Set fd = Application.FileDialog(msoFileDialogFilePicker)
Dim strFile As String

With fd
    .Filters.Clear
    .Filters.Add "PDF files", "*.pdf", 1
    .Title = "Choose the PDF file to attach"
    .AllowMultiSelect = True
.InitialFileName = "C:\my documents" 'Verify this is the correct path; will error if not
    If .Show = True Then
        strFile = .SelectedItems(2)
    End If
End With

DoEvents

With CreateObject("Outlook.Application").CreateItem(0)
     ActiveWindow.View = xlPageBreakPreview
       
     .To = Join(Application.Transpose(Sheets("Data").Range("AU1:AU2").Value), ";")
       
     .Subject = Sheets("Data").Range("a42")
     .body = strBody
 '    .attachments.Add File 'Adds the .xlsx file declared above
     .attachments.Add strFile 'Adds the PDF selected with the FilePicker
          .attachments.Add strFile 'Adds the PDF selected with the FilePicker
     DoEvents
     .Display
End With
Kill File
ActiveWindow.View = xlNormalView
With Application
          .ScreenUpdating = True
          .DisplayAlerts = True
End With
End Sub

Your assistance in rresolving this is most appreciated
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
For your file picker...

VBA Code:
    With fd
        .Filters.Clear
        .Filters.Add "PDF files", "*.pdf", 1
        .Title = "Choose the PDF file to attach"
        .AllowMultiSelect = True
        .InitialFileName = "C:\my documents" 'Verify this is the correct path; will error if not
        If .Show = 0 Then Exit Sub
        ReDim arrResults(1 To .SelectedItems.Count)
        Dim i As Long
        For i = 1 To .SelectedItems.Count
            arrResults(i) = .SelectedItems(i)
        Next i
    End With

To attach the PDF files to your email...

VBA Code:
    For i = LBound(arrResults) To UBound(arrResults)
        .attachments.Add arrResults(i)
    Next i

Hope this helps!
 
Upvote 0
Solution
Hi Domenic


Many thanks for your help. Your code works 100%
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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