Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-20-2005, 10:04 PM
Microchip Microchip is offline
Novice
Microsoft Outlook Movemail
 
Join Date: Nov 2005
Location: Indiana
Posts: 5
Microchip is on a distinguished road
Exclamation Microsoft Outlook Movemail

I am trying to write an Outlook program to move mail to a specific folder based on text in the subject, or anything else.

The program can move mail from the Inbox to a subfolder of Inbox, but I can't for the life of me figure out how to make it go to a folder called "Confirmed", which is NOT a subfolder of Inbox.

Any help would be greatly appreciated!

Here is the source code:
Option Explicit
Sub Analyze()
' This program will analyze the Outlook Inbox
On Error GoTo Analyze_Err
' The NameSpace is the object that gives you access to all of Outlook's Folders.
' In Outlook, there is only one, and it is called "MAPI", which
' is an acronym for Messaging Application Programming Interface.
Dim ns As NameSpace
Dim CurrentMailItem As MailItem
' We are going to refer to a mail folder (a MAPIFolder object)
Dim Inbox As MAPIFolder
Dim Item As Object
Dim Atmt As Attachment ' The attachment objects we will be looking for
Dim FileName As String ' Name and save path for each attachment
Dim a As Integer ' Number of attachments found
Dim i As Integer ' Number of items in the Inbox
Dim m As Integer ' Number of items moved
Dim strMsg As String
Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
a = 0 ' Init the number of attachments
i = 0 ' Init the number of items
If Inbox.Items.Count = 0 Then
MsgBox "There are no messages in the Inbox.", vbInformation, _
"Nothing Found"
Exit Sub
End If
' The Inbox does indeed contain something.
For Each Item In Inbox.Items
i = i + 1
For Each Atmt In Item.Attachments
a = a + 1
Next Atmt
strMsg = ""
If InStr(1, Item.Subject, "SR", vbTextCompare) Then
strMsg = strMsg & "This item pertains to an SR." & vbCrLf
strMsg = strMsg & "Subject: " & Item.Subject & vbCrLf
If MoveMail(Item, Inbox.Folders.Item("Processed").EntryID) Then
m = m + 1
strMsg = strMsg & "Item was moved!" & vbCrLf
Else
strMsg = strMsg & "The message was not moved!" & vbCrLf
End If
MsgBox strMsg, vbInformation, "Item #" & Str(i) & " Info"
End If
Next Item
strMsg = "I found " & i & " items in your Inbox." & vbCrLf
strMsg = strMsg & _
"I moved " & m & " files." & vbCrLf
MsgBox strMsg, vbInformation, "Finished!"
Analyze_Exit:
Set Atmt = Nothing
Set Item = Nothing
Set ns = Nothing


Exit Sub

Analyze_Err:
MsgBox "An unexpected error has occurred." & vbCrLf & _
"Please note and report the following information." & vbCrLf & _
"Macro name: Analyze" & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Description: " & Err.Description & vbCrLf _
, vbCritical, "Error!"
Resume Analyze_Exit
Exit Sub
End Sub

Private Function MoveMail(CurrentMailItem As MailItem, strTargFldrID As String) As Boolean
Dim CurrentNameSpace As NameSpace
Dim CurrentMoveMailItem As MailItem
Set CurrentNameSpace = Application.GetNamespace("MAPI")
On Error GoTo FINISH:
Set CurrentMoveMailItem = CurrentMailItem.Copy
CurrentMoveMailItem.Move _
DestFldr:=CurrentNameSpace.GetFolderFromID(strTarg FldrID)
'CurrMailItem.Delete
Err.Number = True
FINISH:
MoveMail = CBool(Err.Number)
End Function
Reply With Quote
  #2  
Old 11-21-2005, 07:55 AM
Matrix's Avatar
Matrix Matrix is offline Microsoft Outlook Movemail Windows 10 Microsoft Outlook Movemail Office 2010
Admin
 
Join Date: Jan 2005
Posts: 429
Matrix is on a distinguished road
Default

Outlook starts from Inbox by default. If you are dealing with folders outside of Inbox, you need to walk the folder hierarchy using the Folders collections. This link may help: http://www.outlookcode.com/d/code/getfolder.htm

Welcome to the forums.
Reply With Quote
  #3  
Old 11-21-2005, 05:20 PM
Microchip Microchip is offline
Novice
Microsoft Outlook Movemail
 
Join Date: Nov 2005
Location: Indiana
Posts: 5
Microchip is on a distinguished road
Default Can't walk Yet!

No, I can't walk yet. I am still crawling. Referring me to examples that are not specific to what I am trying to do does not do me any good. Sorry.

I am sure that someone who knows this stuff well can take a few minutes and cirrect the one or two line sof code in my example that need to be corrected. Thanks in advance!
Reply With Quote
  #4  
Old 11-21-2005, 07:13 PM
Matrix's Avatar
Matrix Matrix is offline Microsoft Outlook Movemail Windows 10 Microsoft Outlook Movemail Office 2010
Admin
 
