Log in

View Full Version : [SOLVED:] Outlook in Excel - move folder



ChrisTexas
10-11-2007, 12:12 PM
Is it possible to move an email from my inbox to a sub-folder. I set my criteria:


Set OLF = GetObject("", "Outlook.Application").GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
With OLF.Items(i)
If InStr(LCase(.Body), LCase(Bodyin(varc))) > 0 Then
att = True
Exit For

I see you can set the subfolder, but I don't see how to move the email to a subfolder. Any help is appreciated.

Oorang
10-11-2007, 02:57 PM
:welcome:
Hi Chris,
Welcome to the board. Here is an example of how to do what you requested. This example is simple is just clones stuff from your inbox to an archive. You will probably want to update the folder path for it to work, but the drift should be there:) Post back if you hit rough water!

Public Sub AttackOfTheClones()
Dim ns As Outlook.NameSpace
Dim fldrInbox As Outlook.mapiFolder
Dim destFldr As Outlook.mapiFolder
Dim itm As Object
Dim mlItm As Outlook.MailItem
On Error GoTo Err_Hnd
Set ns = Outlook.Application.Session
Set fldrInbox = ns.GetDefaultFolder(olFolderInbox)
Set destFldr = GetFolder("Archive 2007\Clones")
If Not destFldr Is Nothing Then
For Each itm In fldrInbox.Items
If itm.Class = olMail Then
Set mlItm = itm.Copy
mlItm.Move destFldr
End If
Next
End If
Exit_Proc:
On Error Resume Next
Exit Sub
Err_Hnd:
VBA.MsgBox "Error " & VBA.Err.number & " (" & VBA.Err.Description & _
") in procedure AttackOfTheClones of Module Module1"
Resume Exit_Proc
End Sub

Public Function GetFolder(ByVal strFolderPath As String) As Outlook.mapiFolder
Const strBckSlsh_c As String = "\"
Const strFwdSlsh_c As String = "\"
Const lngLwrBnd_c As Long = 1
Const lngRootIndx_c As Long = 0
Dim lngIndex As Long
Dim strFolders() As String
Dim fldr As Outlook.mapiFolder
On Error GoTo Err_Hnd
strFolderPath = Replace(strFolderPath, strFwdSlsh_c, strBckSlsh_c)
strFolders() = Split(strFolderPath, strFwdSlsh_c)
Set fldr = Outlook.Session.Folders.Item(strFolders(lngRootIndx_c))
For lngIndex = lngLwrBnd_c To UBound(strFolders)
Set fldr = fldr.Folders.Item(strFolders(lngIndex))
Next
Set GetFolder = fldr
Exit Function
Err_Hnd:
VBA.MsgBox "Error " & VBA.Err.number & " (" & VBA.Err.Description & _
") in procedure GetFolder of Module Module1"
End Function

ChrisTexas
10-12-2007, 07:43 AM
Thanks for the reply. I can't get the "destfldr" to come up with anything.

I tried it a few ways:


Set destFldr = GetFolder("Inbox\keep")
Set destFldr = GetFolder("keep")

It keeps coming up with nothing. I added the set fldr function. If you can't tell, I am new to trying outlook vb, so any help is appreciated. Thanks!

Oorang
10-12-2007, 08:38 AM
Don't sweat, outlook's folder structure is a bear. I suspect the issue is the inbox name. For instance my Inbox is not named "Inbox", it's named "Mailbox - Bush, Aaron" so my path would look like this:


Set destFldr = GetFolder("Mailbox - Bush, Aaron\keep")

I just right clicked on my inbox and went to properties to get the name.

ChrisTexas
10-12-2007, 11:41 AM
That was it. Thank you, much appreciated.