Reputation: 11
I am developing a VBA program that will be triggered for newly arrived email in Outlook. (Criteria is set at Outlook's "Rules and Alerts".)
The emails are expected to have:-
"subject" field consists of: "WO_name/Task_name"
First line of "Body" consists of : "operation"
"String_1", "String_2", "String_3" and the sender's email address are to be fed into a batch file (4 arguments in total)
This is my code:
Sub ChangeSubjectThenSend(Item As Outlook.MailItem)
WO = readCommand(Item.Subject, 1)
Task = readCommand(Item.Subject, 2)
Dim olItem As Outlook.MailItem
Dim sText As String
For Each olItem In Application.ActiveExplorer.Selection
sText = olItem.Body
Next olItem
Shell ("C:\warehouse\WO\checkInOutTask\taskOperations.bat " & WO & " " & Task & " " & Item.SenderEmailAddress & " " & sText), vbNormalFocus
End Sub
Public Function readCommand(str As String, position As Integer) As String
Dim Tempstr() As String
If position >= 1 Then
Tempstr = Split(str, "/")
readCommand = Tempstr(position - 1)
End If
End Function
It feeds the contents of the email entry currently selected in Outlook rather than the newly arrived email.
How do I make the script run against the newly arrived email instead?
Upvotes: 1
Views: 715
Reputation: 9199
To apply code to the newly arrived mail, do not select anything.
Instead use Item
passed in (Item As MailItem)
.
Option Explicit
Sub ChangeSubjectThenSend(Item As MailItem)
WO = readCommand(Item.subject, 1)
Task = readCommand(Item.subject, 2)
Dim sText As String
sText = Item.body
Shell ("C:\warehouse\WO\checkInOutTask\taskOperations.bat " & WO & " " & Task & " " & Item.SenderEmailAddress & " " & sText), vbNormalFocus
End Sub
Public Function readCommand(str As String, position As Long) As String
Dim Tempstr() As String
If position >= 1 Then
Tempstr = Split(str, "/")
readCommand = Tempstr(position - 1)
End If
End Function
Upvotes: 0
Reputation: 12413
When you open the Outlook Visual Basic Editor, the minimum Project Explorer you will see is something like:
If you clck the plus against "Project1 (VbaProject.OTM)", it will expand to:
It is probable that your initial view will be this with Forms (if any) and Modules already expanded. But "Microsoft Office Outlook Objects" will probably not be expanded. Click the plus against it to get:
Click "ThisOutlookSession" to select it and you will get:
"ThisOutlookSession" is like a module but it has extra privileges. In particular you can specify event routines here. Yours will be empty but here you can see the code I have created for you.
Private Sub Application_MAPILogonComplete()
is called when the logon process is complete. I have included code to initialise Public WithEvents MyNewItems As Outlook.Items
. Because of this initialisation, Private Sub myNewItems_ItemAdd(ByVal Item As Object)
is called every time a new item is added to Inbox. My code simply displays the new item's subject in the immediate window but, if you replace my code with yours, you should be able to get the effect you seek.
Add the code below to your "ThisOutlookSession" then exit and re-enter Outlook. You may get enough new emails anyway but, if not, send yourself some emails.
Option Explicit
Public WithEvents MyNewItems As Outlook.Items
Private Sub Application_MAPILogonComplete()
Dim NS As NameSpace
Set NS = CreateObject("Outlook.Application").GetNamespace("MAPI")
With NS
Set MyNewItems = NS.GetDefaultFolder(olFolderInbox).Items
End With
End Sub
Private Sub myNewItems_ItemAdd(ByVal Item As Object)
With Item
Debug.Print "Item added to Inbox with Subject: " & .Subject
End With
End Sub
Upvotes: 1