Premchand
Premchand

Reputation: 51

How I can run macro automatically with new email arrives in outlook

Am very new to VBScript and macro . I have a vbscript which will create tickets for email's but I need to run this script manually every time .To make it automation I have mapped my macro to the "run a script" script rule in Outlook but when the rule runs it is not fetching the data in the mail which is arrived .It's creating tickets with previous email always .

I have gone through so many VBscripts but none worked to convert the mail that received in to ticket.

If any one faced similar type of issue please let me know the complete solution.

Dim olApp As outlook.Application
Dim objNS As outlook.NameSpace
Dim objSourceFolder As outlook.MAPIFolder
Dim objDestFolder As outlook.MAPIFolder
Dim Msg As outlook.MailItem
Set olApp = outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set objSourceFolder = objNS.GetDefaultFolder(olFolderInbox)
Set objDestFolder = objSourceFolder.Folders("Termination")
Set Msg = objDestFolder.Items.GetLast
Set sh = CreateObject("Shell.Application")
Set ie = CreateObject("InternetExplorer.Application")
LocationURL = "Ticket URL” & Msg.EntryID"
ie.Navigate (LocationURL)
ie.Visible = True
With ie.Document
    .getElementById("details").Value = Msg.Body
    .getElementById("short_description").Value = Msg.Subject
    .getElementById("requester_login").Value = "premchand"

End With

Upvotes: 0

Views: 2904

Answers (2)

Eugene Astafiev
Eugene Astafiev

Reputation: 49395

There are several ways for handling incoming emails:

  1. The NewMailEx event of the Application class which is fired when a new item is received in the Inbox. This event is not available in Microsoft Visual Basic Scripting Edition (VBScript).

  2. The ItemAdd event of the Items class is fired when one or more items are added to the specified collection. So, you can subscribe to the Inbox folder to see new items. Be aware, this event does not run when a large number of items are added to the folder at once. This event is not available in Microsoft Visual Basic Scripting Edition (VBScript).

  3. Assign a macro VBA sub to the rule in Outlook. In that case the incoming mail item is passed as a parameter to the sub as Tim showed.

Upvotes: 0

Tim Williams
Tim Williams

Reputation: 166306

When you run a macro from a rule it typically has an argument to which the item triggering the rule gets passed: you do not have to navigate through the Inbox to find the item.

Public Sub DoSomething(Item As Outlook.MailItem)

   'code which acts on "Item"

End Sub

Upvotes: 2

Related Questions