![]() |
If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. |
|
|
Thread Tools | Search this Thread | Display Modes |
#1
|
|||
|
|||
![]()
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 |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Calendar folder missing from folder list in outlook 2003 | Bill B | Outlook - Calandaring | 11 | July 7th 06 04:44 PM |
Importing Outlook Alias into an Excel 2003 macro | fidgitthedigit | Outlook - General Queries | 2 | May 30th 06 02:11 PM |
Outlook 2003: Macro field is empty .. vbaproject.otm not recognized | Kemal Balioglu | Outlook and VBA | 2 | May 26th 06 05:45 PM |
view folder size in outlook using a macro | john mcmichael | Outlook and VBA | 0 | May 4th 06 04:07 PM |
Contacts folder in Outlook 2003 | Jos Sall | Outlook - Using Contacts | 3 | February 21st 06 06:58 AM |