#Region ;**** Directives created by AutoIt3Wrapper_GUI **** #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.14.5 Author: myName Script Function: Print attactch files from outlook mails i at specific folder. 1. Find fist Oldest mail in folder. 2. Sort/delete and print files. 3. Remove email and left over files. #ce ---------------------------------------------------------------------------- ; Script Start #include #include #include #include #include HotKeySet("+!e", "_Exit") ;Shift-Alt-E to Exit the script Global $DebugOn = 1 ; ***************************************************************************** ; Test contion ; ***************************************************************************** Global $oOutlook = _OL_Open() ; Oping connection to Outlook ; Temp variable $sSaveDir = @TempDir & "\Automatically Print\" ; Temp save location $sLogDir = "C:\Temp\" ; Log file location $OutlookFolder = "*\Indbakke\Print Automatically" ; Outlook folder to monitor. Global $FileFormats = StringSplit("pdf,docx,doc",",") ; Files to print Global $mailsArray[1] = ["0"] ; Array of printet mails after program run Initialize() ; ####### ; # Start main loop If $DebugOn = 1 Then _WriteErrorLog("Start While loop") If $DebugOn = 1 Then ConsoleWrite("Start While loop - ") While 1 ; Check for new mails in $OutlookFolder Global $aOL_Item = _OL_ItemFind($oOutlook, $OutlookFolder, $olMail, "", "", "", "EntryID,Subject,CreationTime" , "[Date]") If IsArray($aOL_Item) Then ; if item found then contionue ; Check if folder is empty Local $iSize = DirGetSize($sSaveDir) ;Check temp is empty If $DebugOn = 1 Then ConsoleWrite("DirGetSize: " & $iSize) if $iSize > 0 Then MsgBox($MB_SYSTEMMODAL, "", "'$sSaveDir' has files in it. Deleting files." & @CRLF) CleanOpFiles() Else If $aOL_Item[0][0] <> 0 or $aOL_Item[0][0] == "" Then If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemAttachmentGet Script", "Error getting list of attachments for mail in folder 'Print Automatically'. @error = " & @error & ", @extended = " & @extended) ;~ _ArrayDisplay($aOL_Item, "") If $DebugOn = 1 Then _WriteErrorLog("### Outlook Items found: " & $aOL_Item[1][1]) If $DebugOn = 1 Then ConsoleWrite("Outlook Items found" & $aOL_Item[1][1]) If $DebugOn = 1 Then ConsoleWrite("n\Func ExportFiles - ") ExportFiles() ; Run export for the first email found in array Sleep(500) If $DebugOn = 1 Then ConsoleWrite("Func SortPrint - ") SortPrint() ; Run Print files sleep(500) If $DebugOn = 1 Then ConsoleWrite("Func CleanOpFiles - ") CleanOpFiles() ; Run Cleenop files and delete the mail, so we don't check it again EndIf EndIf EndIf ; Avoid high CPU usage. Sleep(1000) WEnd ; ####### ; # Start ExportFiles ; # Getting all attactch files in mail and save them to Temp Save locaton Func ExportFiles() If $DebugOn = 1 Then _WriteErrorLog("### ExportFiles") If $DebugOn = 1 Then ConsoleWrite("Outlook: " & $oOutlook & " Error: " & @error & " Extended: " & @extended & @CRLF) ; Get a list of all attachments of a mail item ; ***************************************************************************** $Result = _OL_ItemAttachmentGet($oOutlook, $aOL_Item[1][0], Default) If @error <> 0 Then Return ConsoleWrite("_OL_ItemAttachmentGet: " & $oOutlook & " Error: " & @error & " Extended: " & @extended & @CRLF) if $Result <> 0 Then If $DebugOn = 1 Then _WriteErrorLog("Files to saved: " & _ArrayToString($Result, ", ")) _OL_ItemSave($oOutlook, $aOL_Item[1][0], Default, $sSaveDir, $olHTML, 2+4) If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemSave Script", "Error saving mail item to " & $sSaveDir & ". @error = " & @error & ", @extended = " & @extended) EndIf EndFunc ; ####### ; # Start SortPrint ; # Getting list of all files in $sSaveDir and print physical copies of all file types that is in $FileFormats array ; # Files not in $FileFormats are deleted Func SortPrint() If $DebugOn = 1 Then _WriteErrorLog("### SortPrint") If $DebugOn = 1 Then ConsoleWrite("ExportFiles: Start") Local $FilesArray = _FileListToArray($sSaveDir, "*", 1) ; getting list _ArraySort($FilesArray) If $DebugOn = 1 Then _WriteErrorLog("$FilesArray: " & _ArrayToString($FilesArray, ",")) ; ##### ; # Chect to see if mail alrady printet. If $DebugOn = 1 Then ConsoleWrite("See if mail alrady printet: " & $aOL_Item[1][0] & @CRLF) Local $mailsArrayFind = _ArraySearch($mailsArray, $aOL_Item[1][0], 0, 0, 0, 0, 1) If Not @error Then If $DebugOn = 1 Then _WriteErrorLog("Mail Check if allready printet: " & $mailsArrayFind & " -VS-") If $DebugOn = 1 Then _WriteErrorLog("Mail Check if allready printet: " & $aOL_Item[1][0]) MsgBox($MB_SYSTEMMODAL, 'Email print', 'Mail Check if allready printet: ' & $mailsArrayFind) Else If $DebugOn = 1 Then _WriteErrorLog("Mail not printet: " & $mailsArrayFind) ; Get list of save files and print only thoes in $FileFormats array and delete the rest for $i = 1 To UBound($FilesArray) -1 If $DebugOn = 1 Then ConsoleWrite("ExportFiles: loop File array") Local $sDrive = "", $sDir = "", $sFileName = "", $sExtension = "", $vFileName = $FilesArray[$i] Local $aPathSplit = _PathSplit($vFileName, $sDrive, $sDir, $sFileName, $sExtension) ; Check if file extension is in $FileFormats $FileTypeTemp = StringTrimLeft(StringLower($sExtension), 1) If $DebugOn = 1 Then ConsoleWrite("Array looking for: " & $FileTypeTemp & " ") Local $ret = _ArraySearch($FileFormats, $FileTypeTemp, 0, 0, 0, 0, 1, 2) If $DebugOn = 1 Then ConsoleWrite("Array found at: " & $FileTypeTemp & $ret & " ") If $DebugOn = 1 Then _WriteErrorLog("Array looking for: " & $FileTypeTemp & " And found: " & $ret) ; File is wrong format, so delete it If $ret = -1 Then If $DebugOn = 1 Then ConsoleWrite("Deleting item: " & $vFileName & " ") If $DebugOn = 1 Then _WriteErrorLog("Deleting item: " & $vFileName) FileDelete($sSaveDir & $vFileName) ; Continue to print Elseif $ret <> -1 Then If $DebugOn = 1 Then ConsoleWrite("Printing item: " & $vFileName & " ") If $DebugOn = 1 Then _WriteErrorLog("Printing item: " & $vFileName) if $FileTypeTemp = "docx" or $FileTypeTemp = "doc" then PrintWordDocument($vFileName) elseif $FileTypeTemp = "pdf" Then PrintPDFDocument($vFileName) ; # print it all if all else fails. Else If $DebugOn = 1 Then _WriteErrorLog("Print all else: " & $vFileName) If $DebugOn = 1 Then ConsoleWrite("Print all else: " & $vFileName & " ") ShellExecuteWait($sSaveDir & "\" & $vFileName,"","","Print",@SW_MINIMIZE) EndIf EndIf Next If $DebugOn = 1 Then ConsoleWrite("Add mailID to Array: " & $aOL_Item[1][0] & @CRLF) If $DebugOn = 1 Then _WriteErrorLog("Add mailID to Array: " & $aOL_Item[1][0]) _ArrayAdd($mailsArray, $aOL_Item[1][0]) EndIf EndFunc ; Delete email from Outlook and delete all remaining files Func CleanOpFiles() If $DebugOn = 1 Then _WriteErrorLog(" ### CleanOpFiles") If $DebugOn = 1 Then ConsoleWrite("Starting Func CleanOpFiles") Sleep(1000) ; Delete email if $aOL_Item[0][0] <> 0 Then If $DebugOn = 1 Then _WriteErrorLog("Deleting mail: " & $aOL_Item[1][0] & " - " & $aOL_Item[1][2]) _OL_ItemDelete($oOutlook, $aOL_Item[1][0], Default) If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemDelete Script", "Error deleting mail. @error = " & @error & ", @extended = " & @extended) If $DebugOn = 1 Then ConsoleWrite("Deleting item: " & $aOL_Item[1][0] & " ") Else If $DebugOn = 1 Then _WriteErrorLog("No email to delete") If $DebugOn = 1 Then ConsoleWrite("No email to delete." & @CRLF) EndIf ; Delete files $aFilesArray = _FileListToArray ( $sSaveDir, "*", 1) If $DebugOn = 1 Then _WriteErrorLog("Delete all files in folder: " & _ArrayToString($aFilesArray, ",")) for $i = 1 To UBound($aFilesArray) -1 FileDelete($sSaveDir & $aFilesArray[$i]) Next EndFunc ; ###### ; # Tests read-write permissions and sets up save folder Func Initialize() If $DebugOn = 1 Then _WriteErrorLog("####### Code start #######") If $DebugOn = 1 Then _WriteErrorLog("Remove old temp DIR") ; Remove old savefolder DirRemove($sSaveDir, $DIR_REMOVE) ; Check to see if save temp folder exists If FileExists($sSaveDir) Then If $DebugOn = 1 Then ConsoleWrite("Save Folder found ") Else If $DebugOn = 1 Then ConsoleWrite("Save Folder not found" & @error & @CRLF & "Creating folder.") DirCreate($sSaveDir) EndIf ; ####### ; # Test temp save folder write primisions ; Create a constant variable in Local scope of the filepath that will be read/written to. If $DebugOn = 1 Then _WriteErrorLog("Test write permisions") Local Const $sFilePath = _WinAPI_GetTempFileName($sSaveDir) Local $iFileExists = FileExists($sFilePath) If $iFileExists Then If $DebugOn = 1 Then ConsoleWrite("The file exists." & @CRLF & "FileExist returned: " & $iFileExists & @CRLF) Else MsgBox($MB_SYSTEMMODAL, "", "Cannot create file in '$sSaveDir'. Error: ") EndIf ; Delete the temporary file. FileDelete($sFilePath) EndFunc ; ###### ; Print word files Func PrintWordDocument(ByRef $vFileName) ; Open the document Local $oWord = _Word_Create(False) Local $oDoc = $oWord.Documents.Open($sSaveDir & $vFileName, Default, True, false, Default, "", Default, Default, "", Default, False ) If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocPrint", "Error opening '" & $vFileName & "' " & @CRLF & "@error = " & @error & ", @extended = " & @extended) ; Print the complete document with default values Local $sActivePrinter = $oDoc.Application.ActivePrinter If $DebugOn = 1 Then ConsoleWrite("Print word: " & $vFileName & " ") If $DebugOn = 1 Then _WriteErrorLog("Print word: " & $vFileName) _Word_DocPrint($oDoc) If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocPrint", "Error printing the document." & @CRLF & "@error = " & @error & ", @extended = " & @extended) ; Close a Word coument _Word_DocClose($oDoc) EndFunc ; ###### ; # Pint pdf i adobe Func PrintPDFDocument($vFileName) Local $iFileExists86 = FileExists("C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe") Local $iFileExists64 = FileExists("C:\Program Files\Adobe\Acrobat Reader DC\Reader\AcroRd64.exe") ; Display a message of whether the file exists or not. If FileExists("C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe") Then If $DebugOn = 1 Then ConsoleWrite("Adobe 32Bit print: " & $vFileName & " ") If $DebugOn = 1 Then _WriteErrorLog("Adobe 32Bit print: " & $vFileName) If $DebugOn = 1 Then _WriteErrorLog('"C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" /h /p "' & $sSaveDir & $vFileName) If $DebugOn = 1 Then ConsoleWrite('"C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" /h /p "' & $sSaveDir & $vFileName & '" ') Local $iPID = Run('"C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" /h /p "' & $sSaveDir & $vFileName & '" ',"",@SW_MINIMIZE) sleep(6500) ProcessClose($iPID) ElseIf FileExists("C:\Program Files\Adobe\Acrobat Reader DC\Reader\AcroRd64.exe") Then If $DebugOn = 1 Then ConsoleWrite("Adobe 64Bit print: " & $vFileName & " ") If $DebugOn = 1 Then _WriteErrorLog("Adobe 64Bit print: " & $vFileName) If $DebugOn = 1 Then _WriteErrorLog('"C:\Program Files\Adobe\Acrobat Reader DC\Reader\AcroRd64.exe" /h /p "' & $sSaveDir & $vFileName) Local $iPID = Run('"C:\Program Files\Adobe\Acrobat Reader DC\Reader\AcroRd64.exe" /h /p "' & $sSaveDir & $vFileName & '" ',"",@SW_MINIMIZE) If $DebugOn = 1 Then ConsoleWrite('"C:\Program Files\Adobe\Acrobat Reader DC\Reader\AcroRd64.exe" /h /p "' & $sSaveDir & $vFileName & '"') sleep(6500) ProcessClose($iPID) Else If $DebugOn = 1 Then _WriteErrorLog("Adobe not found, print: " & $vFileName) If $DebugOn = 1 Then ConsoleWrite("Adobe print: " & $vFileName & " ") ShellExecuteWait($sSaveDir & "\" &$vFileName,"","","Print",@SW_MINIMIZE) EndIf EndFunc Func _WriteErrorLog($ErrorMessage) FileWriteLine($sLogDir & "\" & @ScriptName & ".log", @HOUR & ":" & @MIN & ":" & @SEC & ": " & $ErrorMessage) EndFunc ;==>WriteErrorLog Func _Exit() _OL_Close($oOutlook) Exit EndFunc ;==>_Exit _OL_Close($oOutlook) exit