VBA - Moving a mail from inbox to a specific folder

Frequent Contributor

Hi every One, greetings

 

I have to do the following.

 

I need a vba for, moving a mail in outlook inbox to a specified folder in outlook based on either Sender name or sender mail or mail subject, commanding from excel. 

I have browsed for this all over internet, and found only outlook VBAs, which I think it will create security issues. .. 

 

Could any "Master Minds in VBA"  help on this????

20 Replies

@Sameer_Kuppanath_Sultan 

I cannot follow your plan, probably from the translation.

It would be beneficial if you insert a file (without sensitive data), if necessary, and explain your plans on the basis of this file.

 

An additional advantage of getting a quick and precise solution proposal here is if you would send us your Excel version and your operating system.

 

Nonetheless, yesterday I had a topic that was similar, attached the file that I sent, maybe it fits into your plan.

 

I would be happy to know if I could help.

 

Nikolino

I know I don't know anything (Socrates)

 

* Kindly Mark and Vote this reply if it helps please, as it will be beneficial to more Community members reading here.

@NikolinoDE  Hi- Thanks for coming up for helping me. 

 

I have attached the code herewith-please find it.

 

this code will help me to save the attachment from outlook to a specific folder in my PC.

 

What I need is, to move the mail in outlook inbox to a folder in outlook after clicking this VBA. 

 

So ultimately, I will save the attachment and will move this to a folder in outlook. so no open files inbox to attend!!!!

 

 

 

Hey Master Minds

 

Please help me on this. 

 

Just to describe again,

 

I have attached the code herewith-please find it.

 

this code will help me to save the attachment from outlook to a specific folder in my PC.

 

What I need is, to move the selected mail in outlook inbox to a folder in outlook after clicking this VBA. 

 

So ultimately, I will save the attachment and will move this mail to a folder in outlook. so no mails inbox to attend!!!!

 

 

@Sameer_Kuppanath_Sultan 

 

I think that this will solve your problem.

 

MailItem.Move method (Outlook)

https://docs.microsoft.com/en-us/office/vba/api/outlook.mailitem.move

 

Thank you for your understanding and patience

 

Wish you a nice day.

 

Nikolino

I know I don't know anything (Socrates)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

I tried that, but showing the below screen shot and yellow highlighted the code below.

Please note: I did it in Excel VBA

@NikolinoDE 

 

Sameer_Kuppanath_Sulta_0-1606372720484.png

Sameer_Kuppanath_Sulta_1-1606372776122.png

 

@Sameer_Kuppanath_Sultan 

I don't think I'm getting any further with Outlook.

That's why I forwarded your question to the Outlook group / community.
Maybe it needs some settings in Outlook, which I don't know.
Whatever the case, let's wait for the response and then we'll see.

 

Question Link:

https://techcommunity.microsoft.com/t5/outlook/auto-move-the-selected-mail-in-outlook-inbox-to-a-sel...

 

Sorry that I haven't been able to offer you a solution until now.

 

 

Thx for your Tiem and patience

 

Nikolino

I know I don't know anything (Socrates)

@Sameer_Kuppanath_Sultan 

 

Maybe this approach could help you resolve your issue.

Please adjust in your code

 

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
If TypeOf Item Is Outlook.MailItem Then
SaveSentMail Item
End If
End Sub

Private Sub SaveSentMail(Item As Outlook.MailItem)
Dim Inbox As Outlook.MAPIFolder
Dim Subfolder As Outlook.MAPIFolder

If Item = "Nikolino" Then
Set Inbox = Application.Session.GetDefaultFolder(5)
Set Subfolder = Inbox.folders("0200 Nikolino").folders("General")
Set Item.SaveSentMessageFolder = Subfolder
End If

 

 

Sorry that I haven't been able to offer you a quick, acceptable solution until now.

 

Will keep you updated

Thank you for your patience and time.

 

Nikolino

I know I don't know anything (Socrates)

 

@NikolinoDE is this an outlook code or to paste in excel vba??

 

 

@NikolinoDE It will not work. Could you paste the whole code then. 

@Sameer_Kuppanath_Sultan 

 

I was also able to pick this up.

From one folder to another.

 

Thank you for your understanding and patience

 

 

Nikolino

I know I don't know anything (Socrates)

@NikolinoDE 

still i am facing this highlighted area, with an error message

 

Sameer_Kuppanath_Sulta_2-1606624411072.png

 

Sameer_Kuppanath_Sulta_1-1606624361346.png

 

 

Actually i want to move from Inbox to client a mail based on Sender email Address.!!!

 

Sameer_Kuppanath_Sulta_0-1606624237708.png

 

