Jump to content

Outlook, SavingAttachments


Recommended Posts

Hi Guys

Im still a begginer with autoit and using the programe about 5 months to help me with my work

I have created gui's and exe's to monitor folders and return values, and run scripts.

Im a bit stuck as my boss created a VS program That has crashed and i know nothing about VS / .net

lookng for some quick help as these emails are taking up alot of time, perday(800 emails) of which 80 % have attachments that we need to process

What I'm Trying to achive

I need to look at a mail box (Inbox) and search for any that have attachments and then save these attachments to a folder

and then move these to a 2 folder for processing,

as well as moving the email to a folder location in outlook (inbox - processed)

I have tryed to play with the outlook udf but its confusing me.

wondered if any one is able to help me write this..

Link to comment
Share on other sites

Which Outlook UDF did you try? The one written by wooltown () or my version ()?

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

so far i have this But is not saving the file/ attachment to the location

reading my mail box and then bring back the amount of unreads mails i have ( not looking at Email with attachments )

EDIT - I have now been able to save the emal to a file location , But im unable to get the path of the attachment in the email

#AutoIt3Wrapper_AU3Check_Parameters= -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6

#AutoIt3Wrapper_AU3Check_Stop_OnWarning=Y

#include <OutlookEX.au3>

Global $aItems,$file,$file1,$output

Global $oOutlook = _OL_Open()

Global $aOL_Item = _OL_ItemFind($oOutlook, "*Inbox",$olmail, "[unread]=True", "", "", "EntryID")

If $aOL_Item[0][0] = 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemAttachmentSave Example Script", "Could not find a mail item in folder 'inbox folder'. @error = " & @error & ", @extended = " & @extended)

_ArrayDisplay($aol_item,"List of items") ;;;; displays items unread in my Inbox

$file1 = _OL_ItemAttachmentGet($oOutlook,$aol_Item[1][0]) ;;; item id number ( 1,1)

_ArrayDisplay($file1)

$output = $file1[1][1] ;;; name of the file

;~ $outputfile = ??? [1][3]

$file = _OL_ItemAttachmentSave($oOutlook, $output, Default, 1,"S:Development ShareCraigNewGuitest"& $output)

If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF", "Error saving mail items @error =" & @error & ", @extended = " & @extended)

;~ ;;;;; ##### Saves file of email to location

Edited by GNGNUT
Link to comment
Share on other sites

This (test) script will give you an array with all unread items in then inbox with at least one attachment:

#AutoIt3Wrapper_AU3Check_Parameters= -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#AutoIt3Wrapper_AU3Check_Stop_OnWarning=Y
#include <OutlookEX.au3>
Global $oOutlook = _OL_Open()
If @error Then Exit MsgBox(16, "OutlookEX UDF - _OL_ItemSearch Example Script", "Error running _OL_Open. @error = " & @error & ", @extended = " & @extended)
; *****************************************************************************
; Search the inbox for unread mails with at least one attachment
; Returns: EntryID
; *****************************************************************************
; Access the default mail folder
$aFolder = _OL_FolderAccess($oOutlook, "", $olFolderInbox)
If @error Then Exit MsgBox(16, "OutlookEX UDF - _OL_ItemSearch Example Script", "Error running _OL_FolderAccess. @error = " & @error & ", @extended = " & @extended)
$sFilter = "@SQL=(""urn:schemas:httpmail:hasattachment"" = 1 AND ""urn:schemas:httpmail:read"" = 0)"
$aResult = _OL_ItemSearch($oOutlook, $aFolder[1], $sFilter, "EntryID")
If @error Then
    MsgBox(16, "OutlookEX UDF - _OL_ItemSearch Example Script", "Error running _OL_ItemSearch. @error = " & @error & ", @extended = " & @extended)
Else
    _Arraydisplay($aresult, "Example 6")
EndIf
_OL_Close($oOutlook)
Exit

