netrixuser
Board Regular
- Joined
- Jan 21, 2019
- Messages
- 77
- Office Version
- 365
- Platform
- Windows
Any help given, as always, gratefully received....
I have the code below that loops through any unread emails in a certain folder and imports the attached csv file into a Workbook.
My issue is in trying to get the code to open the oldest unread email first, followed by next oldest etc. At the moment the code appears to be a tad random in which email it opens first.
To test I have 5 emails in the referenced email folder beneath the Inbox, with dates and times as in the table below: [The emails are currently sorted by attachment size]
When the code runs it selects the email sent on Friday 4th August first, which is neither the oldest or the newest, nor the first or the last in the folder. If I have the email folder sorted by date, the code selects the newest email first - and I want the exact opposite.
I added this small piece of code below, just before the Loop "For each AlertEmail in Folder.ITems" but it made no difference, set to True or False
Main Code
I have the code below that loops through any unread emails in a certain folder and imports the attached csv file into a Workbook.
My issue is in trying to get the code to open the oldest unread email first, followed by next oldest etc. At the moment the code appears to be a tad random in which email it opens first.
To test I have 5 emails in the referenced email folder beneath the Inbox, with dates and times as in the table below: [The emails are currently sorted by attachment size]
When the code runs it selects the email sent on Friday 4th August first, which is neither the oldest or the newest, nor the first or the last in the folder. If I have the email folder sorted by date, the code selects the newest email first - and I want the exact opposite.
Wed 09/08/2023 03:47 |
Fri 04/08/2023 04:28 |
Tue 08/08/2023 22:24 |
Thu 03/08/2023 05:03 |
Thu 03/08/2023 11:34 |
I added this small piece of code below, just before the Loop "For each AlertEmail in Folder.ITems" but it made no difference, set to True or False
VBA Code:
Dim Items As Object
Set Items = Folder.Items
Items.Sort "Receivedtime", True
For Each AlertEmail In Folder.Items
Main Code
VBA Code:
Sub TESTGetEmailFromOutlook()
' Need References added for Microsoft Outlook Object Library and
' "Microsoft Shell Controls And Automation"
Dim OutlookApp As Outlook.Application
Dim OutlookNamespace As Namespace
Dim Folder As MAPIFolder 'variable to store the name of the Outlook folder
Dim Alertemail As Variant
Dim OutlookAtch As Object
Dim NewFileName As String
Dim OutlookSent As Variant
Dim lastRow As Integer 'variable used to increment row by 1 when refreshing the query
lastRow = 1
Const AlertemailSenderAddress = "no-reply@abc123.com"
Const AlertemailSubjectKey = "Missing Recording Alerts Update"
Dim shell As Shell32.shell
Dim DestinationFolder As Shell32.Folder
Dim SourceFolder As Shell32.Folder
'Configure Paths for zip files on disk
Set shell = New Shell32.shell
Dim AttachmentDestinationFolder As Variant
AttachmentDestinationFolder = "C:\Temp\UnzippedAttachments"
Set DestinationFolder = shell.Namespace(AttachmentDestinationFolder)
'check for Attachment Destination Folder
If Dir(AttachmentDestinationFolder, vbDirectory) = "" Then
MsgBox "Destination folder not found, please create: " & AttachmentDestinationFolder, vbExclamation, "Exit"
Exit Sub
End If
Dim AttachmentSourceFileName As String 'variable to store the name of the zipped file
AttachmentSourceFileName = "c:\Temp\Attachments\log.zip" 'set the variable to Path:Log.zip
Set OutlookApp = CreateObject("Outlook.Application")
Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
Dim SharedMailbox As Object 'variable to store name of Outlook folder
Dim MailFolder As Object
Dim OutlookNS As Outlook.Namespace
Set OutlookNS = OutlookApp.GetNamespace("MAPI")
OutlookNS.Logon
Set SharedMailbox = OutlookNS.CreateRecipient("SharedMail@ABC.com") 'store Outlook folder as the Variable "SharedMailbox"
SharedMailbox.Resolve
'if the ShareMailbox name exists, store the name of the Outlook folder as the Variable "Folder"
If SharedMailbox.Resolved Then
Set Folder = OutlookNS.GetSharedDefaultFolder(SharedMailbox, olFolderInbox).Folders("New Alerts")
End If
'if Outlook folder is empty - display message and end code
If Folder.UnReadItemCount = 0 Then
MsgBox ("There are no Unread Emails - The script will now end"), vbExclamation
End
End If
Dim UnredEmailCnt As Integer 'variable to store number of unread emails in Oulook folder
'if unread emails, count and display number
If Folder.UnReadItemCount > 0 Then
UnredEmailCnt = Folder.UnReadItemCount
MsgBox "Found " & UnredEmailCnt & " matching Alert E-mail(s)"
End If
Dim EmailSubject As String 'variable to store email subject
Dim EmailSenderEmailAddress As String 'variable to store email senders address
For Each Alertemail In Folder.Items
EmailSubject = LCase(Alertemail.Subject) 'assign value to variable
EmailSenderEmailAddress = LCase(Alertemail.SenderEmailAddress) 'assign value to variable
If ((StrComp(EmailSenderEmailAddress, AlertemailSenderAddress, vbTextCompare) = 0) And InStr(1, EmailSubject, LCase(AlertemailSubjectKey), vbTextCompare)) Then
If Alertemail.UnRead = True Then
Alertemail.Attachments.Item(1).SaveAsFile (AttachmentSourceFileName) 'Save zipped attachment to Attachments folder
Set SourceFolder = shell.Namespace(AttachmentSourceFileName) 'set the source file name to path:Log.zip
DestinationFolder.CopyHere SourceFolder.Items '??? UnZip File and save csv file to Unzipped Attachments Folder
Alertemail.UnRead = False 'set email to read - needed if deleting on next line ???
'Alertemail.Delete 'delete email
Dim oFSO As Object
Dim oFolder As Object
Dim oFile As Object
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(AttachmentDestinationFolder) 'folder where attachment (csv) file is temp stored
For Each oFile In oFolder.Files 'for loop - not really necessary as only one csv file in the folder at any one time ??
FileName = oFile.Path
'store the unique number from the filename in the Resource Sheet
fname = Left(Right(FileName, 18), 14)
Sheets("Resource Sheet").Range("S1").Value = fname
Sheets("resource sheet").Range("S1").Copy
Sheets("resource sheet").Range("T1").Insert Shift:=xlDown
With Sheets("resource sheet").Range("AT1").End(xlDown)
Sheets("resource sheet").Range("U1").Value = Application.WorksheetFunction.CountIf(Range("T1", Range("T1").End(xlDown)), ">1")
End With
Exit For
Next oFile
Dim ws As Worksheet
Set ws = ActiveWorkbook.Sheets("Scratch Sheet")
On Error Resume Next
With ws.QueryTables.Add(Connection:="TEXT;" & FileName, _
Destination:=ws.Range("A" & lastRow))
.TextFileParseType = xlDelimited
.TextFileCommaDelimiter = True
'.TextFileStartRow = 2
.Refresh
End With
Kill (FileName) 'delete file from AttachmentDestinationFolder
'Call DeleteExisting 'new attachment will have existing alerts - delete these
'Call CopyNew 'copy new alerts from Scratch Sheet to Current Alerts sheet
'Call RemClosed ' remove alerts from Current Alerts that are CLOSED in Scratch Sheet
End If
End If
Next Alertemail
'Set MailFolder = Nothing
Set Folder = Nothing
Set OutlookNamespace = Nothing
Set OutlookApp = Nothing
Set shell = Nothing
Set DestinationFolder = Nothing
Set OutlookNS = Nothing
Set SourceFolder = Nothing
Set oFSO = Nothing
Set oFolder = Nothing
Set ws = Nothing
Set SharedMailbox = Nothing
End Sub