PDA

View Full Version : Outlook Email to Excel and Download Attachment



vistarbistik
10-31-2019, 04:16 AM
Hi

I would like to seek for your assistance on how to run below codes since I am encountering some error. Very new to VBA and I find it really hard to understand and solve it. I would also appreciate if you could add codes to on how to download the attachment to my pc shared drive like Drive C\John\. I'm encountering error like runtime error 404 and runtime error 1004. Please, this is the second time I've posted here and yet unsolved. Kindly help me.



Dim olApp as Outlook.Application
Dim olNS as Outlook.Namespace
Dim olFldr as Outlook.MAPIFolder
Dim OlItms as Outlook.Items
Dim olMail as Variant


Dim 1 as Integer


Set olApp = New Outlook.Application
Set olNS = olApp.GetNamespace ("MAPI")
Set OlFldr = olNS.Pickfolder
if olFldr = "Nothing" then Exit sub
Set olItms = olFldr.Items


i = 1


if olItms.ReceivedTime >= Range("B4").value then


Range("Email_Subject").offset(1,0).Value = OutlookMail.Subject
Range("Email_Date").offset(1,0).Value = Outlookmail.ReceivedTime
Range("Email_Sender").offset(1,0).Value = Outlookmail.SenderName
Range ("Email_Body").offset(1,0).Value = Outlookmail.body
i = 1 + 1
else
endif


Range("A:D").EntireColumn.Autofit


Set Folder = Nothing
Set olFldr = Nothing
Set olApp = Nothing


End Sub



Thanks

Kenneth Hobs
11-06-2019, 01:12 PM
Errors could be the Dim one as Integer and maybe not setting a reference to the Outlook object.

Why not respond to your other thread and resolve that first? http://www.vbaexpress.com/forum/showthread.php?66139-Transfer-Outlook-Email-to-Excel-and-its-Attachment-to-Drive-C&p=395888 If you follow the advice for Option Explicit given, it will help you find some errors before a run. That thread also added a loop to iterate the folder items. It does need a tweak but should be get close. That is best discussed there. Adding the extract attachment feature is trivial.

Always Compile a project from the Debug menu before a trial run.