Sign in to follow this  
Followers 0
spudw2k

Outlook Folder Enumeration

2 posts in this topic

#1 ·  Posted (edited)

Pretty Self explanatory. Creates a text file with folder counts.

edit: Updated count script. Optimized count method (didn't realize there was a property for this already...doh)

#include <GuiTreeView.au3>
#include <Array.au3>
#include <GUIConstants.au3>

$objOutlook = ObjCreate("Outlook.Application")
$objNamespace= $objOutlook.GetNamespace("MAPI")
$objFolderInbox = $objNamespace.Folders

Dim $arrFolders[1]
For $folder In $objFolderInbox
    If Not StringInstr($folder.FolderPath,"Public Folders") Then
        If Ubound($arrFolders)=1 Then
            $arrFolders[0]=$folder.FolderPath
            ReDim $arrFolders[UBound($arrFolders)+1]
        Else
            $arrFolders[UBound($arrFolders)-1]=$folder.FolderPath
            ReDim $arrFolders[UBound($arrFolders)+1]
        EndIf
    EndIf
Next
ReDim $arrFolders[UBound($arrFolders)-1]
$arrFldrs = SelectionBox($arrFolders)

If Not IsArray($arrFldrs) Then Exit
$file = FileSaveDialog("Save as",@ScriptDir,"Text File (*.TXT)",18,"emailcount.txt")
If @error Then Exit
If StringLeft(StringRight($file,4),1) <> "." Then $file &= ".TXT"
If FileExists($file) Then FileOpen($file,2)

For $fldr In $arrFldrs
    $fldr = StringReplace($fldr,"\\","")
    $folder = $objNamespace.Folders.Item($fldr)
    EnumerateFolder($folder)
Next

Msgbox(0,"Email Count Complete","",3)

Func EnumerateFolder($objFolder)
    $dat = ""
    $dat &= Chr(34) & $objFolder.FolderPath & Chr(34) & ","
    $dat &= Chr(34) & $objFolder.Items.Count & " items" & Chr(34)
    FileWriteLine($file,$dat)
    For $folder In $objFolder.Folders
        EnumerateFolder($folder)
    Next
EndFunc

Func SelectionBox($arrFolders)
    Dim $tTreeArr[1]
    $tGui = GUICreate("Select Folder(s)",250,326,-1,-1,"","")
    $tTree = GUICtrlCreateTreeView(5,5,235,258,BitOr(256,55))
    $tSelectAll = GUICtrlCreateButton("Select All",85,270,75,20)
    $tDeSelectAll = GUICtrlCreateButton("Deselect All",165,270,75,20)
    $tOKBtn = GUICtrlCreateButton("OK",5,270,75,20)
        GUICtrlSetState(-1,$GUI_FOCUS)

    For $i = 0 to UBound($arrFolders) - 1
        $tTreeArr[$i] = GUICtrlCreateTreeViewItem($arrFolders[$i],$tTree)
        ReDim $tTreeArr[$i + 2]
    Next

    $tVarTreeCount = _GUICtrlTreeView_GetCount($tTree)
    GUISetState(@SW_SHOW,$tGui)

    While 1

        $msg = GUIGetMsg()

        If $msg = $tSelectAll Then
            For $i = 0 to $tVarTreeCount - 1
                GUICtrlSetState($tTreeArr[$i],1)
            Next
        EndIf

        If $msg = $tDeSelectAll Then
            For $i = 0 to $tVarTreeCount - 1
                GUICtrlSetState($tTreeArr[$i],4)
            Next
        EndIf

        If $msg = $tOKBtn Then
            Local $tStrSelectedHosts
            For $i = 0 to $tVarTreeCount - 1
                If BitAnd(GUICtrlRead($tTreeArr[$i]),1) Then
                    $tStrSelectedHosts = $tStrSelectedHosts & GUICtrlRead($tTreeArr[$i],1) & "|"
                EndIf
            Next
            GUIDelete($tGui)
            $tArrSelectedHosts = StringSplit($tStrSelectedHosts,"|")
            _ArrayDelete($tArrSelectedHosts,0)
            _ArrayDelete($tArrSelectedHosts,UBound($tArrSelectedHosts)-1)
            Return $tArrSelectedHosts
        EndIf

    WEnd
EndFunc
Edited by spudw2k

Share this post


Link to post
Share on other sites



Here's another Outlook tool. This one collects emails (Path,Subject,Received) in the Inbox.

#region - Main
;Global Variables
Global Const $__TREEVIEWCONSTANT_ClassName = "SysTreeView32"
Global Const $TV_FIRST = 0x1100
Global Const $TVM_GETCOUNT = $TV_FIRST + 5
Global $Debug_TV = False
Global $debug = 0

;COM Object Initialize
$oMyError = ObjEvent("AutoIt.Error","MyErrFunc")
$objOutlook = ObjCreate("Outlook.Application")
$objNamespace= $objOutlook.GetNamespace("MAPI")
$objFolders = $objNamespace.Folders

;Query Accessible Mailboxes and Retrive / Format Data
Dim $arrFolders[1]
For $folder In $objFolders
    If Not StringInstr($folder.FolderPath,"Public Folders") Then
        If Ubound($arrFolders)=1 Then
            $arrFolders[0]=$folder.FolderPath
            ReDim $arrFolders[UBound($arrFolders)+1]
        Else
            $arrFolders[UBound($arrFolders)-1]=$folder.FolderPath
            ReDim $arrFolders[UBound($arrFolders)+1]
        EndIf
    EndIf
Next
ReDim $arrFolders[UBound($arrFolders)-1]
;Mailbox Seleciton Prompt
$arrFldrs = SelectionBox($arrFolders)

;Save As Dialog for Report Output
If Not IsArray($arrFldrs) Then Exit
$file = FileSaveDialog("Save as",@ScriptDir,"Comma Separated Values (*.CSV)",18,"emaildump.csv")
If @error Then Exit
;Prepare CSV File
If StringLeft(StringRight($file,4),1) <> "." Then $file &= ".TXT"
If FileExists($file) Then FileOpen($file,2)
FileWriteLine($file,Chr(34) & "Path" & chr(34) & "," & Chr(34) & "Subject" & CHR(34) & "," & Chr(34) & "Date Received" & CHr(34))

;Traverse Mailbox for Inbox Folders
For $fldr In $arrFldrs
    $fldr = StringReplace($fldr,"\\","")
    $folder = $objFolders.Item($fldr)
    EnumerateFolder($folder)
Next

;Process Complete
Msgbox(0,"Inbox Dump Complete","",3)
#endregion
#region - Functions
Func EnumerateFolder($objFolder,$indent=0)  ;Folder Enumeration Function
    If StringInStr($objFolder.FolderPath,"Inbox") Then
        $path = Chr(34) & $objFolder.FolderPath & Chr(34) & ","
        For $item In $objFolder.Items
            $dat = ""
            $dat = $path
            $dat &= Chr(34) & StringReplace($item.Subject,'"',"'") & Chr(34) & ","
            $date = $item.ReceivedTime
            If $date Then $date = StringRegExpReplace($date,"(\d{4,4})(\d{2,2})(\d{2,2})(\d{2,2})(\d{2,2})(\d{2,2})",'$1/$2/$3 $4:$5:$6')
            If $date Then $dat &= Chr(34) & $date & Chr(34)
            If $dat Then FileWriteLine($file,$dat)
        Next
    EndIf
    For $folder In $objFolder.Folders
        EnumerateFolder($folder,$indent+1)
    Next
EndFunc

Func SelectionBox($arrFolders)  ;Selection Box Prompt
    Dim $tTreeArr[1]
    $tGui = GUICreate("Select Folder(s)",250,326,-1,-1,"","")
    $tTree = GUICtrlCreateTreeView(5,5,235,258,BitOr(256,55))
    $tSelectAll = GUICtrlCreateButton("Select All",85,270,75,20)
    $tDeSelectAll = GUICtrlCreateButton("Deselect All",165,270,75,20)
    $tOKBtn = GUICtrlCreateButton("OK",5,270,75,20)
        GUICtrlSetState(-1,256)

    For $i = 0 to UBound($arrFolders) - 1
        $tTreeArr[$i] = GUICtrlCreateTreeViewItem($arrFolders[$i],$tTree)
        ReDim $tTreeArr[$i + 2]
    Next

    $tVarTreeCount = _GUICtrlTreeView_GetCount($tTree)
    GUISetState(@SW_SHOW,$tGui)

    While 1

        $msg = GUIGetMsg()

        If $msg = $tSelectAll Then
            For $i = 0 to $tVarTreeCount - 1
                GUICtrlSetState($tTreeArr[$i],1)
            Next
        EndIf

        If $msg = $tDeSelectAll Then
            For $i = 0 to $tVarTreeCount - 1
                GUICtrlSetState($tTreeArr[$i],4)
            Next
        EndIf

        If $msg = $tOKBtn Then
            Local $tStrSelected
            For $i = 0 to $tVarTreeCount - 1
                If BitAnd(GUICtrlRead($tTreeArr[$i]),1) Then
                    $tStrSelected = $tStrSelected & GUICtrlRead($tTreeArr[$i],1) & "|"
                EndIf
            Next
            GUIDelete($tGui)
            $tArrSelected = StringSplit($tStrSelected,"|")
            _ArrayDelete($tArrSelected,0)
            _ArrayDelete($tArrSelected,UBound($tArrSelected)-1)
            Return $tArrSelected
        EndIf

    WEnd
EndFunc

Func MyErrFunc()  ;Generic COM Error Handler
  If Not $debug Then Return 0
  FileWrite(@ScriptDir & "\" & @ScriptName & ".dbg.txt","AutoItCOM intercepted a COM Error !"      & @CRLF  & @CRLF & _
             "err.description is: "    & @TAB & $oMyError.description    & @CRLF & _
             "err.windescription:"     & @TAB & $oMyError.windescription & @CRLF & _
             "err.number is: "         & @TAB & hex($oMyError.number,8)  & @CRLF & _
             "err.lastdllerror is: "   & @TAB & $oMyError.lastdllerror   & @CRLF & _
             "err.scriptline is: "     & @TAB & $oMyError.scriptline     & @CRLF & _
             "err.source is: "         & @TAB & $oMyError.source         & @CRLF & _
             "err.helpfile is: "       & @TAB & $oMyError.helpfile       & @CRLF & _
             "err.helpcontext is: "    & @TAB & $oMyError.helpcontext    & @CRLF)
Endfunc
#endregion

#region - User Defined Functions
Func _ArrayDelete(ByRef $avArray, $iElement)
    If Not IsArray($avArray) Then Return SetError(1, 0, 0)

    Local $iUBound = UBound($avArray, 1) - 1

    If Not $iUBound Then
        $avArray = ""
        Return 0
    EndIf

    ; Bounds checking
    If $iElement < 0 Then $iElement = 0
    If $iElement > $iUBound Then $iElement = $iUBound

    ; Move items after $iElement up by 1
    Switch UBound($avArray, 0)
        Case 1
            For $i = $iElement To $iUBound - 1
                $avArray[$i] = $avArray[$i + 1]
            Next
            ReDim $avArray[$iUBound]
        Case 2
            Local $iSubMax = UBound($avArray, 2) - 1
            For $i = $iElement To $iUBound - 1
                For $j = 0 To $iSubMax
                    $avArray[$i][$j] = $avArray[$i + 1][$j]
                Next
            Next
            ReDim $avArray[$iUBound][$iSubMax + 1]
        Case Else
            Return SetError(3, 0, 0)
    EndSwitch

    Return $iUBound
EndFunc   ;==>_ArrayDelete

Func _GUICtrlTreeView_GetCount($hWnd)
    If $Debug_TV Then __UDF_ValidateClassName($hWnd, $__TREEVIEWCONSTANT_ClassName)

    If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)

    Return _SendMessage($hWnd, $TVM_GETCOUNT)