; #FUNCTION# ====================================================================================================================
; Name ..........: _OL_ItemSearch
; Description ...: Find items (extended search) using a DASL query returning an array of all specified properties.
; Syntax.........: _OL_ItemSearch($oOL, $vOL_Folder, $avOL_Search, $sOL_ReturnProperties[, $sOL_Sort = ""[, $iOL_Flags = 0]])
; Parameters ....: $oOL               - Outlook object returned by a preceding call to _OL_Open()
;                 $vOL_Folder          - Folder object as returned by _OL_FolderAccess or full name of folder where the search will be started.
;                 +If you want to search a default folder you have to specify the folder object.
;                 $avOL_Search        - Can bei either a string containing the full DASL query or a one based two-dimensional array with unlimited number of rows containing the elements to build the DASL query:
;                 |0: Property to query. This can be either the hex value or the name of the property. The function transaltes the name to the hex value. Unknown names set @error
;                 |1: Type of comparison operator: 1 = "=", 2 = "ci_startswith", 3 = "ci_phrasematch", 3 = "like"
;                 |2: Value to search for
;                 |3: Operator to concatenate the next comparison. Has to be "and", "or", "or not" or "and not"
; For details please see Remarks
;                 $sOL_ReturnProperties - Comma separated list of properties to return. Can be the property name (e.g. "subject") or the MAPI proptag (e.g. "http://schemas.microsoft.com/mapi/proptag/0x10F4000B")
;                 $sOL_Sort          - Optional: Property to sort the result on plus optional flag to sort descending (default = None). E.g. "[Subject], True" sorts the result descending on the subject
;                 $iOL_Flags            - Optional: Flags to set different processing options. Can be a combination of the following:
;                 |1: Subfolders will be included
;                 |2: Row 1 contains column headings. Therefore the number of rows/columns in the table has to be calculated using UBound
;                 |4: Just return the number of records. You don't get an array, just a single integer denoting the total number of records found
; Return values .: Success - One based two-dimensional array with the properties specified by $sOL_ReturnProperties
;                 Failure - Returns "" and sets @error:
;                 |1  - $oOL is not an object
;                 |2  - Error accessing the specified folder. See @extended for errorcode returned by _OL_FolderAccess
;                 |3  - $sOL_ReturnProperties is empty
;                 |5  - $avOL_Search is an array but not a two dimensional array or the first row doesn't contain the numbers of rows and columns
;                 |6  - Specified search property could not be translated to a hex code. @extended is set to the row in $avOL_Search
;                 |7  - Specified search operator is not an integer or < 1 or > 3. @extended is set to the row in $avOL_Search
;                 |8  - Specified search value is empty. @extended is set to the row in $avOL_Search
;                 |9  - Invalid search operator. Must be "and" or "or". @extended is set to the row in $avOL_Search
;                 |10 - The last entry in the search array has a search operator
;                 |11 - The entry in the search array has no operator but more search arguments follow
;                 |12 - Error executing the search operation. @extended is set to the error returned by method GetTable
;                 |13 - No records returned by the search operation
;                 |14 - Error adding $sOL_ReturnProperties to the result set. @extended is the number of the property in error
;                 |15 - Error executing the search operation. @extended is set to the error returned by method GetTable
; Author ........: water
; Modified ......:
; Remarks .......: DASL syntax: "Searching Outlook Data" - http://msdn.microsoft.com/en-us/library/cc513841.aspx"
;                 List of MAPI proptags:                - http://www.dimastr.com/redemption/enum_MAPITags.htm
; Related .......:
; Link ..........:
; Example .......: Yes
; ===============================================================================================================================
Func _OL_ItemSearch($oOL, $vOL_Folder, $avOL_Search, $sOL_ReturnProperties);, $sOL_Sort = "", $iOL_Flags = 0)

    Local $asOL_Operator[5] = [4, "=", "ci_startswith", "ci_phrasematch", "like"]
    Local $sOL_Filter = '@SQL=', $aOL_Temp, $sOL_Property, $iOL_Rows, $iOL_Cols, $oOL_Row
    If Not IsObj($oOL) Then Return SetError(1, 0, "")
    If Not IsObj($vOL_Folder) Then
        $aOL_Temp = _OL_FolderAccess($oOL, $vOL_Folder)
        If @error Then Return SetError(2, @error, "")
        $vOL_Folder = $aOL_Temp[1]
    EndIf
    If StringStripWs($sOL_ReturnProperties, 7) = "" Then Return SetError(3, 0, "")
    Local $aOL_ReturnProperties = StringSplit(StringStripWS($sOL_ReturnProperties, 8), ",")
    ; Build search string
    If IsArray($avOL_Search) Then
        If UBound($avOL_Search, 0) <> 2 Or Not IsInt($avOL_Search[0][0]) Or Not IsInt($avOL_Search[0][1]) Then Return SetError(5, 0, "")
        Local Const $sPropTag = "http://schemas.microsoft.com/mapi/proptag/"
        For $iOL_Index = 1 To $avOL_Search[0][0]
            If IsInt($avOL_Search[$iOL_Index][0]) Then
                $sOL_Property = "0x" & Hex(Int($avOL_Search[$iOL_Index][0]))
            Else
                $sOL_Property = __OL_Property2Hex($avOL_Search[$iOL_Index][0])
                If @error Then Return SetError(6, $iOL_Index, "")
            Endif
            If Not IsInt($avOL_Search[$iOL_Index][1]) Or $avOL_Search[$iOL_Index][1] < 1 Or $avOL_Search[$iOL_Index][1] > 3 Then Return SetError(7, $iOL_Index, "")
            $avOL_Search[$iOL_Index][2] = StringStripWS($avOL_Search[$iOL_Index][2], 7)
            If $avOL_Search[$iOL_Index][2] = "" Then Return SetError(8, $iOL_Index, "")
            $avOL_Search[$iOL_Index][3] = StringStripWS($avOL_Search[$iOL_Index][3], 7)
            If $avOL_Search[$iOL_Index][3] <> "" Then
                If $avOL_Search[$iOL_Index][3] <> "and" And $avOL_Search[$iOL_Index][3] <> "or" And _
                    $avOL_Search[$iOL_Index][3] <> "and not" And $avOL_Search[$iOL_Index][3] <> "or not" _
                    Then Return SetError(9, $iOL_Index, "")
                If $iOL_Index = $avOL_Search[0][0] Then Return SetError(10, $iOL_Index, "")
            Else
                If $iOL_Index < $avOL_Search[0][0] Then Return SetError(11, $iOL_Index, "")
            EndIf
            $sOL_Filter = $sOL_Filter & '"' & $sPropTag & $sOL_Property & '" ' & $asOL_Operator[$avOL_Search[$iOL_Index][1]] & " '" & $avOL_Search[$iOL_Index][2] & "'"
            If $avOL_Search[$iOL_Index][3] <> "" Then $sOL_Filter = $sOL_Filter & " " & $avOL_Search[$iOL_Index][3] & " "
        Next
    Else
        $sOL_Filter = $avOL_Search
    EndIf
    ; execute the search
    Local $oOL_Table = $vOL_Folder.GetTable($sOL_Filter)
    If @error Or Not IsObj($oOL_Table) Then Return SetError(12, @error, "")
    If $oOL_Table.GetRowCount = 0 Then Return SetError(13, 0, "")
    ; http://msdn.microsoft.com/en-us/library/bb176396%28v=office.12%29.aspx
    ; Remove all columns in the default column set
    $oOL_Table.Columns.RemoveAll
    ; Specify desired properties
    For $iOL_Index = 1 To $aOL_ReturnProperties[0]
        $oOL_Table.Columns.Add($aOL_ReturnProperties[$iOL_Index])
        If @error Then Return SetError(14, $iOL_Index, "")
    Next
    ; Create and fill the result table
    $iOL_Rows = $oOL_Table.GetRowCount + 1
    If @error Then Return SetError(15, @error, "")
    $iOL_Cols = $aOL_ReturnProperties[0]
    Local $avOL_Result[$iOL_Rows][$iOL_Cols] = [[$iOL_Rows - 1]]
    If UBound($avOL_Result, 2) > 1 Then $avOL_Result[0][1] = $iOL_Cols
    Local $iOL_Index2 = 1
    While Not $oOL_Table.EndOfTable
        $oOL_Row = $oOL_Table.GetNextRow
        For $iOL_Index = 1 To $aOL_ReturnProperties[0]
            $avOL_Result[$iOL_Index2][$iOL_Index-1] = $oOL_Row($aOL_ReturnProperties[$iOL_Index])
        Next
        $iOL_Index2 = $iOL_Index2 + 1
    WEnd
    Return $avOL_Result

