In this example, we are going to incorporate logic that takes a PDF that is generated as a datapath export and email it through the local Microsoft Outlook Application for a Mi-Forms running on a Windows device. It is assumed that you have a PDF export as a datapath export (Form Properties->Datapaths) and that you have Microsoft Outlook installed on your device.
In order to accomplish this, you will need to add a few .dll references via the (.NET) script editor. At the bottom of the editor, you should see two tabs labeled "Errors" and "References". Select "References", right click in the white space and select "Add Reference" from the dialog box.
The first reference is the DatapathExportModule.dll. After selecting "Add Reference" browse to find this reference, most likely in the C:/Program Files (x86)/Mi-Co/Mi-Forms Designer directory. It does not need to be embedded.
The second and third references will need be embedded and may be found in various locations, depending upon your device's configuration. The second one is "Microsoft.Office.Interop.Outlook.dll" and the third is "office.dll". We conveniently found both of these by entering %AppData% at a file explorer window and then navigating to /AppData/Local/Temp/MFD/.
Once your references are in place, we are ready to starting coding. Below is a copy of the code that we implemented for this solution.
<MiCode(FormScriptEventType.AfterDataPathsRun)> _ Public Sub Form_AfterDataPathsRun(ByVal e As AfterDataPathsRunEventArgs) Export() End Sub Private Sub Export() 'iterate through export data paths Dim er As ExportResult For Each e As ExportResult In _Form.Validator.ExportResults If e.DatapathName = "PDF" Then er = e End If Next ' Create an Outlook application. Dim oApp As Outlook._Application oApp = New Outlook.Application() ' Create a new MailItem. Dim oMsg As Outlook._MailItem oMsg = oApp.CreateItem(Outlook.OlItemType.olMailItem) oMsg.Subject = "Form Test" oMsg.Body = "This is a test of a form." ' TODO: Replace with a valid e-mail address. oMsg.To = "firstname.lastname@example.org" ' Add an attachment Dim sSource As String = er.ExpandedFilePath ' TODO: Replace with attachment name Dim sDisplayName As String = System.IO.Path.GetFileName(er.ExpandedFilePath) Dim sBodyLen As String = oMsg.Body.Length Dim oAttachs As Outlook.Attachments = oMsg.Attachments Dim oAttach As Outlook.Attachment oAttach = oAttachs.Add(sSource, Nothing, sBodyLen + 1, sDisplayName) ' Send oMsg.Display() 'oMsg.Send() ' Clean up oApp = Nothing oMsg = Nothing oAttach = Nothing oAttachs = Nothing End Sub
In this code snippet, you should see that we have created a private function called Export() that is called from the Form_AfterDataPathsRun. In Export, we first iterate through the datapaths (as we could have many) and locate the PDF. We then go through a series of steps in creating an Outlook reference, mail, add any attachments, send and then clean up.
We hope this was useful. It can be altered in many different ways but please let us know if we can be of assistance to your solution needs.