dcsimg
Results 1 to 4 of 4
  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2018
    Posts
    2

    Need help with macro to sort emails into folder by body string

    I am new to this whole thing and was just looking for a macro that is able to go through a certain folder and if it finds a specific string of text in the body to move emails containing that string to a different folder.

    I tried to splice a macro that moves the emails into one that reads the text of the body but I get an error 91 when I try to run the macro. Any help would be appreciated! (not sure if this is the correct place to send these questions)

    Code:
    Sub AutomateMoveWithSearchString()
        Dim myInspector As Outlook.Inspector
        Dim myObject As Object
        Dim myItem As Outlook.MailItem
        Dim myDoc As Word.Document
        Dim mySelection As Word.Selection
        Dim strItem As String
        Dim strGreeting As String
        Dim objNS As Outlook.NameSpace
        Dim objDestFolder As Outlook.MAPIFolder
        Dim objItem As Outlook.MailItem
                
        Set myInspector = Application.ActiveInspector
        Set myObject = myInspector.CurrentItem
        Set objNS = Application.GetNamespace("MAPI")
        Set objDestFolder = objNS.GetDefaultFolder(olFolderInbox).Folders("Correspondence")
        Set objItem = Application.ActiveInspector.CurrentItem
        
        
        'The active inspector is displaying a mail item.
        If myObject.MessageClass = "IPM.Note" And myInspector.IsWordMail = True Then
            Set myItem = myInspector.CurrentItem
            'Grab the body of the message using a Word Document object.
            Set myDoc = myInspector.WordEditor
            myDoc.Range.Find.ClearFormatting
            Set mySelection = myDoc.Application.Selection
            With mySelection.Find
                .Text = "Other Information (e.g., current Trend customer & products, what competitor are they using, etc)"
                .Replacement.Text = ""
                .Forward = True
                .Wrap = wdFindContinue
                .Format = False
                .MatchCase = False
                .MatchWholeWord = False
                .MatchAllWordForms = False
                .MatchSoundsLike = False
                .MatchWildcards = True
            End With
            If mySelection.Find.Execute = True Then
                 objItem.Move objDestFolder
                 Set objDestFolder = Nothing
                 Set objNS = Nothing
                End If
            Else
                MsgBox "There is no data in this message."
            End If
    End Sub

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    23,543

    Re: Need help with macro to sort emails into folder by body string

    but I get an error 91
    on what line? and what is the error description?

    the above code would require microsoft word to be the default editor for emails, and only works with an email open in an inspector window

    maybe try like
    Code:
    Dim objdestfolder As MAPIFolder
    Dim msg As MailItem, strtomatch As String
    Set objdestfolder = objNS.GetDefaultFolder(olFolderInbox).Folders("Correspondence")
    strtomatch = "some string to match"  'change to suit
    For Each msg In ActiveExplorer.Items
        If InStr(msg.HTMLBody, strtomatch) > 0 Then
            msg.Move objdestfolder
        End If
        
    Next
    this should work through all the messages in the folder currently displayed in outlook
    it would be easy to adapt to a specific folder instead
    it will not change anything within the message, just move it or not
    i did not test this at all, so test with care
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2018
    Posts
    2

    Re: Need help with macro to sort emails into folder by body string

    I seem to be getting an error on line 4.
    The error is as follows:
    Code:
    Run Time Error '424' object required

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    23,543

    Re: Need help with macro to sort emails into folder by body string

    i did spot that later, it was something i copied from your original code, change to
    Code:
    Set objdestfolder = Session.GetDefaultFolder(olFolderInbox).Folders("Correspondence")
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width


×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.