PDA

View Full Version : Search and Move to folder in outlook



jinesh1986
09-30-2008, 06:38 AM
Dear Friends,

Could any one help me with the above?
I want to pass a string from excel to outlook. Search in the inbox and if found move to a perticular folder which not a subfolder of inbox.

Any help will be much appreciated.

Thanks
Jinesh

Mavyak
09-30-2008, 08:26 PM
Are you searching subject, body, or both? When you say the folder you want to move it to is not a sub-folder of the inbox does that mean it's not a MAPI folder (e.g. a folder on your hard-drive) or it's just not a sub-folder of the inbox? Are you searching just the inbox for these emails or all folders in Outlook?

jinesh1986
10-01-2008, 01:03 AM
Dear Mavyak,

Thanks for replying so fast.

1. I want to search only the subject line
2. The folder to which I want to move the searched email is an MAPI folder but it's not a subfolder of Inbox but a General folder in my Mailbox.
3. I just want to search the inbox only.

Thanks a Ton Mavyak for your willingness to help.

Regards
Jinesh

Mavyak
10-01-2008, 04:32 AM
Option Explicit
Sub FInd_And_Move_Emails()
Dim Inbox As MAPIFolder
Dim DestinationFolder As MAPIFolder
Dim mi As MailItem
Dim InboxItem As Object
Dim StringToFind As String
Dim yn As Integer

yn = vbNo
While yn = vbNo
StringToFind = InputBox("Enter the text to search for:", "Find", "NewEngland Clam Chowder")
yn = MsgBox("You entered:" & vbCr & vbCr & StringToFind & vbCr & vbCr & "Is that correct?", vbYesNoCancel, "Confirm Search Criteria")
Wend
If yn = vbCancel Then GoTo ExitSub
yn = vbNo

Set Inbox = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)

While DestinationFolder Is Nothing Or yn = vbNo
Set DestinationFolder = Application.GetNamespace("MAPI").PickFolder()
If DestinationFolder Is Nothing Then
yn = MsgBox("You cancelled the selection. Would you like to try again?", vbYesNo, "No Folder Selected")
If yn = vbNo Then GoTo ExitSub
Else
If DestinationFolder.EntryID = Inbox.EntryID And DestinationFolder.StoreID = Inbox.StoreID Then
MsgBox "The source folder cannot be the same as the destination folder.", vbOKOnly, "Move from Point A to Point A?"
Else
yn = MsgBox("You selected folder:" & vbCr & vbCr & DestinationFolder.Name & vbCr & vbCr & "Ist that correct?", vbYesNoCancel, "Confirm Folder Selection")
If yn = vbCancel Then
GoTo ExitSub
ElseIf yn = vbNo Then
Set DestinationFolder = Nothing
End If
End If
End If
Wend

For Each InboxItem In Inbox.Items
If Not mi Is Nothing Then mi.Move DestinationFolder
Set mi = Nothing

If InboxItem.Class = olMail Then
If InStr(UCase(InboxItem.Subject), UCase(StringToFind)) > 0 Then
Set mi = InboxItem
End If
End If
Debug.Print InboxItem.Subject
Next InboxItem

If Not mi Is Nothing Then mi.Move DestinationFolder

MsgBox "Process Complete."
ExitSub:
Set mi = Nothing
Set InboxItem = Nothing
Set DestinationFolder = Nothing
Set Inbox = Nothing
End Sub


That works in Outlook2002. If this is a recurring thing you could probably set up a rule via the Rules wizard to do this for you instead.

Enjoy!

Mavyak

Mavyak
10-01-2008, 09:08 AM
I have copy pasted your code in outlook and it worked. But i need to avoid user intervention and pass the search string from Excel.

i.e call this function from excel. I have coded the whole program in an Excel file.

My process is

1. Search for the email for string passed from excel
2. Save the attachment in a folder (in the drive)
3. Print the attachment
4. Move the email to "Actoned" Folder in outlook

I am done with 1 & 2.
Can you help me with 3 & 4?
#3
I don't know how to print attachments. My guess would be that it will involve automation which means the host program will have to support automation and you will have to test the file extension to determine which program to automate. If the attachment is always a particular Office file (Excel, Word, etc.) then I might be able to help you out with #3 after all.

#4
You will need to get the folder's EntryId and StoreId and hard-code it into the Excel code. To do that, perform the following steps:

1. Open the Actoned folder in MS Outlook
2. Open any email in the folder (if no email exists in folder, put one there and open it)
3. Open the VBE by hitting Alt+F11
4. If the Immediate window is not visible, hit Ctrl+G to make it visible
5. In the Immediate Window enter the the following two commands:
?activeinspector.currentitem.parent.entryid
?activeinspector.currentitem.parent.storeid
Post back when you have those values and I'll knock out the code for you.
I may not get to it until this evening as I'm at work right now.

Mav

p.s. Please keep all communications on the board. That way people with the same problem you have now will be able to find an answer.

jinesh1986
10-06-2008, 04:05 AM
Hi Mavyak,

I have made something to select the folder automatically. See below:

Set Defaultfolder = Inbox.folders("Actoned")

this worked well. Thanks for your help though.

Reg Printing:

Please note that almost 80 % of attachments are tif and rest PDF.
I think I will have to add a code which identifies the last three characters (File Ext) of the document name & then call a printing function accordingly.
Like if it's tif......Call printtif() or if it is PDF call pdfprint().

Could you make a function for printing tif & PDF and a function for identifying the document extension.

Reg PS. Sorry for replying seperatly. I thought to do so because the pgm you have coded worked well and never wanted to give an impression that there was something wrong with it. Any way I will keep this in Mind.

Thanks Mavyak.

Regards
Jinesh



#3
I don't know how to print attachments. My guess would be that it will involve automation which means the host program will have to support automation and you will have to test the file extension to determine which program to automate. If the attachment is always a particular Office file (Excel, Word, etc.) then I might be able to help you out with #3 after all.

#4
You will need to get the folder's EntryId and StoreId and hard-code it into the Excel code. To do that, perform the following steps:

1. Open the Actoned folder in MS Outlook
2. Open any email in the folder (if no email exists in folder, put one there and open it)
3. Open the VBE by hitting Alt+F11
4. If the Immediate window is not visible, hit Ctrl+G to make it visible
5. In the Immediate Window enter the the following two commands:
?activeinspector.currentitem.parent.entryid
?activeinspector.currentitem.parent.storeid
Post back when you have those values and I'll knock out the code for you.
I may not get to it until this evening as I'm at work right now.

Mav

p.s. Please keep all communications on the board. That way people with the same problem you have now will be able to find an answer.

Mavyak
10-08-2008, 04:42 PM
I don't know how to automate the printing of those types of files. If anyone else does, feel free to usurp this thread.