检查数组是否包含Outlook中的阻止文件types

我有这样的代码,如果目录中的文件types在Outlook中被阻止,它将不会包含在附件中。

Dim objMail as object dim i,count as integer With objMail .Subject = "sample" For i = 20 To lRow ' directories starts in row 20 in column O On Error GoTo pst attach.add main.Range("O" & i).Value pst: If count = 0 Then MsgBox "Some files is not allowed." count = 1 'count 1 so that this error will not be displayed again and again End If Next i end with 

这已经工作,但我的问题是,如果用户添加另一个文件types,并且空白单元格之间的非空白单元格,它不会被填充。

我有这个代码添加列O中的目录并填充它。

 dim file as variant file = Application.GetOpenFilename("All Files, *.*", , "Select File", , True) For i = 1 To UBound(file) lRow = Cells(Rows.count, 15).End(xlUp).Row lRow = lRow + 1 ThisWorkbook.Sheets("Main").Range("O" & lRow).Value = CStr(file(i)) Next i 

有没有其他的方法来检查,如果数组中的内容是在Outlook中阻止的文件types之一?

你可以给我一个关于怎么做,如果我检查从O20-29空白单元格,并插入目录中find的第一个空白单元格或检查数组? 顺便说一句,这些是在Outlook中被阻止的文件types。谢谢!

这是我将如何做到这一点

 Sub Sample() Dim sFileType As String, Extn As String Dim MyAr As Variant, file As Variant '~~> List of blocked types sFileType = "ade|adp|app|asp|bas|bat|cer|chm|cmd|com|cpl|crt|csh|der|" sFileType = sFileType & "exe|fxp|gadget|hlp|hta|inf|ins|isp|its|js|jse|" sFileType = sFileType & "ksh|lnk|mad|maf|mag|mam|maq|mar|mas|mat|mau|mav|" sFileType = sFileType & "maw|mda|mdb|mde|mdt|mdw|mdz|msc|msh|msh1|msh2|" sFileType = sFileType & "mshxml|msh1xml|msh2xml|ade|adp|app|asp|bas|bat|cer|" sFileType = sFileType & "chm|cmd|com|cpl|crt|csh|der|exe|fxp|gadget|hlp|" sFileType = sFileType & "hta|msi|msp|mst|ops|pcd|pif|plg|prf|prg|pst|reg|" sFileType = sFileType & "scf|scr|sct|shb|shs|ps1|ps1xml|ps2|ps2xml|psc1|" sFileType = sFileType & "psc2|tmp|url|vb|vbe|vbs|vsmacros|vsw|ws|wsc|wsf|wsh|xnk" '~~> Create an array of blocked types MyAr = Split(sFileType, "|") file = Application.GetOpenFilename("All Files, *.*", , "Select File", , True) If file = False Then Exit Sub For i = 1 To UBound(file) '~~> Get file extension Extn = Right$(file(i), Len(file(i)) - InStrRev(file(i), ".")) '~~> Check if Extn is a blocked type If IsInArray(Extn, MyAr) Then Debug.Print file(i) & " is of blocked type" '~~> Do what you want Else '~~> Do what you want End If Next i End Sub Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean On Error Resume Next IsInArray = Application.Match(stringToBeFound, arr, 0) On Error GoTo 0 End Function 

阻止的types可以在这里find: https : //support.office.com/en-us/article/Blocked-attachments-in-Outlook-3811cddc-17c3-4279-a30c-060ba0207372

如果我这样做,我可能会将其存储在一个单独的工作表中,我存储了用于validation的静态数据。 例如,您可以将这些types读入字典并使用dictionary.exists方法来testing您正在循环的附件是否为阻塞types之一。

 ' to create a dictonary dim objDic as object: set objDic = createobject("scripting.dictionary") 

我个人不喜欢在每个循环中使用MsgBox ,因为它打破了代码。 您可能希望将错误消息存储在单独的数组中,并在子结束之前立即使用join来输出所有错误消息。