
Move Selected Mail Items to Public Folder
Thanks Sue. Based on that info, I tried the following with no success.
Where is the public function supposed to go?
Sub JunkMail()
'Set myFolder1 = Session.Folders("Public Folders")
'Set myFolder2 = myFolder1.Folders("All Public Folders")
'Set myFolder3 = myFolder2.Folders("Junk Mail")
Set targetFolder = GetFolder("Public Folders/All Public Folders/Junk
Mail")
Set objOL = CreateObject("Outlook.Application")
Set oSelection = objOL.ActiveExplorer.Selection
intCount = oSelection.Count
For I = intCount To 1 Step -1
Set objItem = oSelection.Items(I)
objItem.Move targetFolder
Next
End Sub
Public Function GetFolder(strFolderPath As String) As MAPIFolder
' folder path needs to be something like
' "Public Folders/All Public Folders/Company/Sales"
Dim objApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim colFolders As Outlook.Folders
Dim objFolder As Outlook.MAPIFolder
Dim arrFolders() As String
Dim I As Long
On Error Resume Next
strFolderPath = Replace(strFolderPath, "/", "\")
arrFolders() = Split(strFolderPath, "\")
Set objApp = CreateObject("Outlook.Application")
Set objNS = objApp.GetNamespace("MAPI")
Set objFolder = objNS.Folders.Item(arrFolders(0))
If Not objFolder Is Nothing Then
For I = 1 To UBound(arrFolders)
Set colFolders = objFolder.Folders
Set objFolder = Nothing
Set objFolder = colFolders.Item(arrFolders(I))
If objFolder Is Nothing Then
Exit For
End If
Next
End If
Set GetFolder = objFolder
Set colFolders = Nothing
Set objNS = Nothing
Set objApp = Nothing
End Function
Several key issues:
1) Selection is a child object of the ActiveExplorer object representing the
currently visible folder. To work with the selection, you need to set an
object:
Set objOL = CreateObject("Outlook.Application")
Set oSelection = objOL.ActiveExplorer.Selection
2) Selection does not support the Move method. (When in doubt, check the
object browser: Press ALt+F11 to open the VBA environment in Outlook, then
press F2.)
3) Selection is a collection. To work with individual items in a collection,
you use a For Each ... Next loop or a For I = ... Next loop.
4) Never try to move or delete items in a For Each ... Next loop. Instead
use a countdown loop:
intCount = oSelection.Count
For i = intCount to 1 Step -1
Set objItem = oSelection.Items(i)
objItem.Move myFolder3
Next
You may find the sample code at
http://www.slipstick.com/dev/code/getfolder.htm useful in returning a
particular public folder as a MAPIFolder object.
--
Sue Mosher, Outlook MVP
Outlook and Exchange solutions at http://www.slipstick.com
Author of
Microsoft Outlook Programming: Jumpstart
for Administrators, Power Users, and Developers
http://www.slipstick.com/books/jumpstart.htm
Quote:
> Hello,
> I am trying to create a macro (to ultimately be put on the toolbar) to
move
> selected mail items to a public folder. Obviously I am CLUELESS based on
> what I have attempted to this point:
> Sub JunkMail()
> Set myFolder1 = Session.Folders("Public Folders")
> Set myFolder2 = myFolder1.Folders("All Public Folders")
> Set myFolder3 = myFolder2.Folders("Junk Mail")
> olSelection.Move (myFolder3)
> End Sub
> Any guidance and/or kindness would be greatly appreciated.
> Deuce