Pull email from specified folder instead of folderpicker

coryjacques

New Member
Joined
May 17, 2019
Messages
14
The below code allows me to select a folder from outlook and pull some information from it. I didn't create this code - someone way more intelligent did! What I'm having issues with is getting rid of the selector for folder; I want to specify a mail folder within the code that will never vary. I've tried a few things that don't seem to work... Any suggestions?

Code:
Sub GetMailInfo()
Dim results() As String
  ' get contacts
  results = ExportEmails(True)

  Range(Cells(1, 1), Cells(UBound(results), UBound(results, 2))).Value = results
    MsgBox "Completed"
End Sub
Function ExportEmails(Optional headerRow As Boolean = False) As String()
Dim objOutlook As Object ' Outlook.Application
Dim objNamespace As Object ' Outlook.Namespace
Dim strFolderName As Object
Dim objMailbox As Object
Dim objFolder As Object
Dim mailFolderItems As Object ' Outlook.items
Dim folderItem As Object
Dim msg As Object ' Outlook.MailItem
Dim tempString() As String
Dim i As Long
Dim numRows As Long
Dim startRow As Long
Dim jAttach As Long ' counter for attachments
Dim debugMsg As Integer
Sheets("OutlookResults").Select
Sheets("OutlookResults").Cells.ClearContents
Range("A1").Select
Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set strFolderName = objNamespace.PickFolder
Set mailFolderItems = strFolderName.Items
  If headerRow Then
    startRow = 1
  Else
    startRow = 0
  End If
  numRows = mailFolderItems.Count
  ReDim tempString(1 To (numRows + startRow), 1 To 100)
  For i = 1 To numRows
    Set folderItem = mailFolderItems.Item(i)
    If IsMail(folderItem) Then
      Set msg = folderItem
    End If
    With msg
      tempString(i + startRow, 1) = .Subject
      tempString(i + startRow, 2) = .SenderName
      tempString(i + startRow, 3) = .SentOn
      tempString(i + startRow, 4) = .ReceivedTime
      tempString(i + startRow, 5) = .To
    End With
  Next i
  ' first row of array should be header values
  If headerRow Then
    tempString(1, 1) = "Subject"
    tempString(1, 2) = "SenderName"
    tempString(1, 3) = "SentOn"
    tempString(1, 4) = "Received Time"
    tempString(1, 5) = "To"
  End If
  ExportEmails = tempString

End Function
Function IsMail(itm As Object) As Boolean
  IsMail = (TypeName(itm) = "MailItem")
End Function
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Add this after the PickFolder line in your original code:
Code:
MsgBox strFolderName.FolderPath
and tell me what is displayed in the message box when you run it.

For example, if "\\MyAccount\folder1\subfolder1" is displayed, replace the PickFolder line with:
Code:
    Set strFolderName = objNamespace.Folders("MyAccount").Folders("folder1").Folders("subfolder1")
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,264
Members
452,627
Latest member
KitkatToby

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top