EndFunc   ;==>_GUICtrlTreeView_GetCount

Func _SendMessage($hWnd, $iMsg, $wParam = 0, $lParam = 0, $iReturn = 0, $wParamType = "wparam", $lParamType = "lparam", $sReturnType = "lresult")
    Local $aResult = DllCall("user32.dll", $sReturnType, "SendMessageW", "hwnd", $hWnd, "uint", $iMsg, $wParamType, $wParam, $lParamType, $lParam)
    If @error Then Return SetError(@error, @extended, "")
    If $iReturn >= 0 And $iReturn <= 4 Then Return $aResult[$iReturn]
    Return $aResult
EndFunc   ;==>_SendMessage

Func __UDF_ValidateClassName($hWnd, $sClassNames)
    __UDF_DebugPrint("This is for debugging only, set the debug variable to false before submitting")
    If _WinAPI_IsClassName($hWnd, $sClassNames) Then Return True
    Local $sSeparator = Opt("GUIDataSeparatorChar")
    $sClassNames = StringReplace($sClassNames, $sSeparator, ",")

    __UDF_DebugPrint("Invalid Class Type(s):" & @LF & @TAB & "Expecting Type(s): " & $sClassNames & @LF & @TAB & "Received Type : " & _WinAPI_GetClassName($hWnd))
    Exit
