#Region ;**** Directives created by AutoIt3Wrapper_GUI **** #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.14.5 Author: myName Script Function: Print files from outlook mails i at specific folder. #ce ---------------------------------------------------------------------------- ; Script Start - Add your code below here #include #include #include #include #include HotKeySet("+!e", "_Exit") ;Shift-Alt-E to Exit the script ; ***************************************************************************** ; Test contion ; ***************************************************************************** Global $oOutlook = _OL_Open() ; Temp variable $sSaveDir = "C:\Temp\Print Automatically\" $sLogDir = "C:\Temp\" $OutlookFolder = "*\Indbakke\Print Automatically" Global $FileFormats = StringSplit("pdf,docx,doc",",") ; Files to check $iSizeTemp = "00" $iSize = "00" Global $mailsArray[1] = ["0"] ; Array of printet mails after program run _WriteErrorLog("####### Code start #######") _WriteErrorLog("Remove old temp DIR") ; Remove old savefolder DirRemove($sSaveDir, $DIR_REMOVE) ; Check savefolder If FileExists($sSaveDir) Then ConsoleWrite("Save Folder found " & @error & @CRLF) Else ConsoleWrite("Save Folder not found" & @error & @CRLF & "Creating folder.") ; Create the directory. DirCreate($sSaveDir) EndIf ; ####### ; # Test save forlder write ; Create a constant variable in Local scope of the filepath that will be read/written to. _WriteErrorLog("Test write permisions") Local Const $sFilePath = _WinAPI_GetTempFileName($sSaveDir) Local $iFileExists = FileExists($sFilePath) ; Display a message of whether the file exists or not. If $iFileExists Then ConsoleWrite("The file exists." & @CRLF & "FileExist returned: " & $iFileExists & @CRLF) Else MsgBox($MB_SYSTEMMODAL, "", "Cannot create file in '$sSaveDir'. Error: " & @error & @CRLF) EndIf ; Delete the temporary file. FileDelete($sFilePath) ; ####### ; # Start main loop _WriteErrorLog("Start While loop") ConsoleWrite("Start While loop - " & @error & @CRLF) 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 found Local $iSize = DirGetSize($sSaveDir) ; Check temp is empty if $iSize <> $iSizeTemp Then ConsoleWrite("DirGetSize: " & $iSize & @error & @CRLF) $iSizeTemp = $iSize EndIf 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 _WriteErrorLog("### Outlook Items found: " & $aOL_Item[1][1]) ConsoleWrite("Outlook Items found" & $aOL_Item[1][1] & @error & @CRLF) 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) ConsoleWrite("n\Func ExportFiles - " & @error & @CRLF) ExportFiles() ; Run export files Sleep(500) ConsoleWrite("Func SortPrint - " & @error & @CRLF) SortPrint() ; Run Print files sleep(500) ConsoleWrite("Func CleanOpFiles - " & @error & @CRLF) CleanOpFiles() ; Run Cleenop files EndIf EndIf EndIf ; Avoid high CPU usage. Sleep(1000) WEnd Func ExportFiles() _WriteErrorLog("### ExportFiles") ConsoleWrite("Outlook: " & $oOutlook & " Error: " & @error & " Extended: " & @extended & @CRLF) If $aOL_Item[0][0] = 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemSave Script", "Could not find a mail item in folder '" & $OutlookFolder & "'. @error = " & @error & ", @extended = " & @extended) ; 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 _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 Func SortPrint() _WriteErrorLog("### SortPrint") ConsoleWrite("ExportFiles: Start" & @error & @CRLF) Local $FilesArray = _FileListToArray($sSaveDir, "*", 1) _WriteErrorLog("$FilesArray: " & _ArrayToString($FilesArray, ",")) ; ##### ; # See if mail alrady printet. ConsoleWrite("See if mail alrady printet: " & $aOL_Item[1][0] & @CRLF) Local $mailsArrayFind = _ArrayBinarySearch($mailsArray, $aOL_Item[1][0], 1) If not @error Then _WriteErrorLog("Mail Check if allready printet: " & $mailsArrayFind & " -VS-") _WriteErrorLog("Mail Check if allready printet: " & $aOL_Item[1][0]) MsgBox($MB_SYSTEMMODAL, 'Email print', 'Mail Check if allready printet: ' & $mailsArrayFind) Else _WriteErrorLog("Mail Check if allready printet: " & $mailsArrayFind) for $i = 1 To UBound($FilesArray) -1 ConsoleWrite("ExportFiles: loop File array" & @error & @CRLF) Local $sDrive = "", $sDir = "", $sFileName = "", $sExtension = "" Local $aPathSplit = _PathSplit($FilesArray[$i], $sDrive, $sDir, $sFileName, $sExtension) $FileTypeTemp = StringLower($sExtension) $FileTypeTemp = StringTrimLeft ( $FileTypeTemp, 1 ) ConsoleWrite("Array looking for: " & $FileTypeTemp & " " & @error & @CRLF) Local $ret = _ArraySearch($FileFormats, $FileTypeTemp, 0, 0, 0, 0, 1, 2) ConsoleWrite("Array found at: " & $FileTypeTemp & $ret & " " & @error & @CRLF) _WriteErrorLog("Array looking for: " & $FileTypeTemp & " And found: " & $ret) If $ret = -1 Then ConsoleWrite("Deleting item: " & $FilesArray[$i] & " " & @error & @CRLF) _WriteErrorLog("Deleting item: " & $FilesArray[$i]) FileDelete($sSaveDir & $FilesArray[$i]) Elseif $ret <> -1 Then ConsoleWrite("Printing item: " & $FilesArray[$i] & " " & @error & @CRLF) _WriteErrorLog("Printing item: " & $FilesArray[$i]) ; ###### ; Print word files if $FileTypeTemp = "docx" or $FileTypeTemp = "doc" then ; Open the document Local $oWord = _Word_Create(False) Local $oDoc = $oWord.Documents.Open($sSaveDir & $FilesArray[$i], Default, True, false, Default, "", Default, Default, "", Default, False ) If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocPrint Example", "Error opening '" & $FilesArray[$i] & "' " & @CRLF & "@error = " & @error & ", @extended = " & @extended) ; Print the complete document with default values Local $sActivePrinter = $oDoc.Application.ActivePrinter ConsoleWrite("Print word: " & $FilesArray[$i] & " " & @error & @CRLF) _WriteErrorLog("Print word: " & $FilesArray[$i]) _Word_DocPrint($oDoc) If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocPrint Example", "Error printing the document." & @CRLF & "@error = " & @error & ", @extended = " & @extended) ; Closes all documents, the Word application and removes the object reference to it _Word_Quit($oWord) ; ###### ; # Pint pdf i adobe elseif $FileTypeTemp = "pdf" Then 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 ConsoleWrite("Adobe 32Bit print: " & $FilesArray[$i] & " " & @error & @CRLF) _WriteErrorLog("Adobe 32Bit print: " & $FilesArray[$i]) _WriteErrorLog('"C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" /h /p "' & $sSaveDir & $FilesArray[$i]) ConsoleWrite('"C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" /h /p "' & $sSaveDir & $FilesArray[$i] & '" ') Local $iPID = Run('"C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" /h /p "' & $sSaveDir & $FilesArray[$i] & '" ',"",@SW_MINIMIZE) sleep(6500) ProcessClose($iPID) ElseIf FileExists("C:\Program Files\Adobe\Acrobat Reader DC\Reader\AcroRd64.exe") Then ConsoleWrite("Adobe 64Bit print: " & $FilesArray[$i] & " " & @error & @CRLF) _WriteErrorLog("Adobe 64Bit print: " & $FilesArray[$i]) _WriteErrorLog('"C:\Program Files\Adobe\Acrobat Reader DC\Reader\AcroRd64.exe" /h /p "' & $sSaveDir & $FilesArray[$i]) Local $iPID = Run('"C:\Program Files\Adobe\Acrobat Reader DC\Reader\AcroRd64.exe" /h /p "' & $sSaveDir & $FilesArray[$i] & '" ',"",@SW_MINIMIZE) ConsoleWrite('"C:\Program Files\Adobe\Acrobat Reader DC\Reader\AcroRd64.exe" /h /p "' & $sSaveDir & $FilesArray[$i] & '"') sleep(6500) ProcessClose($iPID) Else _WriteErrorLog("Adobe not found, print: " & $FilesArray[$i]) ConsoleWrite("Adobe print: " & $FilesArray[$i] & " " & @error & @CRLF) ShellExecuteWait($sSaveDir & $FilesArray[$i],"","","Print",@SW_MINIMIZE) EndIf ; # print it all if all else fails. Else _WriteErrorLog("Print all else: " & $FilesArray[$i]) ConsoleWrite("Print all else: " & $FilesArray[$i] & " " & @error & @CRLF) ShellExecuteWait($sSaveDir & $FilesArray[$i],"","","Print",@SW_MINIMIZE) EndIf EndIf Next ConsoleWrite("Add mailID to Array: " & $aOL_Item[1][0] & @CRLF) _WriteErrorLog("Add mailID to Array: " & $aOL_Item[1][0]) _ArrayAdd($mailsArray, $aOL_Item[1][0]) EndIf EndFunc Func CleanOpFiles() _WriteErrorLog(" ### CleanOpFiles") ConsoleWrite("Starting Func CleanOpFiles" & @error & @CRLF) Sleep(1000) if $aOL_Item[0][0] <> 0 Then _WriteErrorLog("Deleting mail: " & $aOL_Item[1][0]) _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) ConsoleWrite("Deleting item: " & $aOL_Item[1][0] & " " & @error & @CRLF) Else _WriteErrorLog("No email to delete") ConsoleWrite("No email to delete." & @CRLF) EndIf $FilesArray = _FileListToArray ( $sSaveDir, "*", 1) _WriteErrorLog("Delete all files in folder: " & _ArrayToString($FilesArray, ",")) for $i = 1 To UBound($FilesArray) -1 FileDelete($sSaveDir & $FilesArray[$i]) Next 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