Compartir a través de


Definitive Source for Code to Automate E-Mailing Forms

You will find that at some point you need to be a little more clever with sending mail than what InfoPath will allow you to do with just the GUI and dataconnections.  A colleague of mine, Scott Heim, sent me an awesome little XSN file that had 4 buttons in it that highlighting four different ways to kick off e-mail programmatically from InfoPath.   If you e-mail me I'll send you the file.  The code however is pretty easy to follow and is posted here for your enjoyment.

 

Sub btnShowMailItem_OnClick(eventObj)

XDocument.UI.ShowMailItem "yourname@microsoft.com", "", "", "Subject: Test", "Body: Test"

End Sub '

Sub btnMailEnvelope_OnClick(eventObj)

Dim oEnvelope

Set oEnvelope = Application.ActiveWindow.MailEnvelope

oEnvelope.Subject = "Subject: Test"

oEnvelope.To = yourname@microsoft.com

oEnvelope.Visible = true

End Sub

Sub btnOutlook_OnClick(eventObj)

Dim objOutlook

Dim objOutlookMsg

Dim objOutlookRecip

Dim objOutlookAttach

Dim frmSavePath

'Create the Outlook session.

Set objOutlook = CreateObject("Outlook.Application")

Set objOutlookMsg = objOutlook.CreateItem(0) 'olMailItem

With objOutlookMsg

'Add the To recipient(s) to the message.

Set objOutlookRecip = .Recipients.Add(yourname@microsoft.com)

objOutlookRecip.Type = 1 'olTo '

'Add the CC recipient(s) to the message.

' Set objOutlookRecip = .Recipients.Add(someone@somewhere.com)

' objOutlookRecip.Type = 2 'olCC '

' 'Add the BCC recipient(s) to the message.

' Set objOutlookRecip = .Recipients.Add(someoneelse@somewhere.com)

' objOutlookRecip.Type = 3 'olBCC

'Set the Subject, Body, and Importance of the message.

.Subject = "This is an Automation test with Microsoft Outlook"

.body = "This is the body of the message"

' **NOTE: if you want the body of the message to be HTML, then use '.HTMLbody instead of just .body

.Importance = 2 'High importance

'Resolve each Recipient's name.

For Each objOutlookRecip In .Recipients

objOutlookRecip.Resolve

Next

'Display the message before sending

.display

'If you want to add the form as an attachment, you need to make sure it is saved first and then add that file as an attachment

'** NOTE: THIS REQUIRES THE FORM TO BE FULLY TRUSTED!!!

frmSavePath = "C:\MyForm.XML"

XDocument.SaveAs(frmSavePath)

Set objOutlookAttach = .Attachments.Add(frmSavePath)

End With

Set objOutlook = Nothing

Set objOutlookMsg = Nothing

Set objOutlookRecip = Nothing

Set objOutlookAttach = Nothing

End Sub

Comments

  • Anonymous
    February 25, 2007
    Note from a reader: Please add a remark that as from Outlook 2007 or Exchange 2005, the usage of MAPI with Outlook or Exchange require seperate installation of "Microsoft Collaboration Data Objects (CDO) 1.2.1" from Mirosoft's web site, else error message will occour when you will try to create the instance of the MAPI Session.

  • Anonymous
    January 30, 2008
    How can I do same stuff from outside Infopath at serverside without launching infopath?