![]() |
|
|
|
#1
|
|||
|
|||
|
I'm using outlook 2010 and 2007 so please can you tell me if your advice is specific to either version, thank you
I want to move emails from one folder to another by a macro. I have searched the internet and found a couple of ideas but can not make them work. I also need to search the subject line for a "string" that starts "abcd" and then is followed by 6 digits, so I'm looking for something that looks like this abcd123456 The folder I want to move the email too will vary so for now could we assume I want to move an email to either the Drafts or Deleted folder from my inbox? I think this code below that I found on the internet proves that my understanding of the path is correct. The Inbox or Drafts folder is momentarily displayed. there is a second lot of code below this. Code:
Sub TestGetFolder()
Dim folder As Outlook.folder
'Set folder = GetFolder("Personal Folders\Inbox")
Set folder = GetFolder("Personal Folders\Drafts")
If Not (folder Is Nothing) Then
folder.Display
End If
End Sub
Function GetFolder(ByVal FolderPath As String) As Outlook.folder
Dim TestFolder As Outlook.folder
Dim FoldersArray As Variant
Dim i As Integer
On Error GoTo GetFolder_Error
If Left(FolderPath, 2) = "\\" Then
FolderPath = Right(FolderPath, Len(FolderPath) - 2)
End If
'Convert folderpath to array
FoldersArray = Split(FolderPath, "\")
Set TestFolder = Application.Session.Folders.Item(FoldersArray(0))
If Not TestFolder Is Nothing Then
For i = 1 To UBound(FoldersArray, 1)
Dim SubFolders As Outlook.Folders
Set SubFolders = TestFolder.Folders
Set TestFolder = SubFolders.Item(FoldersArray(i))
If TestFolder Is Nothing Then
Set GetFolder = Nothing
End If
Next
End If
'Return the TestFolder
Set GetFolder = TestFolder
Exit Function
GetFolder_Error:
Set GetFolder = Nothing
Exit Function
End Function
I then found this code on the internet but cannot make it work although this is the line of code that I have created having modified the original line Set moveToFolder = ns.Folders("Personal Folders\Inbox").Folders("Personal Folders\Drafts").Folders but I get an error message saying target folder not found. Code:
'Outlook VB Macro to move selected mail item(s) to a target folder
Sub MoveToFiled()
On Error Resume Next
Dim ns As Outlook.NameSpace
Dim moveToFolder As Outlook.MAPIFolder
Dim objItem As Outlook.MailItem
Set ns = Application.GetNamespace("MAPI")
Set moveToFolder = ns.Folders("Personal Folders\Inbox").Folders("Personal Folders\Drafts").Folders
If Application.ActiveExplorer.Selection.Count = 0 Then
MsgBox ("No item selected")
Exit Sub
End If
If moveToFolder Is Nothing Then
MsgBox "Target folder not found!", vbOKOnly + vbExclamation, "Move Macro Error"
End If
For Each objItem In Application.ActiveExplorer.Selection
If moveToFolder.DefaultItemType = olMailItem Then
If objItem.Class = olMail Then
objItem.Move moveToFolder
End If
End If
Next
Set objItem = Nothing
Set moveToFolder = Nothing
Set ns = Nothing
End Sub
thank you smiler44 |
|
#2
|
|||
|
|||
|
To move selected mailitems. No code for searching for items.
Should be valid in 2007 and 2010 (MAPIFolder is 2003, now Folder, but still works) Code:
Sub MoveToFiled()
'On Error Resume Next ' <--- Do not use this unless there is a specific purpose
' Never at the start.
' Not when debugging.
'
' Turn it off as quickly as possible with
' On Error GoTo 0
Dim ns As Outlook.Namespace
Dim moveToFolder As Outlook.MAPIFolder
'Dim objItem As Outlook.mailitem
Dim objItem As Object ' <--- the selected objItem may not be a mailitem
Set ns = Application.GetNamespace("MAPI")
On Error Resume Next ' To bypass the error when the target folder is not found.
' moveToFolder will be Nothing
' Enter the exact names of the folders
' No slashes. Walk the path one folder at a time.
Set moveToFolder = ns.folders("Personal Folders").folders("Drafts")
On Error GoTo 0 ' No need for On Error Resume Next any more
If moveToFolder Is Nothing Then
MsgBox "Target folder not found!", vbOKOnly + vbExclamation, "Move Macro Error"
' There will an error moving the selected mail to a folder set to Nothing
' since the On Error Resume Next is off
' Leave now before this occurs.
GoTo ExitRoutine
Else
Debug.Print "moveToFolder: " & moveToFolder
End If
If Application.ActiveExplorer.Selection.count = 0 Then
MsgBox ("No item selected")
' Exit Sub <--- forgot to clean up
GoTo ExitRoutine
End If
For Each objItem In Application.ActiveExplorer.Selection
If moveToFolder.DefaultItemType = olMailItem Then
If objItem.Class = olMail Then
objItem.Move moveToFolder
End If
End If
Next
ExitRoutine:
Set objItem = Nothing
Set moveToFolder = Nothing
Set ns = Nothing
End Sub
|
|
#3
|
|||
|
|||
|
niton, that you for the reply. With your code you say to move selected item, can you help me a bit more? Have I manually selected the item and then run your code or does your code move the first email in the folder?
I'm off to bed I'll look tomorrow but thank you in advance smiler44 |
|
#4
|
|||
|
|||
|
The code works on items you select.
You will probably find For Each logic skips every other item if you select multiple items. This issue occurs when moving or deleting. If so then select only one item at a time until you know how to change For Each objItem In Application.ActiveExplorer.Selection to For i = Application.ActiveExplorer.Selection.Count to 1 step -1 |
|
#5
|
|||
|
|||
|
niton,
yes thank you this is working. Alas I have more questions. I'm struggling with changing this line: For Each objItem In Application.ActiveExplorer.Selection I either need to change the above line of code as I need to set the path for the folder to move emails from or I need to add a line of code before it to select the folder to move the emails from. my second challenge is to be able to obtain the subject line and put it into a variable. I think this is the path ("inbox").Folders("testin").Folders("testout") can you help me further? thank you smiler44 |
|
#6
|
|||
|
|||
|
I think I can get the subject.
I have added Dim asg As String and then between these two lines If objItem.Class = olMail Then objItem.Move moveToFolder I have added asg = objItem.Subject if I can then add code to do something and depending on the result set my move to folder I could be there. near but yet so far! smiler44 |
|
#7
|
|||
|
|||
|
Code:
Sub searchSubject()
' In the Visual Basic Editor (VBE)
' Tools menu | References...
' Tick the entry for
' Microsoft VBScript Regular Expressions 5.5
Dim ns As Outlook.Namespace
Dim searchFolder As Folder
Dim searchItems As Items
Dim msg As mailitem
Dim foundFlag As Boolean
Dim i As Long
Set ns = Application.GetNamespace("MAPI")
On Error Resume Next ' To bypass the error when the source folder is not found.
' searchFolder will be Nothing
' Enter the exact names of the folders
' No slashes. Walk the path one folder at a time.
Set searchFolder = ns.folders("Personal Folders").folders("inbox").folders("testin").folders("testout")
On Error GoTo 0
If searchFolder Is Nothing Then
MsgBox "Source folder not found!", vbOKOnly + vbExclamation, "searchSubject error"
GoTo ExitRoutine
Else
Debug.Print vbCr & "searchFolder: " & searchFolder
End If
Set searchItems = searchFolder.Items
For i = searchItems.count To 1 Step -1
If searchItems(i).Class = olMail Then
Set msg = searchItems(i)
pattern_abcd123456 msg, foundFlag
If foundFlag = True Then
Debug.Print " Move this mail: " & searchItems(i)
End If
End If
Next
ExitRoutine:
Set msg = Nothing
Set searchItems = Nothing
Set searchFolder = Nothing
Set ns = Nothing
End Sub
Sub pattern_abcd123456(MyMail As mailitem, fndFlag)
Dim subj As String
Dim re As Object
Dim match As Variant
fndFlag = False
subj = MyMail.Subject
Set re = CreateObject("vbscript.regexp")
re.pattern = "[a-z][a-z][a-z][a-z][0-9][0-9][0-9][0-9][0-9][0-9]"
For Each match In re.Execute(subj)
fndFlag = True
Debug.Print vbCr & subj
Debug.Print " *** Pattern found: " & match
Next
End Sub
|
|
#8
|
|||
|
|||
|
Niton, thank you I'll give this a go.
Would I be taking the micky if I asked if this code could be converted to VBA code suitable for Excel? I just copied and pasted it and Excel did not like it. I'll try outlook, see if that will play ball. cheers smiler44 |
|
#9
|
|||
|
|||
|
Sorry more questions
what does this line do? Debug.Print " Move this mail: " & searchItems(i) I put this below it and got a message box with the subject line MsgBox (searchItems(i)) Great news niton. I can find the email with the search criteria I set, I can move the email to the right folder. I will set about trimming the subject line so I just have my search criteria as at the moment I get the whole subject line. I then need to do something based on the subject line If I can get my excel code to work in outlook wow what a result but I think I am going to need this outlook code to be converted to code that excel will work with. tomorrow is another day and so I will have another go niton, appreciate the help as I would still be floundering if it were not for you, thank you smiler44 |
|
#10
|
|||
|
|||
|
Niton,
I have copied your code into excel and with help form an Excel forum and a line or two change have your code working in excel. Thank you very much for writing the macro for me, I really appreciate it. Below is what I now have. It works in testing and over the next couple of days will move the code into the working environment. Thanks again niton smiler44 Code:
Sub moveemail()
' In the Visual Basic Editor (VBE)
' Tools menu | References...
' Tick the entry for
' Microsoft VBScript Regular Expressions 5.5
' &
' microsoft outlook 12.0 object libary
Dim nsNamespace As Outlook.Namespace
Dim objSourceFolder As Outlook.MAPIFolder
Dim moveToFolder As Outlook.MAPIFolder
Dim searchItems As Items
Dim msg As MailItem
Dim foundFlag As Boolean
Dim i As Long
Set NS = CreateObject("Outlook.Application").GetNamespace("MAPI")
On Error Resume Next ' To bypass the error when the source folder is not found.
' searchFolder will be Nothing
' Enter the exact names of the folders
' No slashes. Walk the path one folder at a time.
Set searchFolder = NS.Folders("Personal Folders").Folders("inbox").Folders("testin").Folders("testout")
Set moveToFolder = NS.Folders("Personal Folders").Folders("Drafts").Folders("testing").Folders("test")
On Error GoTo 0
If searchFolder Is Nothing Then
MsgBox "Source folder not found!", vbOKOnly + vbExclamation, "searchSubject error"
GoTo ExitRoutine
Else
Debug.Print vbCr & "searchFolder: " & searchFolder
End If
Set searchItems = searchFolder.Items
For i = searchItems.Count To 1 Step -1
If searchItems(i).Class = olMail Then
Set msg = searchItems(i)
pattern_abcd123456 msg, foundFlag
If foundFlag = True Then
Debug.Print " Move this mail: " & searchItems(i)
MsgBox (searchItems(i))
Call whattodonow
searchItems(i).Move moveToFolder
End If
End If
Next
ExitRoutine:
Set msg = Nothing
Set searchItems = Nothing
Set searchFolder = Nothing
Set NS = Nothing
MsgBox ("all mail items checked")
End Sub
Sub patternabcd123456(MyMail As MailItem, fndFlag)
Dim subj As String
Dim re As Object
Dim match As Variant
fndFlag = False
subj = MyMail.Subject
Set re = CreateObject("vbscript.regexp")
re.Pattern = "[a-z][a-z][a-z][a-z][0-9][0-9][0-9][0-9][0-9][0-9]"
For Each match In re.Execute(subj)
fndFlag = True
Debug.Print vbCr & subj
Debug.Print " *** Pattern found: " & match
Next
End Sub
Sub whattodonow()
MsgBox ("checking what to do now")
End Sub
Last edited by smiler44; 11-20-2014 at 03:05 PM. Reason: missing code |
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| move emails to folder with a subject that is similar | megatronixs | Outlook | 0 | 10-14-2014 03:05 AM |
Outlook VBA rule to search email attachements and move the emails into a folder
|
genius7 | Outlook | 6 | 09-09-2014 07:01 AM |
Move files from one folder to another
|
obasanla | Word | 1 | 09-28-2012 04:42 PM |
| Move conversation to folder after replying | karlads | Outlook | 0 | 01-28-2012 12:52 PM |
| How to move pics in excel to another folder? | SPI | Excel | 1 | 08-19-2008 11:58 AM |