Thomas Goldsbury
Thomas Goldsbury

Reputation: 33

VBA SYNTAX to EXCLUDE .XLS file types being attached to bulk email send

Have a quick question in relation to the attached. section -I have some VBA that will ask a user if they want to attach any additional documents (Atach = MsgBox("Do you wish to attach any additional Documents", vbYesNo) to a looped email task - however, I need to find a smarter way to alert the user that .xls file types cannot be attached and not allow them to attach that file type only.

Have tried if syntax but I think I am missing something. I am rather new to VBA so appreciate all your feedback.

Application.ScreenUpdating = False

Dim OutApp As Object
Dim OutMail As Object
Dim OutWordEditor As Object

Dim Tocell As Range
Dim SigString As String
Dim t               '' Overall timer after selecting files
Dim i As Integer    '' ASLS looping
Dim j As String    '' extra attachment
Dim x As Single     '' Delay timer
Dim valid As Integer

Dim titlestring As String
Dim FileW As Boolean
Dim Attachrng As String
Dim attach As String

Dim Worddoc As Object
Dim WordFile As String
Dim ToRangeCounter As Variant

Dim attachrange() As String

i = 0
j = 0
valid = 0
FileW = False
WordFile = ""

Atach = MsgBox("You have selected the Notifications Auto Email Macro",    vbYesNo)

If Atach = vbNo Then
            MsgBox ("Macro will now close")
            Exit Sub
            End If
WordFile = ""
FileW = False

Do Until FileW = True
WordFile = Application.GetOpenFilename(Title:="Select Notificatin to send as the email body", MultiSelect:=False)

Call Preview(WordFile)

Usercheck = MsgBox("Confirm this is correct ", vbYesNo)

If Usercheck = vbYes Then
FileW = True
End If
Loop
Else
MsgBox ("Please re select the file you wish to attach")
End If
Loop



***

Atach = MsgBox("Do you wish to attach any additional Documents", vbYesNo)
If Atach = vbYes Then

    For k = 0 To 1
    j = InputBox("How many files do you wish to attach?" & vbNewLine & "Only enter a number", "Enter a number here", 1)
    If Not IsNumeric(j) Then
        MsgBox ("You have failed to enter a number, please try again")
        k = 0
        valid = valid + 1
        If valid = 3 Then
            MsgBox ("You have failed to input a number 3 times" & vbNewLine & "Macro will now close")
            Exit Sub
            End If

        Else
            k = k + 1
            ReDim Preserve attachrange(j)
        End If
    Next

For Z = 1 To j
   titlestring = "You are attaching file number " & Z & " of " & j
   attachrange(Z) = Application.GetOpenFilename(Title:=titlestring, MultiSelect:=False)
   Next

  End If


On Error GoTo Cleanup

Upvotes: 1

Views: 78

Answers (1)

Variatus
Variatus

Reputation: 14373

If you were to use Word's FileDialog you could do two things. One, you could filter out xlx files. They wouldn't show up for selection. Two, you could allow multiple selections, doing away with your question of how many the user wants to add. You might adapt the following code to your needs.

Function FilePicker(ByVal Tit As String, _
                    ByVal Btn As String, _
                    ByVal FltDesc As String, _
                    ByVal Flt As String, _
                    Fn As String, _
                    Optional Multi As Boolean = False) As Boolean
    ' 22 Mar 2017

    ' ==================================================
    '   Parameters:
    '       Tit               = Title
    '       Btn               = Button caption
    '       FltDesc           = Plain language filter
    '       Flt               = Filter string
    '       Fn                = Initial file name
    '                           Returns selected full file name
    ' ==================================================

    Dim FoD As FileDialog                           ' FilePicker Dialog

    ' ==================================================

    Set FoD = Application.FileDialog(msoFileDialogFilePicker)
    With FoD
        .Title = Tit
        .ButtonName = Btn
        .Filters.Clear
        .Filters.Add FltDesc, Flt, 1
        .AllowMultiSelect = Multi
        .InitialFileName = Fn
        If .Show Then
            Fn = .SelectedItems(1)
            FilePicker = True
        End If
    End With

    Set FoD = Nothing
End Function

Then, you might work on the files the user has attached (perhaps using some other method or by modifying the FileDialog) and check the FileFormat property of the attached files. Remove any you don't which to approve. Look for xlFileFormat constants to identify specific file formats.

Upvotes: 2

Related Questions