Forum Discussion
VBA - Moving a mail from inbox to a specific folder
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- Hemant007Sep 10, 2022Copper ContributorHi, Did you got solution/code for your issue ?
I am also searching for same code.... - Sameer_Kuppanath_SultanMay 11, 2021Brass ContributorFrom Excel
- NikolinoDEJan 30, 2021Platinum ContributorDo you activate this VBA code from Outlook directly or via Excel?