I need your help guys if you can. I am new to VBA so I am still learning and my code is probably not powerful but I would like to bring it to life. It works, but in a different way than I expected. I am sharing with you my excel VBA code to receive emails from Outlook. When I start Excel and Outlook and run the vba code I get an error on this line =>
Set Folder = Outlook.Session.Folders(MailboxName).Folders(Pst_Folder_Name).Folders(subFolderName)
but when I close and re-open outlook it works and gets all the information into the table up to 30 days back and then I get the message “object doesn’t support this property method” code438.. in this line =>
Sheets(1).Cells(iRow, 1) = Folder.Items.Item(iRow).ReceivedTime
FYI.. I have a default email address set up in outlook which is mine and a shared email address that other ppl also use and there is a sub file in the shared email address. so I’m trying to get the email from the shared email address and subfolder only.
here is the code:
Sub GetEmailsInTo()
Dim ol As Outlook.Application
Dim ns As Outlook.Namespace
Dim Pst_Folder_Name As String
Dim MailboxName As String
Dim subFolderName As String
Set ol = New Outlook.Application
Set ns = ol.GetNamespace("MAPI")
'Mailbox or PST Main Folder Name (As how it is displayed in your Outlook Session)
MailboxName = "[email protected]"
'Mailbox Folder or PST Folder Name (As how it is displayed in your Outlook Session)
Pst_Folder_Name = "Inbox"
'subfolder name
subFolderName = "important"
Set Folder = Outlook.Session.Folders(MailboxName).Folders(Pst_Folder_Name).Folders(subFolderName)
If Folder = "" Then
MsgBox "Invalid Data in Input"
GoTo end_lbl1:
End If
Range("A2", Range("A2").End(xlDown).End(xlToRight)).Clear
'Date
Columns("A:A").Select
Selection.NumberFormat = "[$-409]ddd dd/mm/yy;@"
Range("A2:A500").Select
Selection.ColumnWidth = 13
Range("A2:A500").HorizontalAlignment = xlLeft
Range("A2:A500").VerticalAlignment = xlCenter
Range("A1:E1").Select
With Selection
.VerticalAlignment = xlBottom
.WrapText = False
.RowHeight = 55
.HorizontalAlignment = xlCenter
End With
Range("B2:B500").Select
With Selection
.WrapText = True
.ColumnWidth = 16
.Rows.AutoFit
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlCenter
End With
Range("C2:C500").Select
With Selection
.WrapText = True
.ColumnWidth = 40
.Rows.AutoFit
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlCenter
End With
Range("D2:D500").Select
With Selection
.WrapText = True
.ColumnWidth = 170
.Rows.AutoFit
.VerticalAlignment = xlTop
.HorizontalAlignment = xlLeft
End With
Range("E2:E500").Select
With Selection
.WrapText = True
.ColumnWidth = 50
.Rows.AutoFit
.VerticalAlignment = xlTop
.HorizontalAlignment = xlLeft
End With
'Rad Through each Mail and export the details to Excel for Email Archival
Sheets(1).Activate
For iRow = 2 To Folder.Items.Count
Sheets(1).Cells(iRow, 2).Select
Sheets(1).Cells(iRow, 1) = Folder.Items.Item(iRow).ReceivedTime
Sheets(1).Cells(iRow, 2) = Folder.Items.Item(iRow).SenderName
Sheets(1).Cells(iRow, 3) = Folder.Items.Item(iRow).Subject
Sheets(1).Cells(iRow, 4) = Folder.Items.Item(iRow).To
Sheets(1).Cells(iRow, 5) = Folder.Items.Item(iRow).CC
Next iRow
MsgBox "Email import complete"
end_lbl1:
End Sub
Miki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.