🛡️ 500+ Free Excel, VBA, Outlook & Python Guides — Tested & Copy-Ready Explore 150+ VBA Macros →

Summarize Outlook Emails with Gemini API using VBA Macro

Summarize Outlook Emails with Gemini API - VBA Macro

Monday morning, 80 unread emails in Inbox. Half of them are long reply chains where the actual point is hidden in the last 2 lines. I am sure most of us have been there.

What if a macro reads these emails, sends them to Gemini & gives you one mail with a 3 line summary of each email & what action is needed from you? That is what we are going to build in this page, with Outlook VBA.

This code uses the same Gemini functions from the Excel VBA Gemini article. Only difference is, instead of reading the prompt from a cell, we read it from the email body.

Also Read: Download Outlook Emails to Excel

Important - Before Sending Office Emails to Gemini

Please read this before running the code on your office mailbox.

This macro sends the email content to Google servers. In Gemini free tier, the content you send can be used by Google to improve their products. So, do not use this with a free key on confidential emails, customer data or anything your company does not allow to share outside. Check your company IT policy first. For personal mailbox or newsletters, it is fine.

You also need a Gemini API key. If you don't have one, here is how to get free Gemini API key.

Steps to Setup the Macro in Outlook

  1. Open Outlook & press Alt + F11 to open VB editor.
  2. Go to Insert -> Module.
  3. From the Excel VBA Gemini article, copy the 2 constants at the top (GEMINI_API_KEY & GEMINI_MODEL) & the 3 functions: Gemini_Get_Response, Json_Escape & Json_Get_Text. Paste them in this module. Do not copy the Ask_Gemini_From_Excel sub, it uses Excel cells.
  4. Paste your API key in GEMINI_API_KEY.
  5. Copy paste the below code in the same module, below those functions.
  6. If macros are disabled, go to File -> Options -> Trust Center -> Trust Center Settings -> Macro Settings, enable "Notifications for all macros" & restart Outlook.

Outlook VBA Code to Summarize Selected Emails

Select one or more emails in your Inbox (use Ctrl + Click to select many), go to VB editor, place the cursor inside Summarize_Selected_Emails & press F5.

'Summarize the emails selected in Outlook & show the report in a new mail
Sub Summarize_Selected_Emails()
    Dim oSel As Outlook.Selection
    Dim oItem As Object
    Dim sReport As String
    Dim iCount As Long

    Set oSel = Application.ActiveExplorer.Selection
    If oSel.Count = 0 Then
        MsgBox "Select one or more emails in Inbox & run again"
        Exit Sub
    End If

    'Loop thru each selected item & process only emails
    For Each oItem In oSel
        If TypeName(oItem) = "MailItem" Then
            sReport = sReport & Summarize_One_Email(oItem)
            iCount = iCount + 1
            DoEvents
        End If
    Next oItem

    Show_Summary_Report sReport, iCount
End Sub

'Build the prompt for one email, send to Gemini & return formatted summary
Private Function Summarize_One_Email(ByVal oMail As Outlook.MailItem) As String
    Dim sPrompt As String, sSummary As String

    sPrompt = "Summarize this email in maximum 3 short bullet points. " & _
              "Then add one line starting with 'Action needed:' with what I have to do, or 'None'. " & _
              "Reply in plain text without markdown." & vbLf & vbLf & _
              "From: " & oMail.SenderName & vbLf & _
              "Subject: " & oMail.Subject & vbLf & vbLf & _
              Left$(oMail.Body, 15000)

    sSummary = Gemini_Get_Response(sPrompt)

    'Gemini returns line breaks as vbLf. Outlook body needs vbCrLf.
    sSummary = Replace(sSummary, vbCrLf, vbLf)
    sSummary = Replace(sSummary, vbLf, vbCrLf)

    Summarize_One_Email = "Subject : " & oMail.Subject & vbCrLf & _
                          "From    : " & oMail.SenderName & "  (" & _
                          Format(oMail.ReceivedTime, "dd-mmm-yyyy hh:mm") & ")" & vbCrLf & _
                          sSummary & vbCrLf & String(60, "-") & vbCrLf & vbCrLf

    'Small wait to stay within API rate limit
    Wait_Seconds 2
