Link to home
Start Free TrialLog in
Avatar of jana
janaFlag for United States of America

asked on

VBA to move selected email from an Inbox to X folder within same PST or another PST in Outlook

Hi!

I am trying to create a script that would send the selected email in either 2 PST Inbox and move them to specif project folder.

I have various PST files where each has an Inbox receiving emails.

I want to click on any email on any PST inbox and move them to their project folder.

The Projects folders are located under the Default  PST Inbox:
PST File 1(All in inbox)
   Inbox (256)
        Projects DONE
            Sivana LLC Project
                Done tasks of Sivana
                     ToBeDeleted tasks of Sivana
                          Authorize First
             Rurals Projects
                Done tasks of Rurals
                     ToBeDeleted tasks of Rurals
                          Authorize First
PST FIles 2
   Inbox (10)
        Projects DONE
            GNN LLC Project
                Done tasks of GNN
                     ToBeDeleted tasks of GNN
                          Authorize First


Notice the in this example I have two inbox (2 PST) where the PST File 1 is the default PST.  Also notices that both PST have up to each Inbox has 5 level of folders created under their respected Inbox.

What I am trying to do is at any Inbox click on the received email and send it to any folder of any PST.

I have search and worked on varios script, this is the closest I had found:
Sub aaaMoveIboxToFolderDEST()
 Dim OutApp As Outlook.Application
 Dim oNS As Outlook.NameSpace
 Dim objInboxFolder As Outlook.MAPIFolder
 Dim objDestFolder As Outlook.MAPIFolder
 Dim curFolder As Outlook.MAPIFolder
    
'Spring up PST/Folders
 usrSubfolders.Show 'variable 'PST_FolderSelected'
 
 Set OutApp = Application
 Set oNS = OutApp.GetNamespace("MAPI")
'use the selected folder
 Set curFolder = OutApp.ActiveExplorer.currentfolder
'Set Inbox
 Set objInboxFolder = oNS.GetDefaultFolder(olFolderInbox)
'Set Destination folder
 Set objDestFolder = objInboxFolder.Parent.Folders(PST_FolderSelected) 'folder name select in form above
'move to the folder
 curFolder.MoveTo objDestFolder
End Sub

Open in new window


The problem seems to be in
Set objDestFolder = objInboxFolder.Parent.Folders(PST_FolderSelected) '

Open in new window

the error message is:
User generated image
Please help on the code
Avatar of Bill Prew
Bill Prew

When you get the error, if you Debug, and the add a Watch on PST_FolderSelected, does it have a value and is it the folder that you expect?


»bp
Avatar of jana

ASKER

yes and I even place the constant, for example "ToBeDeleted tasks of Sivana" same error... maybe has something to do being a sub folder?
What is the data type of PST_FolderSelected?


»bp
Avatar of jana

ASKER

Folder name, alphanumeric, string
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jana

ASKER

Ok will try
Avatar of jana

ASKER

Hi Bill... I tested and it doesn't give any errors, it goes thru ok.. I have also tested going to with all level, as if I wanted to move the email to that speciific level:

  • Set objDestFolder = objInboxFolder.Folders("Projects DONE")
  • Set objDestFolder = objInboxFolder.Folders("Projects DONE").Folders("Sivana LLC Project").
  • Set objDestFolder = objInboxFolder.Folders("Projects DONE").Folders("Sivana LLC Project").Folders("Done tasks of Sivana")
  • Set objDestFolder = objInboxFolder.Folders("Projects DONE").Folders("Sivana LLC Project").Folders("Done tasks of Sivana").Folders("ToBeDeleted tasks of Sivana")

No error message!

However, at the line "curFolder.MoveTo objDestFolder" it doesn't do the move - and I am running the VBA with step-by-step F8 and the curFolder.MoveTo objDestFolder has the value of the folder I want to send it to.

Help
Avatar of jana

ASKER

Hi, working with it, I found that the line in question moves a folder.  I don't want that. I want to move the selected email in the inbox to the destination folder.  

This is the latest a bit more organize code:

Sub aaaMoveIboxToFolderDEST()
 Dim OutApp As Outlook.Application
 Dim oNS As Outlook.NameSpace
 Dim objInboxFolder As Outlook.MAPIFolder
 Dim objDestFolder As Outlook.MAPIFolder
 Dim curFolder As Outlook.MAPIFolder
 Dim ParentFolder, ChildFolder1, ChildFolder2, ChildFolder3, ChildFolder4 As String

'This is the manual resuts of a UserForem which I will be use to ask these folders later on
 ParentFolder = "Projects DONE"
 ChildFolder1 = "Sivana LLC Project" 'cust & prosp
 ChildFolder2 = "Done tasks of Sivana"
 ChildFolder3 = "ToBeDeleted tasks of Sivana"
 ChildFolder4 = "Authorize First"
'Start the process
 Set OutApp = Application
 Set oNS = OutApp.GetNamespace("MAPI")
'use the selected folder
 Set curFolder = OutApp.ActiveExplorer.currentfolder
'Set Inbox
 Set objInboxFolder = oNS.GetDefaultFolder(olFolderInbox)
'Set Destination folder
 Set objDestFolder = objInboxFolder.Folders(ParentFolder) _
    .Folders(ChildFolder1).Folders(ChildFolder2).Folders(ChildFolder3).Folders(ChildFolder4)

'Move to the folder:
'--- missing statement to move the current select email to be
'--- sent to folder selected above
 xxx
End Sub

Open in new window


Please help on the command that determines which email I have clicked on prior running the script and the actual moving to the destination folder.

Thank u!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jana

ASKER

Hi and thanx!  I was just working with it also and came with this:

Dim obj As Object 
 Set obj = Application.ActiveWindow
 If TypeOf obj Is Outlook.Inspector Then
    Set obj = obj.currentItem
   Else
    Set obj = obj.Selection(1)
    obj.Move objDestFolder
    End If

Open in new window

(I place this code at the last part of the routine of the script I sent u last night, after "'Move to the folder:")
It does move the email to the destination folder, but can you check if its correctly written?

I will try you code
Yes, that looks like similar logic just done a little bit differently.  The code at the link I provided is a little cleaner and using a function is a bit more modular, but at a quick look I think what you have should get the same result.  And really in both cases we should make sure that something was selected...


»bp
Avatar of jana

ASKER

Thanx!

I just embeded your code and I see the email selected is chosen, but how do I move it?

GetCurrentItem.Move objDestFolder?
Avatar of jana

ASKER

Just tried it, it seems to work... thank u very much!
(if GetCurrentItem.Move objDestFolder is incorrect, please let me know)
Looks good, glad that helped.


»bp