none
Open email, search for text, delete X number of lines and/or paragraphs? RRS feed

  • Question

  • How would one go about this?

    I have existing code that uses the objActionsMenu.Execute to "resend" the message, essentially creating a new email that I can edit.

    I would like to be able to:

    1) Search for a keyword

    2) Move to the end of the line and/or paragraph

    3) Delete the contents

    The reason is that I often receive important emails with 50+ lines of names and email addresses on the "To:" or "CC:" lines. I want to quickly and easily remove these so I can get to the important part of the email.

    Am I going about it the right way? If so, how would I accomplish this? Thanks.

        
    Tuesday, February 3, 2015 3:32 PM

Answers

  • Hello,

    You can automate Outlook to get the job done. Outlook uses Word as an email editor. So, you can use all properties and methods available in the Word object model. The WordEditor property returns an instance of the Document class which represents the message body. You can read more about that and all three possible ways of working with bodies in the Chapter 17: Working with Item Bodies .

    Also you may find the How to automate Outlook from another program article helpful.

    Are you going to develop an Outlook add-in or standalone application?
    Tuesday, February 3, 2015 3:50 PM
  • How would one go about this?

    I have existing code that uses the objActionsMenu.Execute to "resend" the message, essentially creating a new email that I can edit.

    I would like to be able to:

    1) Search for a keyword

    2) Move to the end of the line and/or paragraph

    3) Delete the contents

    The reason is that I often receive important emails with 50+ lines of names and email addresses on the "To:" or "CC:" lines. I want to quickly and easily remove these so I can get to the important part of the email.

    Am I going about it the right way? If so, how would I accomplish this? Thanks.

        

    I think you maybe in the wrong forums :). This is for Excel and not Outlook. This is a general VBA question though. The easiest way to do this is the following:

    -Assign the content of the email body to a string variable.

    -Use the split function to split at the keyword, and keep the string portion BEFORE the keyword.

    -Assign the split string back to the email body so something like this:

    Public Sub ReplyWithAttachments() ' Ensure reference to Word Object model is set
        Dim rpl As Outlook.MailItem
        Dim itm As Object
        Set itm = GetCurrentItem()
        If Not itm Is Nothing Then
            Set rpl = itm.Reply
            rpl.BodyFormat = olFormatHTML
            rpl.display
        End If
        Dim objDoc As Word.Document
        Set objDoc = rpl.GetInspector.WordEditor
        Dim txt As String
        txt = objDoc.Windows(1).Document.Range.text
        txt = Split(txt, "To")(0) '
        objDoc.Windows(1).Document.Range.text = txt
    End Sub
    Function GetCurrentItem() As Object
        Dim objApp As Outlook.Application
        Set objApp = Application
        On Error Resume Next
        Select Case TypeName(objApp.ActiveWindow)
        Case "Explorer"
        Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
        Case "Inspector"
        Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
        End Select
        GetCurrentItem.UnRead = False
        Set objApp = Nothing
    End Function


    Tuesday, February 3, 2015 3:51 PM

All replies

  • I should add that copying & pasting the email body into Excel or Word would be acceptable as well.
    Tuesday, February 3, 2015 3:48 PM
  • Hello,

    You can automate Outlook to get the job done. Outlook uses Word as an email editor. So, you can use all properties and methods available in the Word object model. The WordEditor property returns an instance of the Document class which represents the message body. You can read more about that and all three possible ways of working with bodies in the Chapter 17: Working with Item Bodies .

    Also you may find the How to automate Outlook from another program article helpful.

    Are you going to develop an Outlook add-in or standalone application?
    Tuesday, February 3, 2015 3:50 PM
  • How would one go about this?

    I have existing code that uses the objActionsMenu.Execute to "resend" the message, essentially creating a new email that I can edit.

    I would like to be able to:

    1) Search for a keyword

    2) Move to the end of the line and/or paragraph

    3) Delete the contents

    The reason is that I often receive important emails with 50+ lines of names and email addresses on the "To:" or "CC:" lines. I want to quickly and easily remove these so I can get to the important part of the email.

    Am I going about it the right way? If so, how would I accomplish this? Thanks.

        

    I think you maybe in the wrong forums :). This is for Excel and not Outlook. This is a general VBA question though. The easiest way to do this is the following:

    -Assign the content of the email body to a string variable.

    -Use the split function to split at the keyword, and keep the string portion BEFORE the keyword.

    -Assign the split string back to the email body so something like this:

    Public Sub ReplyWithAttachments() ' Ensure reference to Word Object model is set
        Dim rpl As Outlook.MailItem
        Dim itm As Object
        Set itm = GetCurrentItem()
        If Not itm Is Nothing Then
            Set rpl = itm.Reply
            rpl.BodyFormat = olFormatHTML
            rpl.display
        End If
        Dim objDoc As Word.Document
        Set objDoc = rpl.GetInspector.WordEditor
        Dim txt As String
        txt = objDoc.Windows(1).Document.Range.text
        txt = Split(txt, "To")(0) '
        objDoc.Windows(1).Document.Range.text = txt
    End Sub
    Function GetCurrentItem() As Object
        Dim objApp As Outlook.Application
        Set objApp = Application
        On Error Resume Next
        Select Case TypeName(objApp.ActiveWindow)
        Case "Explorer"
        Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
        Case "Inspector"
        Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
        End Select
        GetCurrentItem.UnRead = False
        Set objApp = Nothing
    End Function


    Tuesday, February 3, 2015 3:51 PM
  • John,

    > I think you maybe in the wrong forums :). This is for Excel and not Outlook.

    This is the right forum for Outlook specific question. Check out the forum title at the top.

    Tuesday, February 3, 2015 4:11 PM
  • John,

    > I think you maybe in the wrong forums :). This is for Excel and not Outlook.

    This is the right forum for Outlook specific question. Check out the forum title at the top.

    oops my mistake :). I was in the Excel forums. Unless this thread got moved :)
    Tuesday, February 3, 2015 4:32 PM