0

Problem:

Outlook:Cannot move read e-mail to Archive folder.

Tried several options I found on the web but all to no avail... Anyone? :D

I already removed the .move property as it was not working, so please don't fuss about that.

Any help will be much appreciated!

Code:

start Outlook
Function Get-OutlookInBox 
    { 
    Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null 
    $olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]  
    $outlook = new-object -comobject outlook.application 

        $namespace = $outlook.GetNameSpace("MAPI") 
        $folder = $namespace.getDefaultFolder($olFolders::olFolderInBox) 
            $folder.items |  
            Select -Property Subject, ReceivedTime, Importance, SenderName, body 
    } #end function Get-OutlookInbox
    ###################################################################################

    cls

try {
    $switches = get-outlookinbox | where subject -eq "Ip was blocked"
        $e = $switches.body
    $e = $e -replace 'Blocked IP:| for agent| |:\d{1,2}'
                    }
    catch {
          $switches = "Fail"
          }

    $f = $e |select -Unique |sort


    ni $File -type file
        $f | ac $File
            (gc $File) | ? {$_.trim() -ne "" } | sc $File
            $IPCount =  (gc $File)
            $IPCount =  $IPCount.count

    $index=0;  

    #Mark mails as read and delete.
    function display( [string]$subject, [string]$color , [string]$out)  {

    # REQUIRED LENGTH OF STRING
    $len = 20

    # STRINGS THAT ARE LONGER WILL BE CUT DOWN,
    # STRINGS THAT ARE TO SHORT WILL BE MADE LONGER
    if ( $subject.length -lt 20 ){
        $toadd=20-$subject.length;
        for ( $i=0; $i -lt $toadd; $i++ ){
            $subject=$subject+" ";
        }
        $len = $subject.length
    }
    else { $len = 20 }

    $index=$index+1
    Write -ForegroundColor $color -nonewline " |" ((($subject).ToString()).Substring(0,$len)).ToUpper()
}
$outlook = new-object -comobject outlook.application

#Define folders
$namespace = $outlook.GetNameSpace("MAPI")
$pst = $namespace.Stores
$pstRoot = $pst.GetRootFolder()
$pstFolders = $pstRoot.Folders
$Archive = $namespace.Folders | ?{$_.name -match "Archive"}
$DefaultFolder = $namespace.GetDefaultFolder(6)
$InboxFolders = $DefaultFolder.Folders
$DeletedItems = $namespace.GetDefaultFolder(3)
$Emails = $DefaultFolder.Items

#For($i=($emails.count-1);$i -ge 0;$i--){
 #   $($emails)[$i].Unread = $false
  #  $($emails)[$i].delete()
#}
For($i=($emails.count-1);$i -ge 0;$i--){
$($emails)[$i].Unread = $false
$($emails)[$i].move($Archive.Folders.Item("Inbox<Archive>"))

}

Just a user
  • 609
  • 2
  • 10
  • 19

1 Answers1

1

That's a different way to get the folders than I'm used to using. This should do it for you though:

$Archive = $namespace.Folders | ?{$_.name -match "Archive"}

And then in your For loop if you want to move it to your Inbox in the Archives you can do this:

For($i=($emails.count-1);$i -ge 0;$i--){
    $($emails)[$i].Unread = $false
    $($emails)[$i].move($Archive.Folders.Item("Inbox"))
}

If you want a subfolder you add more .Folders.Item("<Subfolder Name>) on after ("Inbox")

TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56
  • Its not working unfortuneatly... Adjusted my code in Question – Just a user Aug 22 '14 at 16:45
  • Maybe I wasn't clear in my last line. If your archive has a Folder named Inbox (most do), and a folder within that named August which you wanted to move the mail to you would do `$($emails)[$i].move($Archive.Folders.Item("Inbox").Folders.Item("August"))` – TheMadTechnician Aug 22 '14 at 16:52
  • Tried that as well, got ur awnser now but still no avail. It does mark them as read but doesnt move. Made an archive folder with both a subfolder named august and inbox (just to be sure) But also $Archive Does not resolve anything. – Just a user Aug 22 '14 at 17:21
  • Solved it by changing $Namespace to $Defaultfolder Thanks a bunch! – Just a user Aug 22 '14 at 17:27