Join Date: Jan 2005
Posts: 429
Matrix is on a distinguished road
Default

Sorry that I didn't make it clear. The reason for the error is explained in my previous post, here is the code changes (Tested on my Outlook 2003):

1. Add the function from http://www.outlookcode.com/d/code/getfolder.htm
Quote:
Public Function GetFolder(strFolderPath As String)As MAPIFolder
' folder path needs to be something like
' "Public Folders\All Public Folders\Company\Sales"
Dim objApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim colFolders As Outlook.Folders
Dim objFolder As Outlook.MAPIFolder
Dim arrFolders() As String
Dim I As Long
On Error Resume Next

strFolderPath = Replace(strFolderPath, "/", "\")
arrFolders() = Split(strFolderPath, "\")
Set objApp = CreateObject("Outlook.Application")
Set objNS = objApp.GetNamespace("MAPI")
Set objFolder = objNS.Folders.Item(arrFolders(0))
If Not objFolder Is Nothing Then
For I = 1 To UBound(arrFolders)
Set colFolders = objFolder.Folders
Set objFolder = Nothing
Set objFolder = colFolders.Item(arrFolders(I))
If objFolder Is Nothing Then
Exit For
End If
Next
End If

Set GetFolder = objFolder
Set colFolders = Nothing
Set objNS = Nothing
Set objApp = Nothing
End Function
2. Change
Quote:
DestFldr:=CurrentNameSpace.GetFolderFromID(strTarg FldrID)
to
Quote:
DestFldr:=GetFolder(strTargFldrID)
3. Change
Quote:
If MoveMail(Item, Inbox.Folders.Item("Processed").EntryID) Then
to
Quote:
If MoveMail(Item, "Personal Folders\outside") Then
Personal Folders\outside is the complete path of my test folder.

Please note that the changed code only deals with the folders outside of "Inbox".
Reply With Quote
  #5  
Old 11-21-2005, 07:43 PM
Microchip Microchip is offline
Novice
Microsoft Outlook Movemail
 
Join Date: Nov 2005
Location: Indiana
Posts: 5
Microchip is on a distinguished road
Default I Love You!

Hurricane, thank you SO Much for that help. I was stuck on this for weeks. Support and assistance like yours will go a long way, because as I learn from generous people like you, I, too, will be able to share what I have learned.

People like you make this world a better place.

Thanks again!!!
Dave
Reply With Quote
  #6  
Old 11-21-2005, 07:49 PM
Matrix's Avatar
Matrix Matrix is offline Microsoft Outlook Movemail Windows 10 Microsoft Outlook Movemail Office 2010
Admin
 
Join Date: Jan 2005
Posts: 429
Matrix is on a distinguished road
Default

Glad it helps.
Reply With Quote
  #7  
Old 11-21-2005, 08:08 PM
Microchip Microchip is offline
Novice
Microsoft Outlook Movemail
 
Join Date: Nov 2005
Location: Indiana
Posts: 5
Microchip is on a distinguished road
Default Next question

How do I run that program without going into the Visual Basic editor every time? I don't want it to run automatically, becaus eit never knows when the last e-mail is coming in. I want to run it after I see all of the e-mail finishing. Can I hook it up to a macro name, an dthen a function key, perhaps?

Thanks!
Reply With Quote
  #8  
Old 11-21-2005, 08:23 PM
Matrix's Avatar
Matrix Matrix is offline Microsoft Outlook Movemail Windows 10 Microsoft Outlook Movemail Office 2010
Admin
 
Join Date: Jan 2005
Posts: 429
Matrix is on a distinguished road
Default

You can run a macro from the outlook with the menu "Tools | Macro | Macros...", select the macro in the list.

An easier way, you can add the macro to the toolbar:
1. Select the menu "Toolbars | Customize".
2. Switch to the Commands tab.
3. Under Categories, select Macros.
4. Drag the macro to the desired toolbar.
4. Right-click on the new toolbar button, then change the Name or button appearance as desired.
Reply With Quote
  #9  
Old 11-21-2005, 09:17 PM
Microchip Microchip is offline
Novice
Microsoft Outlook Movemail
 
Join Date: Nov 2005
Location: Indiana
Posts: 5
Microchip is on a distinguished road
Talking Nice!

Very slick. Thank you again! I am impressed.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Microsoft Licensing LizzyRut Office 0 10-14-2008 02:19 PM
Microsoft calendar 6 pljames Misc 0 05-31-2008 05:33 PM
Microsoft Outlook Movemail Microsoft Outlook 2000 Problem Wiily Outlook 1 04-14-2006 02:06 AM
Microsoft Outlook Movemail Microsoft Outlook Slow to Task Mgr peak100% MICH Office 2 11-27-2005 03:36 AM
Microsoft Outlook Movemail Microsoft Frontpage sufi Office 2 10-29-2005 07:56 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:20 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft