Hardcoding an Outlook 2003 folder name in a Macro?

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
 
M

Marte Palmer - Self-taught Know-little

Try something like...

--------------------
Dim objFolder As MAPIFolder
....

' Get Outlook Folder Name
Set objFolder = objNS.PickFolder
If TypeName(objFolder) <> "Nothing" Then
Set olStartFolder = objFolder
End If
 
S

Sue Mosher [MVP-Outlook]

James, to get a non-default folder, you need to walk the folder hierarchy using the Folders collections or use a function that does that for you. See http://www.outlookcode.com/d/code/getfolder.htm

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top