your text
Hi, needed your expert advice on the below code. i am having no error and the code is downloading attachments properly but some of the email missed to download attachments (randomly, downloaded if run macro again) and email moves to process folder. please have a look on the code below and advise.
Sub Outlook_Automation()
Excel.Application.ScreenUpdating = False
Excel.Application.DisplayAlerts = False
'Main Procedure
With ActiveSheet
lastrow = .Cells(.Rows.Count, "B").End(xlUp).Row
End With
ActiveSheet.Range("A" & lastrow + 1).Value = VBA.Now
Set objoutlook = CreateObject("Outlook.Application")
Set outlookNameSpace = objoutlook.GetNamespace("MAPI")
Set olShareName = outlookNameSpace.CreateRecipient("PostRoom@*****.com")
'// Owner's email address
Set MyFolder = outlookNameSpace.GetSharedDefaultFolder(olShareName, olFolderInbox)
Set UnprocessedDestFolder = MyFolder.Folders("Unprocessed")
Set ImageDestFolder = MyFolder.Folders("Image")
Set UnprocessedItems = UnprocessedDestFolder.Items
Set ImageItems = ImageDestFolder.Items
Set processedDestFolder = MyFolder.Folders("Processed")
Set processedItems = processedDestFolder.Items
preeProcessItemCount = processedItems.Count
preUnprocessItemCount = UnprocessedItems.Count
preImageItemCount = ImageItems.Count
Set PreProcessFolder = MyFolder.Folders("Pre-Processing")
Set PreprocessItems = PreProcessFolder.Items
Set PreprocessItems = PreprocessItems.Restrict("[Unread] = true")
preProcessItemCount = PreprocessItems.Count
ActiveSheet.Range("B" & lastrow + 1).Value = preProcessItemCount
savefolder = "S:\*****\RPA\OCR\OCRInputOutlook\"
If PreprocessItems.Count <> 0 Then
For x = PreprocessItems.Count To 1 Step -1
On Error Resume Next
Set PreprocessMSG = PreprocessItems.Item(x)
Call Check_File_Extension
If RequiredBoolFileCheck = True And UnprocessFileCheck = False Then
Call Convert_And_Save(x)
Excel.Application.Wait Now + TimeValue("00:00:02")
PreprocessItems.Item(x).Move processedDestFolder
Excel.Application.Wait Now + TimeValue("00:00:02")
ElseIf ImageBoolFileCheck = True Then
Call Convert_And_Save(x)
Excel.Application.Wait Now + TimeValue("00:00:02")
PreprocessItems.Item(x).Move ImageDestFolder
Excel.Application.Wait Now + TimeValue("00:00:02")
ElseIf PreprocessMSG.Attachments.Count = 0 Or UnprocessFileCheck = True Then
Call Convert_And_Save(x)
Excel.Application.Wait Now + TimeValue("00:00:02")
PreprocessItems.Item(x).Move UnprocessedDestFolder
End If
Excel.Application.Wait Now + TimeValue("00:00:04")
Next x
End If
Set processedItems = processedDestFolder.Items
Set UnprocessedItems = UnprocessedDestFolder.Items
Set ImageItems = ImageDestFolder.Items
ProcessItemCount = processedItems.Count
UnprocessItemCount = UnprocessedItems.Count
ImageItemCount = ImageItems.Count
End Sub
Sub Check_File_Extension()
' Function for checkin extension to define OOS and In scope
RequiredBoolFileCheck = False
ImageBoolFileCheck = False
UnprocessFileCheck = False
i = 1
For Each objAttachment In PreprocessMSG.Attachments
filenamepre(i) = objAttachment.FileName
If LCase(Right(filenamepre(i), 4)) = "docx" Or LCase(Right(filenamepre(i), 4)) = "xlsx" Or LCase(Right(Trim(filenamepre(i)), 3)) = "pdf" Or LCase(Right(filenamepre(i), 3)) = "pdf" Or LCase(Right(filenamepre(i), 3)) = "doc" Or LCase(Trim(Right(filenamepre(i), 3))) = "xls" Or LCase(Right(filenamepre(i), 3)) = "xls" Or UCase(Right(filenamepre(i), 3)) = "CSV" Or LCase(Right(filenamepre(i), 3)) = "odt" Or LCase(Right(filenamepre(i), 3)) = "rtf" Or LCase(Right(filenamepre(i), 4)) = "docm" Then
RequiredBoolFileCheck = True
ElseIf LCase(Right(filenamepre(i), 3)) = "jpg" Or LCase(Right(filenamepre(i), 3)) = "tif" Or LCase(Right(filenamepre(i), 4)) = "jpeg" Then
ImageBoolFileCheck = True
Exit For
Else
UnprocessFileCheck = True
End If
i = i + 1
Next objAttachment
End Sub
Sub Convert_And_Save(ByVal x As Long)
' Function to change the format and save to define folder N:\*****\RPA\OCR\OCR Input Outlook\
Dim wordApp As Word.Application
Dim wordDoc As Word.Document
Dim pdfName As String
savefolder = "S:\*****\RPA\OCR\OCRInputOutlook\"
For Each objAttachment In PreprocessMSG.Attachments
If UCase(Right(objAttachment.FileName, 3)) = "PDF" Or UCase(Right(Trim(objAttachment.FileName), 3)) = "PDF" Or LCase(Right(Trim(objAttachment.FileName), 3)) = "pdf" Or LCase(Right(objAttachment.FileName, 3)) = "pdf" Then
dateformat = Format(Now, "yyyymmddHmmss")
objAttachment.SaveAsFile savefolder & dateformat & Replace(objAttachment.FileName, "-", "")
Excel.Application.Wait Now + TimeValue("00:00:02")
ElseIf UCase(Right(objAttachment.FileName, 4)) = "DOCX" Or UCase(Right(objAttachment.FileName, 3)) = "DOC" Or UCase(Right(objAttachment.FileName, 3)) = "RTF" Or UCase(Right(objAttachment.FileName, 3)) = "ODT" Or UCase(Right(objAttachment.FileName, 4)) = "DOCM" Then
dateformat = Format(Now, "yyyymmddHmmss")
FilePath = "S:\*****\RPA\OCR\Temp Path\" & objAttachment.FileName
objAttachment.SaveAsFile (FilePath)
Excel.Application.Wait Now + TimeValue("00:00:02")
Set wordApp = CreateObject("Word.Application")
Set wordDoc = wordApp.Documents.Open(FilePath)
pdfName = savefolder & Replace(objAttachment.FileName, "-", "") & dateformat & ".pdf"
wordDoc.ExportAsFixedFormat OutputFileName:=pdfName, ExportFormat:= _
wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:=wdExportOptimizeForPrint, Range:=wdExportAllDocument, _
Item:=wdExportDocumentContent, IncludeDocProps:=True
Excel.Application.Wait Now + TimeValue("00:00:02")
wordDoc.Close savechanges:=False
wordApp.Quit
Set wordApp = Nothing
ElseIf UCase(Right(objAttachment.FileName, 4)) = "XLSX" Or UCase(Right(objAttachment.FileName, 3)) = "XLS" Or UCase(Right(objAttachment.FileName, 3)) = "CSV" Then
On Error Resume Next
dateformat = Format(Now, "yyyymmddHmmss")
FilePath = "S:\*****\RPA\OCR\Temp Path\" & objAttachment.FileName
objAttachment.SaveAsFile (FilePath)
Dim wkb As Workbook
Set wkb = Workbooks.Open(FilePath, editable:=True)
If Application.ProtectedViewWindows.Count > 0 Then
Application.ActiveProtectedViewWindow.Edit
End If
Dim ws As Worksheet
Set ws = Nothing
savefolder = "S:\*****\RPA\OCR\OCRInputOutlook\"
pdfName = savefolder & Replace(objAttachment.FileName, "-", "") & dateformat & ".pdf"
Dim cnt As Integer
cnt = 1
For Each ws In wkb.Worksheets
If Not ws.Visible = xlSheetHidden Then
If Not ws.Visible = xlSheetVeryHidden Then
If Not ws.UsedRange.Address = "$A$1" Then
ws.Copy
cnt = cnt + 1
End If
Set newWb = ActiveWorkbook
Set newWs = newWb.ActiveSheet
If cnt = 1 Then
newWs.ExportAsFixedFormat Type:=xlTypePDF, FileName:=pdfName, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
Else
pdfName = savefolder & Replace(objAttachment.FileName, "-", "") & dateformat & "_" & cnt & ".pdf"
newWs.ExportAsFixedFormat Type:=xlTypePDF, FileName:=pdfName, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End If
newWb.Close savechanges:=False
Else
ws.Select
ws.ExportAsFixedFormat Type:=xlTypePDF, FileName:=pdfName, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
End If
End If
Next ws
wkb.Save
wkb.Close savechanges:=False
End If
Next objAttachment
End Sub
your help is appreciated....
After removing
On Error Resume Next
, this may aid your debugging.