Which version of Excel do you have?
(File -> Account -> Info on Excel ... press the question mark and the version description appears). Which operating system do you have?
(If Windows, then please type in the search winver and press enter, a window will appear with the exact description of your Windows version).

Sie müssen den Dateipfad nach Ihren nöten einstellen, so wie es ist, wird die sicherungsdatei in C:\ abgespeichert.
Please always activate macros and content when opening the file.

@NikolinoDE 

 

as additional info:

Xiant Filer: https://www.softpedia.com/get/Office-tools/Other-Office-Tools/Xiant-Filer.shtml

VBOffice SAM: http://www.vboffice.net/en/products/sam

 

*The third-party products that this article discusses are manufactured by companies that are independent of me. I makes no warranty, implied or otherwise, about the performance or reliability of these products.

 

Thank you for your patience and time.

 

Nikolino

I know I don't know anything (Socrates)

@NikolinoDE 

 

Version: 

Sameer_Kuppanath_Sulta_0-1606739765275.pngSameer_Kuppanath_Sulta_1-1606739789997.png

 

@Sameer_Kuppanath_Sultan 

As far as I could see it is difficult for you to do this in Excel with VBA.

Is also understandable, since VBA has a lot of manual cuts / code changes so that it functions as it should be in the respective environment.

Therefore I would like to offer you this suggested solution via Outlook.

Here, the sent emails are moved by the Outlook rules.

 

Move sent items to other Outlook folders.

When you send a message, a copy of the message is moved to the Sent Items folder.

However, this is not necessarily useful if you e.g.

Collect your correspondence with different people / companies etc. in different folders.

You then always have to look for the copy from the "Sent Items" folder

and then move it into the respective folder.

Guess this is your problem.

 

I would like to show you a solution with Outlook to the problem.

Go to Outlook.

You must now first switch off the send copy function.

To do this, go to the Tools / Options menu - Settings tab in Outlook and click the E-Mail Options button.

Now a new window will open, remove the check mark under “Save message copies in the Sent Items folder” and then close all windows using the OK button.

 

Now you need to define a rule.

To do this, go to the Tools / Rules and Notifications menu in Outlook - E-Mail Rules tab and click here

Click the "Create rule without template" button.

Now select Check messages after sending and then click Next.

In the next window, set an account name for "via account" and now you have to select an account in the rule description (below).

Then click Next.

Now put a tick under “Move a copy of this to the target folder” and select an appropriate folder.

Now a predefined account will be sent and saved in the specified folder.

 

If you want to sort your sent messages by person, repeat steps 1 and 2 and put a tick under “sent to a person / distribution list” in the conditions. Select the name from the address book / distribution list.

 

Warning: All outgoing messages are moved to the specified folder and are no longer stored in the "Sent items".

 

I hope that I was able to help you a bit and I wish you every success with your project.

 

 

Wish you a nice day 

 

Thank you for your patience and time.

 

Nikolino

I know I don't know anything (Socrates)

HI @NikolinoDE 

 

FINALYY I HAVE FOUND THE CODE, CHECK IT. 

 

Option Explicit

Sub INC_Data()
    Dim ol As Object        'Outlook.Application
    Dim ns As Object        'Outlook.Namespace
    Dim inboxFol As Object  'Outlook.Folder
    Dim subFol As Object    'Outlook.Folder
    Dim itm As Object
    Dim mi As Object        'Outlook.MailItem
    Dim att As Object       'Outlook.Attachment
    Dim fso As Object       'Scripting.FileSystemObject
    Dim dirName As String
     
    'Some Set Ups
    Set fso = CreateObject(Class:="Scripting.FileSystemObject")
    Set ol = CreateObject(Class:="Outlook.Application")
    Set ns = ol.GetNamespace("MAPI")
    Set inboxFol = ns.GetDefaultFolder(6) 'olFolderInbox
    Set subFol = inboxFol.Folders("Client")
    
    dirName = "D:\XYZ"
    If Not fso.FolderExists(dirName) Then
        fso.CreateFolder dirName
    End If

    'Finding the search item from Oulook Inbox
    For Each itm In inboxFol.Items
        If itm.Class = 43 Then
            Set mi = itm
            If mi.Attachments.Count > 0 And InStr(mi.SenderEmailAddress, "xxxxxxx@inc.ae") Then
                'Saving Attachments to a folder
                For Each att In mi.Attachments
                    If Right(att.Filename, 4) = "xlsm" Then
                        att.SaveAsFile dirName & "\" & Range("Ad2").Text & ".xlsm"
                    End If
                Next att
                'Move mail item to subfolder
                mi.Move subFol
            End If
        End If
    Next itm
End Sub
Do you activate this VBA code from Outlook directly or via Excel?