<?xml version="1.0" encoding="utf-16" standalone="yes"?>
<!--Created: 23.01.2019 12:18:35-->
<!--ACTOptimumVersion: 6.0.6901.26650-->
<AutoDataItems ACTOptimumVersion="6.0.6901.26650" Created="23.01.2019 12:18:35">
    <AutoDataControl Created="23.01.2019 12:18:35" ClassName="Melville_Schellmann.ACTOptimum3.Control.AutoData3.AutoData3" PrefClassVersion="1.0" ACTOptimumVersion="6.0.6901.26650">
        <Source>' #ScriptName: CreateEmailFromTemplate
    ' #Description: Erstellt eine E-Mail aus einer festgelgeten Wordvorlagendatei (*.ADT)
    ' #Copyright: © 2010-2019 by Melville-Schellmann
    ' #Author: Robert Schellmann, rs@melville-schellmann.de
    ' #Version: 1.3 (23.01.2019) Es können mehrere Dateipfade für Anlagen angegeben werden
    ' #Version: 1.2 (20.07.2017) Neue Option für die Email-Adresse des Kontaktes der verwendet wird.
    ' #Version: 1.1 (10.02.2014) Optionale Angabe einer Datei, die als Anlage an die E-Mail angehängt wird.
    ' #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 der Vorlagendatei im Ordner "Templates"
    sDoc = "Letter.adt"

    ' Betreff für die E-Mail
    sRegarding = "Betrefftext"

    ' Pfad zu der Datei die als Anlage angehängt werden soll. Wenn keine Anlage gewünscht ist auf "" setzen.
    ' Es können mehrere Dateipfade mit Semikolon getrennt angegeben werden.
    sAttachments = "" 

    ' E-Mail Adresse des Kontaktes. Wenn keine ("") Adresse angegeben wird, wird der aktuelle Kontakt verwendet.
    sEmailAddress = ""
    ' Name des E-Mail Feldes
    sEmailFieldName = "E-Mail"
    
    ' -------------------------------------------
    ' Ab hier bitte keine Anpassungen vornehmen.
    
    m_sScriptName = "CreateEmailFromTemplate"
    m_oACTApp = ACTApp
    m_oAutoData = AutoData
    
    Dim sMsgUnableToFindTemplateX As String = "Es konnte nicht die Vorlage '{0}' gefunden werden."
    Dim sMsgUnableToFindAttachmentX As String = "Es konnte nicht die Datei '{0}' für die Anlage gefunden werden."
    Dim sMsgNoContact As String = "Es ist kein Kontakt vorhanden."
    Dim sMsgThereAreMoreThanOneContactWithValueXInFieldY As String = "Der Wert '{0}' wurde bei mehr als einem Kontakt in dem Feld '{1}' gefunden."
    Dim sMsgUnableToFindPropertyX As String = "Die Eigenschaft '{0}' wurde nicht gefunden."
    Dim sMsgUnableToFindMethodX As String = "Die Methode '{0}' wurde nicht gefunden."
    Dim sMsgAnErrorOccurredWhileExecutingMethodX As String = "Es ist ein Fehler beim Ausführen der Methode '{0}' aufgetreten."
    
    Dim sFile As String
    ' Kontrolliere Vorlage
    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
    ' Kontrolliere Anlage
    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-Aufruf von 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 = "Es wurde kein Name für das Suchfeld angegeben."
    Dim sMsgUnableToFindFieldX As String = "Es wurde kein Feld '{0}' gefunden."
    Dim sMsgNoValueForFieldX As String = "Es wurde kein Wert für das Feld '{0}' angegeben."
    Dim sMsgUnableToFindContactWithValueXInFieldY As String = "Es wurde kein Kontakt mit dem Wert '{0}' in dem Feld '{1}' gefunden."
    
    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>E-Mail erstellen aus spezieller Vorlage</AutoDataText>
        <TextAlign>MiddleCenter</TextAlign>
        <AutoDataFormSize>160; 270</AutoDataFormSize>
        <TooltipText>rstellt eine E-Mail aus einer Vorlage</TooltipText>
        <RefreshLoadedViews>False</RefreshLoadedViews>
    </AutoDataControl>
</AutoDataItems>