EndFunc   ;==>_OL_ItemSearch

Func __OL_Property2Hex($sOL_Property)

    Local $aProperties[100][2] = [[2, 2],["subject", "0x0037001E"],["SenderName","0x0C1A001F"]]
    For $i = 1 To $aProperties[0][0]
        If $aProperties[$i][0] = $sOL_Property Then Return $aProperties[$i][1]
    Next
    Return SetError(1, 0, "")

EndFunc   ;==>__OL_Property2Hex
plus you have to replace function _OL_ItemAttachmentGet in the UDF with this code:

Func _OL_ItemAttachmentGet($oOL, $vOL_Item, $sOL_StoreID = Default)

    If Not IsObj($vOL_Item) Then
        If StringStripWS($vOL_Item, 3) = "" Then Return SetError(1, 0, 0)
        $vOL_Item = $oOL.Session.GetItemFromID($vOL_Item, $sOL_StoreID)
    EndIf
    If @error Then Return SetError(2, @error, 0)
    If $vOL_Item.Attachments.Count = 0 Then Return SetError(3, 0, 0)
    Local $aOL_Attachments[$vOL_Item.Attachments.Count + 1][7] = [[$vOL_Item.Attachments.Count, 7]]
    Local $iOL_Index = 1
    For $oOL_Attachment In $vOL_Item.Attachments
        $aOL_Attachments[$iOL_Index][0] = $oOL_Attachment
        $aOL_Attachments[$iOL_Index][1] = $oOL_Attachment.DisplayName
        $aOL_Attachments[$iOL_Index][2] = $oOL_Attachment.FileName
        $aOL_Attachments[$iOL_Index][3] = $oOL_Attachment.PathName
        $aOL_Attachments[$iOL_Index][4] = $oOL_Attachment.Position
        $aOL_Attachments[$iOL_Index][5] = $oOL_Attachment.Size
        $aOL_Attachments[$iOL_Index][6] = $oOL_Attachment.Type
        $iOL_Index += 1
    Next
    Return $aOL_Attachments

