2016-08-11 45 views
2

我有這樣的代碼,如果目錄中的文件類型在Outlook中被阻止,它將不會包含在附件中。如果在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 

是否有任何其他方式首先檢查,如果陣列中的內容是在前景阻止文件類型的一個?

如果我檢查O20-29中的空白單元格,並在找到的第一個空白單元格中插入目錄或檢查數組,可以給我一個關於如何做的問題?順便說一句,這些是blocked file types in Outlook。謝謝!

回答

0

阻塞的類型可以在這裏找到:https://support.office.com/en-us/article/Blocked-attachments-in-Outlook-3811cddc-17c3-4279-a30c-060ba0207372

如果我這樣做,我可能會這樣存儲在一個單獨的工作表,我儲存的靜態數據進行驗證。例如,您可以將這些類型讀入字典並使用dictionary.exists方法來測試您正在循環的任何附件是否爲阻塞類型之一。

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

我個人不喜歡使用MsgBox在每個循環,因爲它打破了代碼。您可能希望將錯誤消息存儲在單獨的數組中,並使用join在子結束之前立即輸出所有錯誤消息。

+0

在你指的是腳本的哪個部分? MsgBox對我無關緊要,但謝謝。你能否就我的主要問題提出一些看法?謝謝! – ramj

3

這是我會怎麼做

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 
相關問題