How to import email attachments from Outlook into an Excel worksheet automatically using VBA. Here's the complete VBA code:
Option Explicit
Const AttachmentPath As String = "C:\myattachments\"
Sub GetFromOutlook2()
Dim outlookAtch As Object
Dim NewFileName As String
NewFileName = AttachmentPath & Format(Date, "DD-MM-YYYY") & "-"
Dim OutlookApp As Outlook.Application
Dim OutlookNamespace As Namespace
Dim Folder As MAPIFolder
Dim OutlookMail As Variant
Dim i As Integer
Dim col As Long
col = 0
Set OutlookApp = New Outlook.Application
Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
Set Folder = OutlookNamespace.GetDefaultFolder(olFolderInbox).Folders("CJ")
i = 1
For Each OutlookMail In Folder.Items
If OutlookMail.Attachments.Count "GREATER THAN" 0 Then
For Each outlookAtch In OutlookMail.Attachments
If OutlookMail.ReceivedTime "GREATER THAN or EQUAL TO" = Range("start_Date").Value Then
Range("email_Subject").Offset(i, 0).Value = OutlookMail.Subject
Range("email_Date").Offset(i, 0).Value = OutlookMail.ReceivedTime
Range("email_Sender").Offset(i, 0).Value = OutlookMail.SenderName
Range("email_Body").Offset(i, 0).Value = OutlookMail.Body
outlookAtch.SaveAsFile NewFileName & outlookAtch.Filename
Range("email_attachment").Offset(i, col).Value = outlookAtch
col = col + 1
End If
Next outlookAtch
col = 0
i = i + 1
End If
Next OutlookMail
Set Folder = Nothing
Set OutlookNamespace = Nothing
Set OutlookApp = Nothing
End Sub
www.exceltrain...
Негізгі бет Import Email Attachments from Outlook into Excel Automatically
Пікірлер: 108