End Function

'Show all summaries in a new email draft (not sent)
Private Sub Show_Summary_Report(sReport As String, iCount As Long)
    Dim oReport As Outlook.MailItem

    If iCount = 0 Then
        MsgBox "No emails found to summarize"
        Exit Sub
    End If

    Set oReport = Application.CreateItem(olMailItem)
    oReport.Subject = "Email Summary - " & iCount & " emails - " & Format(Now, "dd-mmm-yyyy")
    oReport.Body = sReport
    oReport.Display
End Sub

'Outlook does not have Application.Wait like Excel, so use this
Private Sub Wait_Seconds(iSeconds As Long)
    Dim tEnd As Date
    tEnd = DateAdd("s", iSeconds, Now)
    Do While Now < tEnd
        DoEvents
    Loop
End Sub

After few seconds, a new mail window opens with the summary of each selected email. It is only a draft & it is not sent to anyone. You can read it, forward it to yourself or just close it without saving.

What does this code do?

  1. Summarize_Selected_Emails: Takes the emails selected in Outlook. Meeting requests, calendar items etc., are skipped by checking the TypeName.
  2. Summarize_One_Email: Builds the prompt with sender, subject & body of the email & sends it using Gemini_Get_Response. Only first 15000 characters of the body is sent. Long reply chains have the old emails at the bottom, which we don't need anyway & it saves tokens.
  3. Show_Summary_Report: Creates a new email & puts all the summaries in the body.
  4. Wait_Seconds: Outlook VBA does not have Application.Wait. This small loop waits for 2 seconds between each email, so that we don't cross the per minute limit.

You can change the prompt to anything you need. For example, "Reply with only one word: Urgent, Normal or Ignore" to quickly sort the emails. Or "Write a polite reply to this email" to get a reply draft.

Summarize All Unread Emails in Inbox

Selecting emails every time is not needed. This one picks all unread emails from Inbox automatically. Add it to the same module.

Sub Summarize_Unread_Emails()
    Dim oInbox As Outlook.Folder
    Dim oItems As Outlook.Items
    Dim oItem As Object
    Dim sReport As String
    Dim iCount As Long

    'Get only unread items from default Inbox
    Set oInbox = Application.Session.GetDefaultFolder(olFolderInbox)
    Set oItems = oInbox.Items.Restrict("[UnRead] = True")
    oItems.Sort "[ReceivedTime]", True

    For Each oItem In oItems
        If TypeName(oItem) = "MailItem" Then
            sReport = sReport & Summarize_One_Email(oItem)
            iCount = iCount + 1

            'Process maximum 30 emails in one run
            If iCount >= 30 Then Exit For
        End If
    Next oItem

    Show_Summary_Report sReport, iCount
End Sub

The emails are not marked as read by this code. So, you will still see them as unread in Inbox. I have also put a limit of 30 emails per run, so that a big Inbox does not use your full daily quota in one go. Change the number as you need.

If you want to sort your Inbox before running this, read how to sort Outlook emails by date using VBA.

Things to Know

  • Outlook freezes while running: Outlook waits for each Gemini response. For 30 emails with 2 seconds wait, it can take a minute or two. Do not click anything till the report opens.
  • Error 400 in summary: Usually some special character in the email broke the JSON. Json_Escape handles most of them. If it still happens for a particular email, copy the body to a new mail & remove the special formatting before running.
  • New Outlook does not support VBA: This works only in classic Outlook for Windows (Outlook 2016, 2019, 2021 & Microsoft 365 desktop). The "New Outlook" app & Outlook on the web do not run VBA macros.
  • Error 429: Rate limit reached. Increase the Wait_Seconds value or reduce the number of emails per run.

Once this is working, you can add a button for Summarize_Unread_Emails in the Outlook ribbon (File -> Options -> Customize Ribbon -> choose Macros). Then it is just one click every morning.