Georgiboy
Well-known Member
- Joined
- Nov 7, 2008
- Messages
- 1,501
- Office Version
- 365
- 2016
- Platform
- Windows
Hi all,
I have been passed the below code that has an error finding a folder within a shared inbox, the error is not for all users however, everyone is using Excel 2016 and sometimes users get the error, other times they don't. I can't display the error at this stage as i don't get the error often. It is basically an 'Object not found' error from what i understand.
Could anyone more familiar with writing code that talks to outlook cast their eye over the code to see if there is anything obvious missing or needs changing?
I have tried early binding and late binding methods but to no avail, any help would be apreciated. excuse the code being messy, it has been pushed and pulled to try and find the error, it works as is but will produce an error more for some and less for others:
Thanks in advance
George
I have been passed the below code that has an error finding a folder within a shared inbox, the error is not for all users however, everyone is using Excel 2016 and sometimes users get the error, other times they don't. I can't display the error at this stage as i don't get the error often. It is basically an 'Object not found' error from what i understand.
Could anyone more familiar with writing code that talks to outlook cast their eye over the code to see if there is anything obvious missing or needs changing?
I have tried early binding and late binding methods but to no avail, any help would be apreciated. excuse the code being messy, it has been pushed and pulled to try and find the error, it works as is but will produce an error more for some and less for others:
VBA Code:
Dim FilePath As String
Sub attachmentsave()
Dim olApp As Object
Set olApp = CreateObject("Outlook.Application")
Dim wb As Workbook, tmpDate As String, cnt As Long
Dim omailitem As Object
Dim inFol As Object, destFol As Object, errorFol As Object
Dim atmt As Object
Dim tmpSTR1 As String, start1 As Long, end1 As Long, tmpSTR2 As String
Dim olMailbox As Object
Dim olInbox As Object
Dim subFolder As Object
Dim olNS As Object
Set olNS = olApp.GetNamespace("MAPI")
Set olMailbox = olNS.Folders("Distribution Returns")
Set olInbox = olMailbox.Folders("Inbox")
Set inFol = olInbox.Folders("Return Notification") ' I get the error here stating that it can't find the folder but it does exist within the shared inbox!
Set destFol = olInbox.Folders("Return Completed")
Set errorFol = olInbox.Folders("Return Error")
Set wb = ThisWorkbook
Application.ScreenUpdating = False
Application.EnableEvents = False
FilePath = wb.Path & "\Return Downloads\"
If inFol.Items.Count < 1 Then
MsgBox "There are no mails to look at in the 'Return Notification' Outlook folder", vbExclamation, "Error"
Exit Sub
End If
On Error Resume Next
Kill FilePath & "*.*" ' clear old files
On Error GoTo 0
Set omailitem = olApp.CreateItem(olMailItem)
For Each omailitem In inFol.Items
For Each atmt In omailitem.Attachments
If LCase(Left(atmt.DisplayName, 16)) = "returns template" Then
cnt = cnt + 1
atmt.SaveAsFile FilePath & Format(Now(), "ddmmyyyyhhmmss") & "-" & cnt & ".xlsm"
DoEvents
DoEvents
End If
Next
omailitem.UnRead = False
' no valid attachments so ove mail to error
If cnt = 0 Then
omailitem.Move errorFol
MsgBox "Mail moved to error folder in Outlook, click 'OK' to continue" & vbNewLine & vbNewLine & "They have been moved due to an issue with the attachment"
End If
'cnt = 0
Next
Do Until inFol.Items.Count = 0
inFol.Items(1).Move destFol
Loop
Application.ScreenUpdating = True
Call test1
End Sub
Thanks in advance
George