Check Subject of New Outlook Email and Run Excel VBA Program Based on Sender

The code below assigns a variable to either the sender or the subject of every new email that you receive. If the variable equals any of the particular IF statements that you test for, then it will run an Excel program in whichever workbook you decide to open. If you combine this code below with the "Connect Outlook to Cell Phone," then you could technically control your computer via text message or have your Outlook text your phone particular messages when you receive emails with specific words or from specific people while away at lunch. This particular macro is an EVENT macro and I show you where to place the code in my Youtube video below.

Code:

Private Sub Application_NewMail()

Dim individualItem As Object
Dim att As Attachment
Dim strPath As String
Dim dicFileNames As Object

Do Until goku > 25000000

goku = goku + 1


Loop



'Count emails in box
Dim O As Double
For N = 1 To Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox) _
    .Items.Count
    ' counts the amount of emails
Next N
N = N - 1

goku = 1

line10:



'Select only recent email
On Error GoTo line10: ActiveExplorer. & _
AddToSelection Application.GetNamespace & _
("MAPI").GetDefaultFolder(olFolderInbox) & _
    .Items(N)




Dim myOlExp As Outlook.Explorer
 Dim myOlSel As Outlook.Selection
 Dim mySender As Outlook.AddressEntry
 Dim oMail As Outlook.MailItem
 Dim oAppt As Outlook.AppointmentItem
 Dim oPA As Outlook.PropertyAccessor
 Dim strSenderID As String
 Const PR_SENT_REPRESENTING_ENTRYID As String = _
 "http://schemas.microsoft.com/mapi/proptag/0x00410102"
 Dim MsgTxt As String
 Dim x As Long
 'Get senders of selected emails
 
 Set myOlExp = Application.ActiveExplorer
 Set myOlSel = myOlExp.Selection
 For x = 1 To myOlSel.Count
 If myOlSel.Item(x).Class = OlObjectClass.olMail Then
 ' For mail item, use the SenderName property.
 Set oMail = myOlSel.Item(x)
 MsgTxt = oMail.Subject
 End If
 Next

If MsgTxt = "Check Yahoo" Then

Dim wkb As Excel.Workbook
Dim wks As Excel.Worksheet


Set appExcel = CreateObject("Excel.Application")

strSheet = "C:\Users\Nonaluuluu\Desktop\FirstCodeBook.xlsm"
appExcel.Workbooks.Open (strSheet)

Set wkb = appExcel.ActiveWorkbook
Set wks = wkb.Sheets(1)


wks.Activate
appExcel.Application.Visible = True


 wkb.Application.Run "Module1.aaa"
End If






End Sub

I show you in the below video where to place this code in the Visual Basic Editor. If you don't know how to get there, then read this: https://vbatutorialcode.com/connect-outlook-to-phone-text-message/

The only things you'll need to adjust are pictured below.

  1. Switch the path of the workbook that you want to open that has the Excel program that you want to run. If you don't know what a file path is, then I recommend you watch this video: https://www.youtube.com/watch?v=jnbZnw3SyLs

2. Switch the name of the macro to be the name of the subprocedure in your respective workbook.

In my above Module1.aaa my subprocedure in Module 1 is called aaa as pictured below.

If you're unsure of how to create Modules, then I recommend you watch the video at the following link/read the article I wrote or go through all my Main VBA tutorials to get the basics down: https://vbatutorialcode.com/visual-basic-editor/

Comments are closed.