+ Reply to Thread
Results 1 to 9 of 9

How Do You Count Text Occurrences in Excel AND Export to the Body of an Outlook Email?

Hybrid View

  1. #1
    Registered User
    Join Date
    02-17-2014
    Location
    New York
    MS-Off Ver
    Office 365
    Posts
    34

    How Do You Count Text Occurrences in Excel AND Export to the Body of an Outlook Email?

    Hello Excel-sperts,

    I am responsible for summarizing a daily excel report every morning by sending the info out in the body of an Outlook email.

    1. Is that possible to do by, say, importing the number in one cell. For example, cell A1=12. Could I somehow link an outlook template to that worksheet so that:
    "Good morning all, today we had [A1] new donors to our charity."

    2. In my report, there is one column that may have any word in it. I would like to identify all WORDS that occur in 15 or more cells in that column. So in my email, if the words 'apple', banana' and 'orange' occur 15, 17 and 12 times respectively, I would like to be able to export that to the body of my email in this manner (no need for oranges as it occurs less than 15):

    "[15 Apples] donated to the charity
    "[17 Bananas] donated to the charity"

    3. Finally, how would I count the number of times a specific word occurs in a column as opposed to the above where I don't know what words I'm necessarily looking for? So for example: 'Yesses' and 'No's"
    "[# of Yes] said they would attend the benefit dinner"
    "[# of No] said they would NOT attend the benefit dinner."

    Thank you so much for your help in advance. I hope this is clear and concise.
    rhexcel

  2. #2
    Valued Forum Contributor natefarm's Avatar
    Join Date
    04-22-2010
    Location
    Wichita, Kansas
    MS-Off Ver
    2016
    Posts
    1,020

    Re: How Do You Count Text Occurrences in Excel AND Export to the Body of an Outlook Email?

    2. In my report, there is one column that may have any word in it. I would like to identify all WORDS that occur in 15 or more cells in that column. So in my email, if the words 'apple', banana' and 'orange' occur 15, 17 and 12 times respectively, I would like to be able to export that to the body of my email in this manner (no need for oranges as it occurs less than 15):
    Will this word list be sorted? Unless you would be starting with a specific word list to work with, it would be easier to count words if they were sorted. Then you could just go through the rows and increment the count until the word changes.
    Acts 4:12
    Salvation is found in no one else, for there is no other name under heaven given to mankind by which we must be saved.

  3. #3
    Registered User
    Join Date
    02-17-2014
    Location
    New York
    MS-Off Ver
    Office 365
    Posts
    34

    Re: How Do You Count Text Occurrences in Excel AND Export to the Body of an Outlook Email?

    natefarm,
    First, thank you so much for your response. Yes, I could pre-sort the list. But what do you mean by "go through the rows and increment the count..." Right now I am sorting the list and selecting all of the words that are the same and then looking at the bottom right of the screen at the counter. It's a lot of work for something that I should (in theory) be able to do in seconds. Thank you again!

  4. #4
    Valued Forum Contributor natefarm's Avatar
    Join Date
    04-22-2010
    Location
    Wichita, Kansas
    MS-Off Ver
    2016
    Posts
    1,020

    Re: How Do You Count Text Occurrences in Excel AND Export to the Body of an Outlook Email?

    This seems to work. I already had the SendMAPIMessage subroutine in place in an app of my own, so I just had to write the BuildMessage part.

    In the VBA window, you'll need to go to Tools - References and select the reference to Microsoft Outlook xx.0 Object Library to make it work. They're in alpha order.

    Good luck!

    Option Explicit
    Dim Rw As Long, Col As Long, Cnt1 As Long, Cnt2 As Long
    Dim Recipient As String, CCRecipient As String, BodyTxt As String
    Dim NewLine As String
    Const AddressExt = "@yourcompany.com"
    
    Sub BuildMessage()
        NewLine = Chr(10)
        Recipient = "thisone; thatone" ' This could be built from your spreadsheet as well
        CCRecipient = "someoneelse"
        BodyTxt = "Good morning all, today we had " & Range("A1").Value & " new donors to our charity."
        BodyTxt = BodyTxt & NewLine & NewLine ' Double-space
    
        Rw = 1
        Col = 4  ' Word column
        Cnt1 = 0
    
        ' This will only work if the words are sorted
        Do Until Cells(Rw, Col).Value = ""
            If Cells(Rw, Col).Value = Cells(Rw + 1, Col).Value Then
                Cnt1 = Cnt1 + 1
            Else
                If Cnt1 > 14 Then
                    BodyTxt = BodyTxt & Cnt1 & " " & Cells(Rw, Col).Value & " donated to the charity."
                    BodyTxt = BodyTxt & NewLine
                End If
                Cnt1 = 0
            End If
            Rw = Rw + 1
        Loop
    
        Rw = 1
        Col = 6 ' Yes/No column
        Cnt1 = 0
        Cnt2 = 0
    
        Do Until Cells(Rw, Col).Value = ""
            If UCase(Cells(Rw, Col).Value) = "YES" Then
                Cnt1 = Cnt1 + 1
            ElseIf UCase(Cells(Rw, Col).Value) = "NO" Then
                Cnt2 = Cnt2 + 1
            End If
            Rw = Rw + 1
        Loop
        BodyTxt = BodyTxt & NewLine
        BodyTxt = BodyTxt & Cnt1 & " said they would attend the benefit dinner." & NewLine
        BodyTxt = BodyTxt & Cnt2 & " said they would not attend the benefit dinner." & NewLine & NewLine
        BodyTxt = BodyTxt & "Keep up the good work!"
    
        Call SendMAPIMessage(Recipient, CCRecipient, BodyTxt, "Charity Update")
    End Sub
    
    Sub SendMAPIMessage(MsgTo As String, MsgCC As String, MsgTxt As String, MsgSubject As String)
        Dim TempArray() As String, varArrayItem As Variant, strEmailAddress As String
        Dim objOL As Outlook.Application
        Dim MAPISession As Outlook.Namespace, MAPIFolder As Outlook.MAPIFolder, MAPIMailItem As Outlook.MailItem
        Dim oRecipient As Outlook.Recipient
            
        Set objOL = New Outlook.Application
        Set MAPISession = objOL.Application.Session
        If Not MAPISession Is Nothing Then
            MAPISession.Logon , , True, False
            Set MAPIFolder = MAPISession.GetDefaultFolder(olFolderOutbox)
            If Not MAPIFolder Is Nothing Then
                Set MAPIMailItem = MAPIFolder.Items.Add(olMailItem)
                If Not MAPIMailItem Is Nothing Then
                    With MAPIMailItem
                        TempArray = Split(MsgTo, ";")
                        For Each varArrayItem In TempArray
                            strEmailAddress = Trim(varArrayItem)
                            If Len(strEmailAddress) > 0 Then
                                Set oRecipient = .Recipients.Add(strEmailAddress & AddressExt)
                                oRecipient.Type = olTo
                                Set oRecipient = Nothing
                            End If
                        Next varArrayItem
                        
                        TempArray = Split(MsgCC, ";")
                        For Each varArrayItem In TempArray
                            strEmailAddress = Trim(varArrayItem)
                            If Len(strEmailAddress) > 0 Then
                                Set oRecipient = .Recipients.Add(strEmailAddress & AddressExt)
                                oRecipient.Type = olCC
                                Set oRecipient = Nothing
                            End If
                        Next varArrayItem
                       
                       .Subject = MsgSubject
                     
                        If StrComp(Left(MsgTxt, 1), "<", vbTextCompare) = 0 Then
                            .HTMLBody = MsgTxt
                        Else
                            .Body = MsgTxt
                        End If
                        On Error GoTo SendErr
                        .Send
                        On Error GoTo 0
                        Set MAPIMailItem = Nothing
                    End With
               End If
               Set MAPIFolder = Nothing
            End If
            MAPISession.Logoff
        End If
        Exit Sub
    
    SendErr:
        MsgBox "Unable to send message to " & MsgTo & " " & MsgCC & vbCrLf & vbCrLf & Err.Description
    End Sub

  5. #5
    Registered User
    Join Date
    02-17-2014
    Location
    New York
    MS-Off Ver
    Office 365
    Posts
    34

    Re: How Do You Count Text Occurrences in Excel AND Export to the Body of an Outlook Email?

    Thank you for this response also natefarm. I think I've bitten off more than I can chew with this as it is all Greek to me. I will try to make some sense of this with my very vague recollection of being navigated through the VBA window and I'll report back. I wish I had some equally useful knowledge of something with which I could repay you! Thanks!

  6. #6
    Valued Forum Contributor natefarm's Avatar
    Join Date
    04-22-2010
    Location
    Wichita, Kansas
    MS-Off Ver
    2016
    Posts
    1,020

    Re: How Do You Count Text Occurrences in Excel AND Export to the Body of an Outlook Email?

    I could pre-sort the list. But what do you mean by "go through the rows and increment the count..."
    I made that assumption with the code I provided. The loop does what I was referring to (rw = rw + 1).
    Last edited by natefarm; 11-10-2014 at 02:32 PM. Reason: Included quote for clarification

  7. #7
    Registered User
    Join Date
    02-17-2014
    Location
    New York
    MS-Off Ver
    Office 365
    Posts
    34

    Re: How Do You Count Text Occurrences in Excel AND Export to the Body of an Outlook Email?

    natefarm, I bow to your excellency. I am 100% positive this will work as I need, I just have no idea where to begin in Outlook, what to click, where to paste this, which parts of it to change to suit my excel sheet exactly, but I wanted to thank you as I'm still slowly trying to dissect it and learn how to do get your code to the right place to enable it. (I need a lot more hand-holding than I thought.) Shall I still mark this thread as solved then? Thank you so much!

  8. #8
    Valued Forum Contributor natefarm's Avatar
    Join Date
    04-22-2010
    Location
    Wichita, Kansas
    MS-Off Ver
    2016
    Posts
    1,020

    Re: How Do You Count Text Occurrences in Excel AND Export to the Body of an Outlook Email?

    From an Excel workbook, open the VBA window by pressing Alt+F11. To create a new code module, select Insert - Module. It will default to the name, Module1, which is fine. That's where you would paste the code I provided. To run it, click somewhere in the BuildMessage subroutine and click F5, or to run it one line at a time, press F8 to follow the code in Debug mode and observe the results. To see the value of a variable in debug mode, just hover over it. You can press F5 any time. You can also toggle debug stop(s) on/off by clicking in the band to the left of the code. It will put a stop at that point, indicated by a red dot and red highlighted code. When you press F5, it will run until it hits that line. Then you can press F8 or F5 to continue. If your debug stop is within a loop, you can hit F5 repeatedly and it will bump through the loop. You can also use the Immediate pane (View - Immediate, or Ctrl+G) to check or change variable values or object properties. You can terminate the run any time by selecting Run - Reset, or just clicking the Reset button.

    Have fun!

  9. #9
    Registered User
    Join Date
    02-17-2014
    Location
    New York
    MS-Off Ver
    Office 365
    Posts
    34

    Re: How Do You Count Text Occurrences in Excel AND Export to the Body of an Outlook Email?

    Thank you again, Nate. I tried to do this but I need to start from the very basics first. My problem was solved, but through an much inferior workaround that still requires a few extra steps. I hope this thread can help someone else though. Thank you again for your help. I am definitely inspired to find an excel-to-outlook tutorial. Thank you!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Export body of Outlook email to Excel
    By dougmorgan in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-16-2013, 12:37 PM
  2. Add to Excel from Outlook when a email body contain a specific text
    By mathewfer in forum Outlook Programming / VBA / Macros
    Replies: 0
    Last Post: 05-08-2013, 08:28 AM
  3. export outlook 2007 email into excel with subject and body of email
    By akulka58 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-25-2013, 02:37 PM
  4. Export Outlook Email Body Data to Excel Workbook
    By JerryK1124 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-18-2012, 01:33 PM
  5. [SOLVED] Copy hyperlink from outlook email body and export to excel
    By addytiger in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-21-2012, 03:17 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1