This macro is an excel 2007 macro but fails to run in excel 2010.
stepping through the code it fails at
Set searchItems = searchFolder.Items
I fear it may be line by line I ask for the excel 2010 equivalent code
thank you smiler44
stepping through the code it fails at
Set searchItems = searchFolder.Items
I fear it may be line by line I ask for the excel 2010 equivalent code
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.
[FONT="Arial"]Set searchFolder = NS.Folders("Mailbox - group name G").Folders("inbox").Folders("department").Folders("engineers name")[/FONT]
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
'''''''''''''''''''''''''''''''''''''
unreadMessagesInInbox = searchFolder.UnReadItemCount 'counts the number of unread emails in inbox
MsgBox ("unread messages = " & unreadMessagesInInbox)
TotalmessagesInInbox = searchFolder.Items.Count ' counts total number of emails in inbox
MsgBox (" total messages = " & TotalmessagesInInbox)
'''''''''''''''''''''''''''''''''''''
For Each oMail In searchFolder.Items
If oMail.UnRead Then
oUnread = oUnread + 1 ' this gets number of unread emails
Else
oread = oread + 1 ' this gets number of read emails
End If
Next
MsgBox ("read messages = " & oread)
'''''''''''''''''''''''''''''''''''''''
'For i = searchFolder.Items.Count To 1 Step -1
For i = searchItems.Count To 1 Step -1
If searchItems(i).Class = olMail Then
Set msg = searchItems(i)
patternabcd123456 msg, foundFlag
'pattern_abcd123456 msg, foundFlag
If foundFlag = True Then
Debug.Print " Move this mail: " & searchItems(i)
MsgBox (searchItems(i))
Call whattodonow
searchItems(i).UnRead = True ' if email has been read changes it to unread
searchItems(i).Move moveToFolder
'searchItems(i).Save
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
thank you smiler44