2017-10-11 65 views
0

如何自動下載作爲Outlook項目的附件?下載作爲Outlook項目的Outlook附件

imag here

我嘗試使用這個腳本VBA下載,但它並不適用於Outlook項目工作。它適用於.txt或任何其他類型的附件。

Public Sub Savisk(MItem As Outlook.MailItem) 
Dim oAttachment As Outlook.Attachment 
Dim sSaveFolder As String 
sSaveFolder = "D:\userdata\sanakkay\Desktop\" 
For Each oAttachment In MItem.Attachments 
oAttachment.SaveAsFile sSaveFolder & oAttachment.DisplayName 
Next 
End Sub 
+0

這看起來不像'vb.net' ...以及它的一些與'vb6'混合。你能描述一下「它不適用於展望項目」嗎? – Codexer

+0

sry ..........其不是vb.net的vba代碼。此代碼適用於.txt或.excel或其他格式的附件,但不適用於文件類型展望。查看圖像以瞭解Outlook格式的外觀 –

回答

1

Outlook項目可能被命名爲/具有文件名中非法字符的主題。

例如在

任務名稱冒號:KM_CEM_GY

有解決此至少兩個標準方法。

Outlook 2010 VBA How to save message including attachment

Private Sub ReplaceCharsForFileName(sName As String, sChr As String) 
    sName = Replace(sName, "'", sChr) 
    sName = Replace(sName, "*", sChr) 
    sName = Replace(sName, "/", sChr) 
    sName = Replace(sName, "\", sChr) 
    sName = Replace(sName, ":", sChr) 
    sName = Replace(sName, "?", sChr) 
    sName = Replace(sName, Chr(34), sChr) 
    sName = Replace(sName, "<", sChr) 
    sName = Replace(sName, ">", sChr) 
    sName = Replace(sName, "|", sChr) 
End Sub 

VBA dialog boxes automatically answer solution

Function StripIllegalChar(StrInput) 
    Dim RegX   As Object 

    Set RegX = CreateObject("vbscript.regexp") 

    RegX.Pattern = "[\" & Chr(34) & "\!\@\#\$\%\^\&\*\(\)\=\+\|\[\]\{\}\`\'\;\:\<\>\?\/\,]" 
    RegX.IgnoreCase = True 
    RegX.Global = True 

    StripIllegalChar = RegX.Replace(StrInput, "") 

ExitFunction: 
    Set RegX = Nothing 

End Function 
0

如果你想下載從Outlook附件,試試這個。 私人小組GetAttachments()

Dim ns As Namespace 
Dim Inbox As Outlook.MAPIFolder 
Dim Item As Object 
Dim Atmt As Outlook.Attachment 
Dim FileName As String 

Set ns = GetNamespace("MAPI") 
Set Inbox = ns.Folders("MailboxName").Folders("Inbox") 

If Inbox.Items.Count = 0 Then 
    MsgBox "There are no messages in the Inbox.", vbInformation, _ 
      "Nothing Found" 
    Exit Sub 
End If 

For Each Item In Inbox.Items 
    For Each Atmt In Item.Attachments 
     If Atmt.Type = 1 And InStr(Atmt, "xlsx") > 0 Then 
      FileName = "C:\attachments\" & Atmt.FileName 
      Atmt.SaveAsFile FileName 
     End If 
    Next Atmt 
Next Item 

末次 設置到MS Outlook的引用,請記住, 「MailboxName」 是您的電子郵件地址。