EndFunc   ;==>__UDF_ValidateClassName

Func _WinAPI_IsClassName($hWnd, $sClassName)
    Local $sSeparator = Opt("GUIDataSeparatorChar")
    Local $aClassName = StringSplit($sClassName, $sSeparator)
    If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)
    Local $sClassCheck = _WinAPI_GetClassName($hWnd) ; ClassName from Handle
    ; check array of ClassNames against ClassName Returned
    For $x = 1 To UBound($aClassName) - 1
        If StringUpper(StringMid($sClassCheck, 1, StringLen($aClassName[$x]))) = StringUpper($aClassName[$x]) Then Return True
    Next
    Return False
EndFunc   ;==>_WinAPI_IsClassName

Func _WinAPI_GetClassName($hWnd)
    If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)
    Local $aResult = DllCall("user32.dll", "int", "GetClassNameW", "hwnd", $hWnd, "wstr", "", "int", 4096)
    If @error Then Return SetError(@error, @extended, False)
    Return SetExtended($aResult[0], $aResult[2])
EndFunc   ;==>_WinAPI_GetClassName

Func __UDF_DebugPrint($sText, $iLine = @ScriptLineNumber, $err=@error, $ext=@extended)
    ConsoleWrite( _
            "!===========================================================" & @CRLF & _
            "+======================================================" & @CRLF & _
            "-->Line(" & StringFormat("%04d", $iLine) & "):" & @TAB & $sText & @CRLF & _
            "+======================================================" & @CRLF)
    Return SetError($err, $ext, 1)
EndFunc   ;==>__UDF_DebugPrint
#endregion

Share this post


Link to post
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
Sign in to follow this  
Followers 0