Move Selected Mail Items to Public Folder 
Author Message
 Move Selected Mail Items to Public Folder

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



Mon, 19 Sep 2005 21:41:34 GMT  
 Move Selected Mail Items to Public Folder
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



Mon, 19 Sep 2005 21:54:25 GMT  
 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



Tue, 20 Sep 2005 00:40:57 GMT  
 Move Selected Mail Items to Public Folder
You can just put all the procedures in the same VBA module. What didn't work? Did you step through the code?
Quote:

> 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



> > 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



Tue, 20 Sep 2005 01:11:33 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. How to create a public folder journal item for user-selected e-mail messages

2. move item from one public folder to another

3. Automatic move of Journal items to public folder

4. Need to save a mail item to a public folder when it is sent

5. Deleting a Public Folder Mail Item Does not work - Even with right permissions

6. Determine Selected Folder and Selected Item in Folder

7. Move New Mail Items To A Sub Folder?

8. Find the currently selected item in a mail folder

9. Unread Items in Public Folders like in personal Folders

10. Detecting an item move to Deleted Items folder

11. create an e-mail mailing from a folder containing tasks items

12. Programming a daily move of public folder contents

 

 
Powered by phpBB® Forum Software