netrixuser
Board Regular
- Joined
- Jan 21, 2019
- Messages
- 77
- Office Version
- 365
- Platform
- Windows
Hi all,
The long code posted below is working for me but I need to tweak it so that instead of looking at a folder beneath my Inbox in Outlook, I want it to look at a folder in a shared inbox.
I have found similar to what I want to do on Mr Excel but I cannot get it to work for me - the small snippet of code is what I am trying to get to work:
There is a bunch of other stuff in this code to check for unread emails, open the zipped attachment and run (call) a few other Subs which make the sheet look pretty etc. But the big ask now is to get it to do all this from a shared mailbox.
Whilst you clever folk are here - I would really like to be able to delete the email from the shared mailbox folder after the csv attachment has been loaded into the spreadsheet.
Many thanks in advance !
The long code posted below is working for me but I need to tweak it so that instead of looking at a folder beneath my Inbox in Outlook, I want it to look at a folder in a shared inbox.
I have found similar to what I want to do on Mr Excel but I cannot get it to work for me - the small snippet of code is what I am trying to get to work:
VBA Code:
Set objowner = OutlookNamespace.CreateRecipient("SharedoutlookMailbox@ABC.com") 'the name of the shared mailbox in Outlook
objowner.Resolve 'objowner doesn't capitalise to objOwner so errors as no variable set
If objowner.Resolved Then
Set Folder = OutlookNamespace.GetSharedDefaultFolder(olFolderInbox).Folders("TV Vendor Alerts") 'folder beneath the shared mailbox Inbox
End If
There is a bunch of other stuff in this code to check for unread emails, open the zipped attachment and run (call) a few other Subs which make the sheet look pretty etc. But the big ask now is to get it to do all this from a shared mailbox.
Whilst you clever folk are here - I would really like to be able to delete the email from the shared mailbox folder after the csv attachment has been loaded into the spreadsheet.
Many thanks in advance !
VBA Code:
Dim OutlookApp As Outlook.Application
Dim OutlookNamespace As Namespace
Dim Folder As MAPIFolder
Dim OutlookMail As Variant
Dim OutlookAtch As Object
Dim NewFileName As String
Dim OutlookSent As Variant
Dim lastRow As Integer
lastRow = 1
Const VendorEmailSenderAddress = "no-reply@company.com"
Const VendorEmailSubjectKey = "Missing Recording Alerts"
Dim shell As Shell32.shell
Dim DestinationFolder As Shell32.Folder
Dim SourceFolder As Shell32.Folder
Set shell = New Shell32.shell
Dim AttachmentDestinationFolder As Variant
AttachmentDestinationFolder = "C:\Temp\UnzippedAttachments"
Set DestinationFolder = shell.Namespace(AttachmentDestinationFolder)
If Dir(AttachmentDestinationFolder, vbDirectory) = "" Then
MsgBox "Destination folder not found, please create: " & AttachmentDestinationFolder, vbExclamation, "Exit"
Exit Sub
End If
Dim AttachmentSourceFileName As String
AttachmentSourceFileName = "c:\Temp\Attachments\log.zip"
Set OutlookApp = New Outlook.Application
Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
[COLOR=#e25041]Set Folder = OutlookNamespace.GetDefaultFolder(olFolderInbox).Folders("New Alerts") 'this is, I believe, where I need to tweak [/COLOR]
If Folder.UnReadItemCount = 0 Then
MsgBox ("There are no Unread Emails from vendor- The script will now end"), vbExclamation
End
End If
Dim UnredEmailCnt As Integer
If Folder.UnReadItemCount > 0 Then
UnredEmailCnt = Folder.UnReadItemCount
MsgBox "Found " & UnredEmailCnt & " matching Alert E-mail(s)"
End If
Dim EmailSubject As String
Dim EmailSenderEmailAddress As String
For Each OutlookMail In Folder.Items
EmailSubject = LCase(OutlookMail.Subject)
EmailSenderEmailAddress = LCase(OutlookMail.SenderEmailAddress)
If ((StrComp(EmailSenderEmailAddress, VendorEmailSenderAddress, vbTextCompare) = 0) And InStr(1, EmailSubject, LCase(VendorEmailSubjectKey), vbTextCompare)) Then
If OutlookMail.UnRead = True Then
OutlookMail.Attachments.Item(1).SaveAsFile (AttachmentSourceFileName)
Set SourceFolder = shell.Namespace(AttachmentSourceFileName)
DestinationFolder.CopyHere SourceFolder.Items '
OutlookMail.UnRead = False
Dim oFSO As Object
Dim oFolder As Object
Dim oFile As Object
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(AttachmentDestinationFolder)
For Each oFile In oFolder.Files
FileName = oFile.Path
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)
Call DeleteExisting
Call CopyNew
Call RemClosed
Call ChgNewtoExist
End If
End If
Next OutlookMail
Set Folder = Nothing
Set OutlookNamespace = Nothing
Set OutlookApp = Nothing
End Sub