VBA for transfer emails from Outlook app to excel

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

New contributor

Miki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật