2017-07-13 15 views
2

私は受信トレイにある電子メールのすべての添付ファイルを指定されたディレクトリに保存するマクロを持っています。しかし、電子メールの件名をファイル名として添付ファイルに保存したいと思います。Outlook件名で添付ファイルを保存する

これは私の最初のマクロであり、VBAを初めて見ているので、どんなポインタも大変ありがとうございます。

Private Sub Outlook_VBA_Save_Attachment() 
    ''Variable declarions 
    Dim ns As NameSpace 
    Dim inb As Folder 
    Dim itm As MailItem 
    Dim atch As Attachment 

    ''Variables Initialization 
    Set ns = Outlook.GetNamespace("MAPI") 
    Set inb = ns.GetDefaultFolder(olFolderInbox) 
    File_Path = "H:\Notes\" 

    ''Loop Thru Each Mail Item 
    For Each itm In inb.Items 

    ''Loop Thru Each Attachment 
     For Each atch In itm.Attachments 
      If atch.Type = olByValue Then 
       atch.SaveAsFile File_Path & atch.FileName 
      End If 
     Next atch 
    Next itm 

    '''''Notify the Termination of Process 
    MsgBox "Attachments Extracted to: " & File_Path 
End Sub 

答えて

3

あなたがする必要があるのは1行に変更です:元のファイルの拡張子を含めるには

atch.SaveAsFile File_Path & itm.Subject 

を、あなたはそれをつかむためにFileSystemObjectオブジェクトを使用することができます。

Private Sub Outlook_VBA_Save_Attachment() 
    ''Variable declarions 
    Dim ns As Namespace 
    Dim inb As Folder 
    Dim itm As MailItem 
    Dim atch As Attachment 
    Dim fso As FileSystemObject 

    ''Variables Initialization 
    Set ns = Outlook.GetNamespace("MAPI") 
    Set inb = ns.GetDefaultFolder(olFolderInbox) 
    File_Path = "H:\Notes\" 
    Set fso = New FileSystemObject 

    ''Loop Thru Each Mail Item 
    For Each itm In inb.Items 

    ''Loop Thru Each Attachment 
     For Each atch In itm.Attachments 
      If atch.Type = olByValue Then 
       atch.SaveAsFile File_Path & itm.Subject & "." & fso.GetExtensionName(atch.Filename) 
      End If 
     Next atch 
    Next itm 

    '''''Notify the Termination of Process 
    MsgBox "Attachments Extracted to: " & File_Path 
End Sub 

これには、Microsoft Scripting Runtimeへの参照が必要です。

+0

これはうまくいきますが、ファイル拡張子は削除されていますが、ありがとうございます。それを修正する方法はありますか? – BrettJ

+0

ファイル拡張子を含むようにコードサンプルを変更しました。 –

+0

美しい!助けてくれてありがとう。将来のユーザーにとって、これはMS Scripting Runtimeへの参照を有効にする方法です。 https://stackoverflow.com/questions/3233203/how-do-i-use-filesystemobject-in-vba – BrettJ

関連する問題