Get email from Shared Outlook mailbox [Office 365] - assistance needed please

netrixuser

Board Regular
Joined
Jan 21, 2019
Messages
77
Office Version
  1. 365
Platform
  1. 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:
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
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Definitely having a senior moment !!

I have now added
VBA Code:
Dim objOwner as Outlook.Recipient

but getting an error at this line now:
Type MisMatch (with olFolderInbox in the code below highlighted)

VBA Code:
    If objOwner.Resolved Then
        Set Folder = OutlookNamespace.GetSharedDefaultFolder(olFolderInbox).Folders("TVO Vendor Alerts") 
    End If
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,109
Members
453,021
Latest member
Justyna P

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