I found A question addresing this concern similar to mine here;
How can I move Mails Items from Outlook Inbox with specific subject to specific folder/sub folder?
The first Module- The first part of this code- i have exported all email data to the spreadsheet successfully.
The Second Module- I would like to instruct Excel VBA to Move Emails in the main Folder to a subfolder based on datasets i typed out in the spreadsheet ( it will not be based on a filter/Criteria of the emails itself,just its unique subject tittle).
In Column (c), is the subject of the email (All of the subject tittles are specific/unique) and in column (h), i have detailed the name of the sub-folder where i would like to have it moved too. Unfortunately, i have an error while executing the code i created.
I am a beginner in Excel VBA and dont have the best understanding.I got an idea of my code based on different sources, if its incorrect do let me know, it will be greatly appreciated
Thank you.
- i Tried executing the code i found in the questioned asked by the user similar to mine, however it didnt work
Sub MovingEmails_Invoices()
'Declare your Variables
Dim items As Outlook.items
Dim subfolder As Outlook.Folder 'this will be the folder you want to move the Mail to
'Set Outlook Inbox Reference
Set OP = New Outlook.Application
Set NS = OP.GetNamespace("MAPI")
Set Mail = OP.CreateItem(olMailItem)
'To loop through subfolder and its folders
Set rootfol = NS.Folders("SYNTHES-JNJCZ-GBS.DE.AT.CH@ITS.JNJ.com")
Set Folder = rootfol.Folders("Austria")
'The list for invoice number should be dynamic
Dim arraysearch(1 To 1000) As String
Dim i As Long
i = UBound(arraysearch)
arraysearch(i) = Range("C2").offset(i, 0).Value
If i = 0 Then
MsgBox "error"
Exit Sub
End If
'The list for folder type should be dynamic
Dim arraymove(1 To 1000) As String
i = UBound(arraymove)
arraymove(i) = Range("H2").offset(i, 0).Value
If i = 0 Then
MsgBox "error"
Exit Sub
End If
'specific folders for the mail to move to
Set subfolder = rootfol.Folders(arraymove(i))
For Each Mail In Folder.items.Restrict("[Subject] >= arraysearch(i)")
If arraysearch(i) = arraymove(i) Then
item.Move subfolder
End If
Next Mail
End Sub