<?xml version="1.0" encoding="utf-16" standalone="yes"?>
<!--Created: 20.07.2017 15:48:40-->
<!--ACTOptimumVersion: 6.0.6298.24764-->
<AutoDataItems ACTOptimumVersion="6.0.6298.24764" Created="20.07.2017 15:48:40">
    <AutoDataControl Created="20.07.2017 15:48:40" ClassName="Melville_Schellmann.ACTOptimum3.Control.AutoData3.AutoData3" PrefClassVersion="1.0" ACTOptimumVersion="6.0.6298.24764">
        <Source>' #ScriptName: CreateEmailFromTemplate
    ' #Description: Creates an e-mail from a fixed determinated Word template file ( * .ADT )
    ' #Copyright: © 2010-2019 by Melville-Schellmann
    ' #Author: Robert Schellmann, rs@melville-schellmann.de
    ' #Version: 1.3 (23.01.2019) A list of files can be configured as attachments
    ' #Version: 1.2 (20.07.2017) A new option to define the email address of the contact which will be used
    ' #Version: 1.1 (10.02.2014) A new option to define a file which will be used as an attchment for the email
    ' #Version: 1.0 (20.02.2010) s.o.
    
    Dim sPath As String
    Dim sDoc As String
    Dim sRegarding As String
    Dim sAttachments As String
    Dim sEmailAddress As String
    Dim sEmailFieldName As String 

    ' Name of the template file in the folder "templates"
    sDoc = "Letter.adt"

    ' Regarding of the email
    sRegarding = "regarding text"

    ' Path to the file to be included as an attachment . If no attachment is required to put on "" .
    ' Multiple file paths can be specified with semicolon separated.
    sAttachments = "" 

    ' Email address of the contact which will be used. Use "" (empty string) if the current contact should be used.
    sEmailAddress = ""
    ' Name of the email address field
    sEmailFieldName = "E-Mail"
    
    ' -------------------------------------------
    ' Please, no changes from this line
    
    m_sScriptName = "CreateEmailFromTemplate"
    m_oACTApp = ACTApp
    m_oAutoData = AutoData
    
    Dim sMsgUnableToFindTemplateX As String = "Unable to find the template '{0}'."
    Dim sMsgUnableToFindAttachmentX As String = "Unable to find the file '{0}' as an attachment."
    Dim sMsgNoContact As String = "There is no contact."
    Dim sMsgThereAreMoreThanOneContactWithValueXInFieldY As String = "The value '{0}' was found at more than one contact in the field '{1}'."
    Dim sMsgUnableToFindPropertyX As String = "Unable to find property '{0}'."
    Dim sMsgUnableToFindMethodX As String = "Unable to find method '{0}'."
    Dim sMsgAnErrorOccurredWhileExecutingMethodX As String = "An error occurred while executing the method '{0}'."
    
    Dim sFile As String
    ' validate template
    sPath = System.IO.Path.Combine(ACTApp.ActFramework.SupplementalFileManager.Workgroup.Path, "Templates")
    sPath = System.IO.Path.Combine(sPath, sDoc)

    If Not System.IO.File.Exists(sPath) Then
      MsgBox(String.Format(sMsgUnableToFindTemplateX, sPath), MsgBoxStyle.Information, m_sScriptName)
      GoTo Abbruch
    End If
    ' validate attachment
    If Not String.IsNullOrEmpty(sAttachments) Then
      For Each sFile In sAttachments.Split(New Char(){";"c}, StringSplitOptions.RemoveEmptyEntries)
        If Not System.IO.File.Exists(sFile) Then
          MsgBox(String.Format(sMsgUnableToFindAttachmentX, sFile), MsgBoxStyle.Information, m_sScriptName)
          GoTo Abbruch
        End If
      Next
    End If
    
    ' Reflection call of ACTApp.Explorer.CustomCommandHelper.CustomCommandHandler(sPath)
    Dim oMailMerge As Act.UI.Correspondence.EmailMailMergeInfo
    Dim oSelectedContact As Act.Framework.Contacts.ContactList
    Dim oWordProcessor As Object
    Dim oProperty As System.Reflection.PropertyInfo
    Dim sProperty As String
    Dim oMethod As System.Reflection.MethodInfo
    Dim sMethod As String
    Dim aParameter(0) As Object

    oMailMerge = New Act.UI.Correspondence.EmailMailMergeInfo
    
    If String.IsNullOrEmpty(sEmailAddress.Trim) Then
      oSelectedContact = ACTApp.ActFramework.Contacts.GetContactAsContactList(ACTApp.ApplicationState.CurrentContact)
    Else
      If TryGetContactsFromFieldAndValue(sEmailFieldName, sEmailAddress, oSelectedContact) = False Then
        GoTo Abbruch
      End If
      If oSelectedContact.Count &gt; 1 Then
        MsgBox(String.Format(sMsgThereAreMoreThanOneContactWithValueXInFieldY, sEmailAddress, sEmailFieldName), MsgBoxStyle.Exclamation, m_sScriptName)
        GoTo Abbruch
      End If
    End If
   
    If oSelectedContact Is Nothing OrElse oSelectedContact.Count = 0 Then
      MsgBox(sMsgNoContact, MsgBoxStyle.Information, m_sScriptName)
    End If
    With oMailMerge
      .EmailCreateHistoryType = Act.Framework.Preferences.Enums.EmailCreateHistoryType.AttachContact
      .ReturnReciept = False
      .MergeContacts = oSelectedContact
      .TemplateType = Act.UI.Correspondence.PredefinedWordProcessorFilesTypes.Other
      .MergeTemplate = sPath
      .Subject = sRegarding
      If Not String.IsNullorempty(sAttachments) Then
        .Attachments = New System.Collections.Specialized.StringCollection
        For Each sFile In sAttachments.Split(New Char(){";"c}, StringSplitOptions.RemoveEmptyEntries)
          .Attachments.Add(sFile)
        Next
      End If
    End With

    sProperty = "CurrentWordProcessorManager"
    oProperty = ACTApp.UICorrespondenceManager.GetType.GetProperty(sProperty, Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)
    If oProperty Is Nothing Then
      MsgBox(String.Format(sMsgUnableToFindPropertyX, sProperty), MsgBoxStyle.Exclamation, m_sScriptName)
      GoTo Abbruch
    End If
    oWordProcessor = oProperty.GetValue(ACTApp.UICorrespondenceManager, Nothing)

    sMethod = "CreateMergeDocument"
    oMethod = oWordProcessor.GetType.GetMethod(sMethod, Reflection.BindingFlags.Instance Or Reflection.BindingFlags.Public)
    If oMethod Is Nothing Then
      MsgBox(String.Format(sMsgUnableToFindMethodX, sMethod), MsgBoxStyle.Exclamation, m_sScriptName)
      GoTo Abbruch
    End If
    aParameter(0) = oMailMerge
    Try
      oMethod.Invoke(oWordProcessor, aParameter)
    Catch ex As Exception
      MsgBox(String.Format(sMsgAnErrorOccurredWhileExecutingMethodX, sMethod) &amp; vbCrLf &amp; _
        ex.Message, MsgBoxStyle.Exclamation, m_sScriptName)
    End Try
    Abbruch:
    Return String.Empty
  End Function
  
  Private Shared m_sScriptName As String
  Private Shared m_oACTApp As ACT.UI.ActApplication
  Private Shared m_oAutoData As Melville_Schellmann.ACTOptimum3.Control.AutoData3.AutoDataScript
  
  Private Shared Function TryGetContactsFromFieldAndValue(ByVal FieldName As String, ByVal Value As String, ByRef Contacts As  Act.Framework.Contacts.ContactList) As Boolean
    
    Dim sMsgNoFieldName As String = "Missing field name."
    Dim sMsgUnableToFindFieldX As String = "Unable to find field '{0}'."
    Dim sMsgNoValueForFieldX As String = "Missing value for the field '{0}'."
    Dim sMsgUnableToFindContactWithValueXInFieldY As String = "No contact found with the value '{0}' in the field '{1}'."
    
    Dim oField As Act.Framework.MutableEntities.MutableEntityFieldDescriptor
    Dim oLookup As Act.Framework.Lookups.ContactLookup
    
    TryGetContactsFromFieldAndValue = False
    
    If String.IsNullOrEmpty(FieldName) Then
      Msgbox(String.Format(sMsgNoFieldName), MsgBoxStyle.Exclamation, m_sScriptName)
      GoTo Abbruch
    End If

    oField = m_oAutoData.GetMutableEntityFieldDescriptor(ActGlobal.ActRegion.Contact, FieldName)
    
    If oField Is Nothing Then
      MsgBox(String.Format(sMsgUnableToFindFieldX, FieldName), MsgBoxStyle.Exclamation, m_sScriptName)
      GoTo Abbruch
    End If
    
    If String.IsNullOrEmpty(Value) Then
      Msgbox(String.Format(sMsgNoValueForFieldX, FieldName), MsgBoxStyle.Exclamation, m_sScriptName)
      GoTo Abbruch
    End If
    
    oLookup = m_oACTApp.ActFramework.Lookups.LookupContactsReplace(Value, act.Framework.Lookups.OperatorEnum.EqualTo, oField, True, True)
    
    
    If oLookup Is Nothing  Then
      MsgBox(String.Format(sMsgUnableToFindContactWithValueXInFieldY, Value, FieldName), MsgBoxStyle.Exclamation, m_sScriptName)
      GoTo Abbruch
    End If
    
    Contacts = oLookup.GetContacts(Nothing)
    If Contacts Is Nothing OrElse Contacts.Count = 0 Then
      MsgBox(String.Format(sMsgUnableToFindContactWithValueXInFieldY, Value, FieldName), MsgBoxStyle.Exclamation, m_sScriptName)
      GoTo Abbruch
    End If
    
    TryGetContactsFromFieldAndValue = True
    Abbruch:
    
  End Function
  
  Private Shared Function Dummy As String
    Return String.Empty</Source>
        <SourceComment>Begriff: {0}</SourceComment>
        <TargetFields></TargetFields>
        <MsgText>{0}</MsgText>
        <Picklist></Picklist>
        <Multiple>False</Multiple>
        <Expandable>True</Expandable>
        <OverwriteAlways>True</OverwriteAlways>
        <CopyToClipboard>False</CopyToClipboard>
        <ShowMsgBox>False</ShowMsgBox>
        <PositionMode>1</PositionMode>
        <FlatStyle>Standard</FlatStyle>
        <AutoDataText>Create email...</AutoDataText>
        <TextAlign>MiddleCenter</TextAlign>
        <AutoDataFormSize>160; 270</AutoDataFormSize>
        <TooltipText>Creates an email from a special template.</TooltipText>
        <RefreshLoadedViews>False</RefreshLoadedViews>
    </AutoDataControl>
</AutoDataItems>