Consulting

Results 1 to 8 of 8

Thread: Outlook macro not able to loop through all emails in Inbox

  1. #1
    VBAX Newbie
    Joined
    Nov 2022
    Posts
    2
    Location

    Outlook macro not able to loop through all emails in Inbox

    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/...-sender-domain

    and have posted a thread from this link: https://learn.microsoft.com/en-us/an...or-with-b.html but not able to get any solution

  2. #2
    VBAX Regular
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    35
    Location
    You have this solved?
    How to attach file: How to upload your attachments (vbaexpress.com) To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Moderator VBAX Guru Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    4,240
    Location
    @June7, sadly I believe not given the intent of the last 2 lines of hrq's post.
    Remember To Do the Following....
    Use tags when posting code to the thread,
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  4. #4
    VBAX Regular
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    35
    Location
    But the cross-posted thread does indicate resolved.
    How to attach file: How to upload your attachments (vbaexpress.com) To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    Moderator VBAX Guru Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    4,240
    Location
    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".
    Remember To Do the Following....
    Use tags when posting code to the thread,
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  6. #6
    VBAX Newbie
    Joined
    Nov 2022
    Posts
    2
    Location
    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.

  7. #7
    Moderator VBAX Guru Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    4,240
    Location
    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.
    Remember To Do the Following....
    Use tags when posting code to the thread,
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  8. #8
    Moderator VBAX Guru Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    4,240
    Location
    @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
    Remember To Do the Following....
    Use tags when posting code to the thread,
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •