J
James
I'm trying to create a macro that searches a folder called "electronics"
for a string in the subject "TV" and if it is a match, then moves the msg to
the inbox. I cannot get the following code to work. The line
Set olStartFolder = olSession.Folders("electronics")
isn't correct so thecode bombs. How do i do it? Thanks. James
Dim strSearchString As String
Dim lCountOfFound As Long
Sub WalkFolders()
Dim olApp As Outlook.Application
Dim olSession As Outlook.NameSpace
Dim olStartFolder As Outlook.MAPIFolder
Dim strPrompt As String
'Initialize count of folders searched
lCountOfFound = 0
' Get a reference to the Outlook application and session.
Set olApp = Application
Set olSession = olApp.GetNamespace("MAPI")
' Allow the user to input the search string.
strPrompt = "Enter the search string to be found in the subject:"
strSearchString = InputBox(strPrompt)
If strSearchString <> "" Then
' Allow the user to pick the folder in which to start the search.
' Set olStartFolder = olSession.PickFolder
Set olStartFolder = olSession.Folders("electronics")
' Check to make sure user didn't cancel PickFolder dialog.
If Not (olStartFolder Is Nothing) Then
' Start the search process.
ProcessFolder olStartFolder
MsgBox CStr(lCountOfFound) & " messages were found."
End If
End If
End Sub
Sub ProcessFolder(CurrentFolder As Outlook.MAPIFolder)
Dim i As Long
Dim olNewFolder As Outlook.MAPIFolder
' late bind this object variable, since it could be various item types
Dim olTempItem As Object
' Loop through the items in the current folder.
' Looping through backwards in case items are to be deleted,
' as this is the proper way to delete items in a collection.
For i = CurrentFolder.Items.Count To 1 Step -1
Set olTempItem = CurrentFolder.Items(i)
' Check to see if a match is found
If InStr(1, olTempItem.Subject, strSearchString, 0) > 0 Then
' The following are examples of what you can do:
' 1. To notify that message was found:
' MsgBox "Found message with subject: " & olTempItem.Subject
'
' 2. To delete the item:
' olTempItem.Delete
'
' 3. To move the item:
' NOTE: You need to first define olDestFolder
' olTempItem.Move olDestFolder
'
lCountOfFound = lCountOfFound + 1
End If
Next
' Loop through and search each subfolder of the current folder.
For Each olNewFolder In CurrentFolder.Folders
If olNewFolder.Name <> "Deleted Items" Then
ProcessFolder olNewFolder
End If
Next
End Sub
for a string in the subject "TV" and if it is a match, then moves the msg to
the inbox. I cannot get the following code to work. The line
Set olStartFolder = olSession.Folders("electronics")
isn't correct so thecode bombs. How do i do it? Thanks. James
Dim strSearchString As String
Dim lCountOfFound As Long
Sub WalkFolders()
Dim olApp As Outlook.Application
Dim olSession As Outlook.NameSpace
Dim olStartFolder As Outlook.MAPIFolder
Dim strPrompt As String
'Initialize count of folders searched
lCountOfFound = 0
' Get a reference to the Outlook application and session.
Set olApp = Application
Set olSession = olApp.GetNamespace("MAPI")
' Allow the user to input the search string.
strPrompt = "Enter the search string to be found in the subject:"
strSearchString = InputBox(strPrompt)
If strSearchString <> "" Then
' Allow the user to pick the folder in which to start the search.
' Set olStartFolder = olSession.PickFolder
Set olStartFolder = olSession.Folders("electronics")
' Check to make sure user didn't cancel PickFolder dialog.
If Not (olStartFolder Is Nothing) Then
' Start the search process.
ProcessFolder olStartFolder
MsgBox CStr(lCountOfFound) & " messages were found."
End If
End If
End Sub
Sub ProcessFolder(CurrentFolder As Outlook.MAPIFolder)
Dim i As Long
Dim olNewFolder As Outlook.MAPIFolder
' late bind this object variable, since it could be various item types
Dim olTempItem As Object
' Loop through the items in the current folder.
' Looping through backwards in case items are to be deleted,
' as this is the proper way to delete items in a collection.
For i = CurrentFolder.Items.Count To 1 Step -1
Set olTempItem = CurrentFolder.Items(i)
' Check to see if a match is found
If InStr(1, olTempItem.Subject, strSearchString, 0) > 0 Then
' The following are examples of what you can do:
' 1. To notify that message was found:
' MsgBox "Found message with subject: " & olTempItem.Subject
'
' 2. To delete the item:
' olTempItem.Delete
'
' 3. To move the item:
' NOTE: You need to first define olDestFolder
' olTempItem.Move olDestFolder
'
lCountOfFound = lCountOfFound + 1
End If
Next
' Loop through and search each subfolder of the current folder.
For Each olNewFolder In CurrentFolder.Folders
If olNewFolder.Name <> "Deleted Items" Then
ProcessFolder olNewFolder
End If
Next
End Sub