我有这样的代码,如果目录中的文件类型在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
这已经可以了,但我的问题是如果用户添加了另一种文件类型并且空白单元格位于非空白单元格之间,则不会填充它。
我有这个代码,它在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中被阻止的文件类型之一,还有其他方法可以先检查吗?
如果我从O20-29
检查空白单元格并在找到的第一个空白单元格中插入目录或检查数组,你能告诉我该怎么做吗?顺便说一句,这些是blocked file types in Outlook。谢谢!
答案 0 :(得分:2)
我就是这样做的
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
答案 1 :(得分:0)
可以在此处找到阻止的类型:documentation for String.Remove
如果我这样做,我可能会将其存储在一个单独的工作表中,我在其中存储静态数据以进行验证。例如,您可以将这些类型读入字典并使用dictionary.exists方法来测试您循环的任何附件是否是阻止类型之一。
void webBrowser_LoadingStateChanged(object sender, CefSharp.LoadingStateChangedEventArgs e)
我个人不喜欢在每个循环中使用' to create a dictonary
dim objDic as object: set objDic = createobject("scripting.dictionary")
,因为它会破坏代码。您可能希望将错误消息存储在单独的数组中,并使用join在子结束之前立即输出所有错误消息。