-->

How to reference the mail which triggered the outl

2020-08-07 09:59发布

问题:

I am designing a QTP framework which in which the automation engineer sends an email in order to start the test suite execution (The email can be scheduled using MS Outlook)

As soon as the trigger mail is received, the attachment should be downloaded and QTP should be launched (automatically) using appropriate settings by the Outlook Macro

But in the above scenario I want to download the attachment of the mail and move the mail to a different folder which triggered the macro.

Is there any way to reference the mail which triggered the macro?

Currently this is what I am doing.

Sub TestSuiteInitialilzer(mail As Outlook.MailItem)
    Set ns = Application.GetNamespace("MAPI")
    Set Inbox = ns.GetDefaultFolder(olFolderInbox)
    Set objDestFolder = Inbox.Folders("RAN")
    FileName = "C:\Email Attachments\" & mail.Attachments.Item.FileName
    'Download the attachment
    Atmt.SaveAsFile FileName
    'Move the mail to another folder
    mail.Move objDestFolder
    launchQTP = "C:\Unlock.vbs"
    Set objShell = CreateObject("WScript.Shell")
    ‘Launch QTP
    objShell.Run launchQTP  
    objShell = Nothing
End Sub

回答1:

Two steps to skip checking the whole Inbox until a match. Keep yours just in case you need to run it manually for the first matching item.

1) Add code below (altered to use cscript.exe):

Sub TestSuiteInitialilzer_Rules(ByRef oMail As MailItem)
    Const FileName = "C:\Email Attachments\"
    For Each Atmt In oMail.Attachments
        'Download the attachment
        Atmt.SaveAsFile FileName & Atmt.Filename
    Next Atmt
    ' Move the Mail to objDestFolder
    oMail.Move objDestFolder
    'launchQTP = "C:\Unlock.vbs"
    launchQTP = "cscript.exe //nologo C:\Unlock.vbs"
    Set objShell = CreateObject("WScript.Shell")
    'QTP will be launched with the neccassary configurations through the vb script
    objShell.Run launchQTP
    Set objShell = Nothing
End Sub

2) Add Outlook Rule (Apply rule on messages I receive) on top of your existing one. Better to turn off your existing one.



回答2:

You just need to define a VBA macro sub which accepts a MailItem as a parameter. The source object will be passed to the sub and you don't need to search it in the Inbox folder. For example:

Public Sub Text(mail as Outlook.MailItem)
    ' do something with the mail object
End Sub

You can access attachments using the Attachments property of the MailItem class. The Move method allows to move the item to another folder.