Select All Files by File Type (1 Viewer)

FrostByte

Registered User.
Local time
Today, 08:27
Joined
Jan 15, 2015
Messages
56
This code works perfectly by opening a window and allowing you to select which file to attach to an email, but I'm looking to modify it so it automatically selects file types of my choosing (or even them all using *.* for all). Then to attach without having to press "Open" on the browse window.


'On Error GoTo SNDCLM_Click_Err
'Open Directory for current record report and allow user to select files to send within the email to teh address Specified
'Operates from Send Claim Button on Claims entry form

Dim oLook As Object
Dim oMail As Object
Dim FD As Object
Dim vrtSelectedItem As Variant
Dim strname As String

'strname = Application.CurrentProject.Path & "\" & Me.VHID.Column(1) & "\" & "Fault" & "\" & Me.CLID & "\" & Me.CLID & ".pdf"
'strname = "C:\Temp2\Test\*.pdf"
strname = "C:\Temp2\Test\"

Set oLook = CreateObject("Outlook.Application")
Set oMail = oLook.createitem(0)
Set FD = Application.FileDialog(3)

With oMail
.to = "enter@email.com"
.body = "Please see attached Claim Submission & Images."
.Subject = "XXXXXXXXXX"




FD.AllowMultiSelect = True
FD.Filters.Clear
FD.Filters.Add "All Files", "*.*"
FD.InitialFileName = strname



If FD.Show = True Then
For Each vrtSelectedItem In FD.SelectedItems
.Attachments.Add vrtSelectedItem
Next
End If

.Display

End With

Set FD = Nothing
Set oMail = Nothing
Set oLook = Nothing

'SNDCLM_Click_Exit:
Exit Sub
 

theDBguy

I’m here to help
Staff member
Local time
Today, 00:27
Joined
Oct 29, 2018
Messages
21,581
If you don't want the file browser window to open, don't use the FileDialog object, just use Dir() in a loop.
 

FrostByte

Registered User.
Local time
Today, 08:27
Joined
Jan 15, 2015
Messages
56
If you don't want the file browser window to open, don't use the FileDialog object, just use Dir() in a loop.
Hi DB,

Loving the file explorer/viewer demo you have.

Could you assist how I might replace the code using Dir() in a loop?

Thank you in advance
 

moke123

AWF VIP
Local time
Today, 03:27
Joined
Jan 11, 2013
Messages
3,952
Another option is to use FilesystemObject.
 

MarkK

bit cruncher
Local time
Today, 00:27
Joined
Mar 17, 2004
Messages
8,190
Code:
Sub ListFiles(Path As String, Optional Pattern As String = "*.*")
    Dim Name As String
    
    If Right(Path, 1) <> "\" Then Path = Path & "\"
    
    Name = Dir(Path & Pattern)
    Do While Name <> ""
        Debug.Print Name
        Name = Dir
    Loop
End Sub
Path is the folder spec. Pattern is something like "*.pdf" to match the files you want to return.

I think Dir() is a little bit faster than FSO.
 

Users who are viewing this thread

Top Bottom