VBA语法排除附加到批量电子邮件发送的.XLS文件types

关于附件有一个快速的问题。 部分 – 我有一些VBA会问用户他们是否想附加任何额外的文件(Atach = MsgBox(“你想附加任何额外的文件”,vbYesNo)到一个循环的电子邮件任务 – 但是,我需要find一个更智能的方式来提醒用户.xls文件types不能附加,并且不允许它们仅附加该文件types。

尝试过,如果语法,但我想我失去了一些东西。 我对VBA相当陌生,因此非常感谢您的反馈。

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 

如果你使用Word的FileDialog,你可以做两件事。 一,你可以过滤掉xlx文件。 他们不会出现select。 二,你可以允许多个select,取消你想要添加多less用户的问题。 您可能会根据您的需要调整以下代码。

 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 

然后,您可能会处理用户所附的文件(也许使用其他方法或修改FileDialog)并检查附加文件的FileFormat属性 。 删除任何你不认可的。 寻找xlFileFormat常量来识别特定的文件格式。