EndFunc   ;==>_OL_ItemAttachmentGet

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

my final script looks like this

And what this does it looks at the useres inboxfolder reads any unread emails and then works out if any of them are in array (attachments, pictures, Ect) the saves the files and moves the email to a different folder

Again Big Thanks Water made my life a bit easyier now ;)

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Include <File.au3>
#Include <Array.au3>
#include <Date.au3>
#include <OutLookEX.au3>
#include <OutlookExConstants.au3>
#AutoIt3Wrapper_AU3Check_Parameters=
HotKeySet("{ESC}", "Terminate")
Func Terminate()
Exit 0
EndFunc   ;==>Terminate

Global $aItems, $Result
Global $vOlFolder = "*Inbox"
Global $oOutlook = _OL_Open()

$aItems = _OL_ItemFind($oOutlook, $vOlFolder, $olMail, "[UnRead]=True", "", "", "EntryID", "", 1)
If IsArray($aItems) Then
For $i = 1 To $aItems[0][0]
$Result = _OL_ItemAttachmentGet($oOutlook, $aItems[$i][0], Default)
If IsArray($Result) Then
  For $r = 1 To $Result[0][0]
   _OL_ItemAttachmentSave($oOutlook, $aItems[$i][0], Default, 1, "c:temp" &$Result[$r][1])  ;=> location on were to save the   Sleep(500)
   _OL_ItemMove($oOutlook, $aItems[$i][0], Default, "*InboxProcessed")   ;=> loctaion on were its moved the file to once done
  Next
Else
;~   Msgbox (0,"No attachments", "Functio to move Email goes here")
EndIf
Next
Else
    MsgBox(48, "OutlookEX UDF: _OL_ItemFind Example Script", "Could not find an unread mail. @error = " & @error & ", @extended: " & @extended)
EndIf
Big Thank you for your help and the update to The outlookex Edited by GNGNUT
Link to comment
Share on other sites

Glad you got it working ;)

May I suggest that you do some error checking after _OL_ItemAttachmentGet, _OL_ItemAttachmentSave and _OL_ItemMove?

Something like

If @error Then MsgBox(...)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...