Log in

View Full Version : Outlook macro not able to loop through all emails in Inbox



hrq
11-20-2022, 11:04 PM
I would like the code to scan through all mails in my Inbox folder (excluding subfolders) to look out for specific domains.

I don't need a folder to be created if destFolder does not exist but currently the code is not able to read all my emails in my Inbox except for one read email dated in 6 months ago.

i managed to get some referencing on the SMTP code from this link: https://stackoverflow.com/questions/56958257/how-to-move-email-to-folder-based-on-the-sender-domain

and have posted a thread from this link: https://learn.microsoft.com/en-us/answers/questions/1096208/error-34runtime-error-91-object-variable-or-with-b.html but not able to get any solution

June7
11-21-2022, 11:55 AM
You have this solved?

Aussiebear
11-21-2022, 02:19 PM
@June7, sadly I believe not given the intent of the last 2 lines of hrq's post.

June7
11-21-2022, 02:55 PM
But the cross-posted thread does indicate resolved.

Aussiebear
11-21-2022, 04:53 PM
So did ours here for some reason. Since no solution has been either referenced elsewhere nor has it published here, I have returned the status to "unsolved".

hrq
11-21-2022, 08:08 PM
i wanted to post the code, but i am getting an error. Wrote a simple html code for it, i cant even paste the link as well. i try to providing an attachment, i cant attached as well.

Aussiebear
11-21-2022, 09:02 PM
For Files, Click on Go Advanced/ Manage attachments and follow the process from there to attach files. Being a new member there are some restrictions on posting regarding links. As to the error when posting code, you have to explain that one for us.

Aussiebear
11-22-2022, 12:44 AM
@hrq. Here is your code


Option Explicit
Sub moveemailToFolder()
Dim strSenderDomain As String
Dim strSenderEmailAddress As String
Dim objDestFolder As Folder
Dim Selection As Selection
Dim obj As Object
Dim NS As NameSpace
Dim inboxFolder As Outlook.MAPIFolder
Dim iCount As Integer
Dim moveEmail As Boolean
Set NS = GetNamespace("MAPI")
Set inboxFolder = NS.GetDefaultFolder(olFolderInbox)
iCount = 0 'count no. of emails moved
For Each obj In inboxFolder.Items
On Error Resume Next
If obj.SenderEmailType = "EX" Then
' exchange
strSenderEmailAddress = obj.GetExchangeUser.PrimarySmtpAddress
Else
' smtp
strSenderEmailAddress = obj.SenderEmailAddress
End If
strSenderDomain = Mid$(strSenderEmailAddress, InStrRev(strSenderEmailAddress, "@") + 1, _
InStrRev(strSenderEmailAddress, ".") - _
InStrRev(strSenderEmailAddress, "@") - 1)
Set objDestFolder = NS.GetDefaultFolder(olFolderInbox).Folders("shipping")
'you need to create a folder first
If strSenderDomain Like "*epshipping*" Then 'or If not strSenderDomain Like "*shipping*"
obj.Move objDestFolder
moveEmail = True
Debug.Print obj.SenderEmailType & " " & strSenderEmailAddress & " " & strSenderDomain
End If
If moveEmail Then
iCount = iCount + 1
moveEmail = False
End If
Next obj
Set NS = Nothing
Set objDestFolder = Nothing
Set inboxFolder = Nothing
MsgBox ("Total no. of emails moved over: " & iCount)
End Sub