Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle the incoming emails using vbscript

Problem: Handle the incoming emails using vbscript.

Outlook Version: Outlook 2000

Description: I cannot use VBA for this as I believe Outlook 2000 doesn't let you run a VBA script from the rules wizard and hence I have to use the Run a Program | VBScript method.

What I know: I know how to handle email from VBA like this

Sub Sample(MyMail As MailItem)
    Dim strID As String, olNS As Outlook.NameSpace
    Dim olMail As Outlook.MailItem

    strID = MyMail.EntryID
    Set olNS = Application.GetNamespace("MAPI")
    Set olMail = olNS.GetItemFromID(strID)

    '~~> Rest of the code

    Set olMail = Nothing
    Set olNS = Nothing
End Sub

I also know how to run the vbscript on the email which is already in the inbox. To run a vbscript in OL2000, you have to use Run A Program and point it to the vbs file. The Run A Script is not available in OL2000.

What I do not know: And this is where I need help. How to get the mail object which has not hit the mail inbox in VBS. Once I get the object then I can do the rest of the necessary operations.

like image 407
Siddharth Rout Avatar asked Oct 08 '22 05:10

Siddharth Rout


1 Answers

You are correct that OL2000 cannot run a VBA macro from a rule, if this article is to be believed.

Here's how I handle incoming emails. It does use VBA but as far as I know, there isn't a way to do so in VBScript.

Private WithEvents Items As Outlook.Items 
Private Sub Application_Startup() 
  Dim olApp As Outlook.Application 
  Dim objNS As Outlook.NameSpace 
  Set olApp = Outlook.Application 
  Set objNS = olApp.GetNamespace("MAPI") 
  Set Items = objNS.GetDefaultFolder(olFolderInbox).Items 
End Sub
Private Sub Items_ItemAdd(ByVal item As Object) 

  On Error Goto ErrorHandler 
  Dim Msg As Outlook.MailItem 
  If TypeName(item) = "MailItem" Then
    Set Msg = item 
    '~~> do something with the new message here
  End If
ProgramExit: 
  Exit Sub
ErrorHandler: 
  MsgBox Err.Number & " - " & Err.Description 
  Resume ProgramExit 
End Sub

This code should be pasted into the ThisOutlookSession module, then Outlook should be restarted.

like image 58
JimmyPena Avatar answered Oct 12 '22 11:10

JimmyPena