<?xml version="1.0" encoding="utf-16" standalone="yes"?>
<!--Created: 30.06.2015 12:28:54-->
<!--ACTOptimumVersion: 6.0.5581.26549-->
<AutoDataItems ACTOptimumVersion="6.0.5581.26549" Created="30.06.2015 12:28:54">
    <AutoDataControl Created="30.06.2015 12:28:54" ClassName="Melville_Schellmann.ACTOptimum3.Control.AutoData3.AutoData3" PrefClassVersion="1.0" ACTOptimumVersion="6.0.5581.26549">
        <Source>' #ScriptName: ChangeSelectedHistory
    '              AutotData3-Script: ChangeSelectedHistory
    ' #Description: DEUTSCH:
    '               Das Script ändert eine markierte Historie innerhalb eines Kontaktdatensatzes. 
    '               Es werden der Typ, das Ergebnis und der Betreff angepasst. 
    '               Der vorherige Betreff wird in die Details verschoben. 
    '               Es findet eine Verlinkung zum Firmendatensatz des Kontaktes statt.
    '               
    '               ENGLISH:
    '               The script changes a marked history within a contact record. 
    '               There are adapted to the type , the result and the regarding. 
    '               Previous regarding is moved in the details. 
    '               It will be created a link to the company record of contact.
    ' #Author: Julien Temole
    ' #Copyright: Melville-Schellmann
    '             Copyright: © 2014 by Melville-Schellmann
    '             Autor: Julien Temole
    '             Version: 1.1 (2.9.2014) 
    
    ' Deutsch: Der rot dargestellte Text innerhalb der Anführungsstriche kann ggfs. auf Ihren Bedarf angepasst werden.
    ' English: The red shown text within the "" can be changed to your wishes.
    
    ' DEUTSCH: Hier den neuen Historientyp hinterlegen
    ' ENGLISH: Here define the new history type
    Dim sNewActivityTyp As String = "Erledigung"
    
    ' DEUTSCH: Hier das neue Historienergebnis hinterlegen
    ' ENGLISH: Here define the new history result
    Dim sNewHistoryResult As String = "Erledigung abgeschlossen"
    
    ' DEUTSCH: Hier das alte Historienergebnis hinterlegen
    ' ENGLISH: Here define the old history result
    Dim sSelectedHistoryResult As String = "Gesendete E-Mail"
   
    Dim oHistory As Act.Framework.Histories.History
    Dim oActivityType As Act.Framework.Activities.ActivityType
    Dim oHistoryType As Act.Framework.Histories.HistoryType
    Dim sAttachment As String
    Dim sSelectedRegarding As String
    Dim soldReagrding As String
    Dim sValues As String
    Dim sSelected As String
    Dim i As Integer
    Dim oPicklist As act.Framework.PickLists.PickList
    Dim LinkHistoryToCompanies As Boolean 
    Dim oContact As Act.Framework.Contacts.Contact
    Dim oContacts As Act.Framework.Contacts.ContactList
    Dim oCompany As Act.Framework.Companies.Company
    Dim oCompanies As Act.Framework.Companies.CompanyList
    Dim Meldung As String
    Dim oLinkedCompanies() As system.guid
  
    m_sScriptName = "ChangeSelectedHistory"
    m_oACTApp = ACTApp
    m_oAutoData = AutoData
    Meldung = "Achtung!"
    
    If Not TypeOf ACTApp.CurrentView Is Act.UI.IContactDetailView Then
      Msgbox("Falsche Ansicht. Bitte in der Kontakt Ansicht aufrufen.", MsgBoxStyle.Exclamation, m_sScriptName)
      GoTo Abbruch
    End If
    
    ' Tätigkeitstyp ermitteln
    oActivityType = m_oACTApp.ActFramework.Activities.GetActivityType(sNewActivityTyp)
    If oActivityType Is Nothing Then
      MsgBox(String.Format("Der Tätigkeitstyp '{0}' wurde nicht gefunden.", sNewActivityTyp), MsgBoxStyle.Information, m_sScriptName)
      GoTo Abbruch
    End If

    PrintStatus("Historie ermitteln...") 
    oHistory = GetSelectedHistory()
   
    If oHistory Is Nothing Then
      MsgBox("Es konnte kein markierter Historieneintrag ermittelt werden.", MsgBoxStyle.Information, m_sScriptName)
      GoTo Abbruch
    End If
      
    'Betreff und Details ändern
    soldReagrding = oHistory.Regarding
    oHistory.Details = soldReagrding
    
    ' Historietyp ermitteln
    oHistoryType = oHistory.HistoryType
    If String.Compare(oHistoryType.ToString, sSelectedHistoryResult) &lt;&gt; 0 Then
      MsgBox(String.Format("Der Historientyp '{0}' wurde nicht gefunden.", sSelectedHistoryResult), MsgBoxStyle.Information, m_sScriptName)
      GoTo Abbruch
    End If
    PrintStatus("Betreffliste ermitteln...")
    'm_oACTApp.ActFramework.Activities.GetRegardingPicklist(oActivityType).Items.count --- die funktion gibt menge von items zurück
    oPicklist = m_oACTApp.ActFramework.Activities.GetRegardingPicklist(oActivityType)
    If oPicklist Is Nothing Then
      MsgBox(String.Format("Der Tätigkeitstyp '{0}' hat keine Betreffliste .", sNewActivityTyp), MsgBoxStyle.Information, m_sScriptName)
      GoTo Abbruch
    End If
    For i = 0 To oPicklist.Items.Count - 1
      If Not String.IsNullOrEmpty(sValues) Then
        sValues &amp;= ";"
      Else
        sSelectedRegarding = oPicklist.Items.item(i).value 
      End If
      sValues &amp;= oPicklist.Items.item(i).value        
    Next
    'Auswahl von der Liste von Betreff
    sSelected = m_oAutoData.selectString(sValues, "Betreff", sSelectedRegarding)
    If String.IsNullOrEmpty(sSelected) Then
      GoTo abbruch
    End If
    If m_oAutoData.hasAnError Then
      Msgbox(AutoData.ErrorMessage, MsgBoxStyle.Exclamation, m_sScriptName)
      GoTo Abbruch
    End If
    oHistory.Regarding = sSelected
    oHistoryType = GetHistoryType(oActivityType, sNewHistoryResult)
    If oHistoryType Is Nothing Then
      MsgBox(String.Format("Der gewünschte Historientyp '{0}' wurde nicht gefunden .", sNewHistoryResult), MsgBoxStyle.Information, m_sScriptName)
      GoTo abbruch
    End If
    oHistory.HistoryType = oHistoryType
    oHistory.Update
    
    If oHistory.Attachment Is Nothing Then
      sAttachment = String.Empty
    Else
      sAttachment = oHistory.Attachment.FileName
    End If
   
    oContacts = oHistory.ContactList
    If oContacts Is Nothing OrElse oContacts.Count = 0 Then
      GoTo Abbruch
    End If
    'Liste von Firmen holen
    oCompanies = oHistory.CompanyList
    Dim lContactsCompanies As act.Framework.Companies.CompanyList
    
    If (LinkHistoryToCompanies = False) Then
      lContactsCompanies = m_oACTApp.ActFramework.Companies.GetLinkedCompanies(oContacts)
    Else
      Return True
    End If
    If lContactsCompanies Is Nothing OrElse lContactsCompanies.Count = 0 Then
      Msgbox("Zu der markierten Historie konnte keine Firma verlinkt werden, weil es keine gibt.", MsgBoxStyle.Exclamation, m_sScriptName)
      GoTo Abbruch
    End If
  
    If oCompanies Is Nothing OrElse oCompanies.count = 0 Then
      oHistory.CompanyList = m_oACTApp.ActFramework.Companies.GetLinkedCompanies(oContacts)
      oHistory.Update
    
    Else
      'überprüft ob die Firmen gleich sind oder nicht
      ReDim oLinkedCompanies(lContactsCompanies.count)
      For i = 0 To lContactsCompanies.Count - 1
        If  Not isCompanyinList(lContactsCompanies.GetEntity(i), oCompanies) Then
          oLinkedCompanies(i) = lContactsCompanies.GetEntity(i).ID
        Else
          ' Msgbox(String.Format("Zu die markierte Historie ist die Firma '{0}'schon verlinkt.", lContactsCompanies.GetEntity(i)), MsgBoxStyle.Information, m_sScriptName)
          GoTo Abbruch
        End If
      Next
      oHistory.CompanyList = m_oACTApp.ActFramework.Companies.GetCompaniesByID(Nothing, oLinkedCompanies)
      'm_oAutoData.ShowObjects(New Object() {oHistory.companylist})
      oHistory.Update
    End If
      
    Abbruch:
    Return String.Empty
  End Function
  
  'Die funktion überprüft, ob eine firma schon in einer Firmenliste auftaucht
  Private Shared Function isCompanyinList(oCompany As act.Framework.Companies.Company, oCompaniesList As Act.Framework.Companies.CompanyList) As Boolean
    Dim bResult As Boolean = False
    Dim i As Integer 
    For i = 0 To oCompaniesList.Count - 1
      If System.Guid.Equals(oCompany.ID, oCompaniesList.GetEntity(i).ID) Then
        bresult = True
      End If
    Next
    Return bResult
  End Function
 
  ' Lokale Variablen können in jeder Sub oder Function verwendet werden.
  ' Sollten in der Main-Function mit Werten belegt werden.
  Private Shared m_oACTApp As Act.UI.ACTApplication
  Private Shared m_sScriptName As String
  Private Shared m_oAutoData As Melville_Schellmann.ACTOptimum3.Control.AutoData3.AutoDataScript
  
  Private Shared Sub PrintStatus(sMessage As String)
    If String.IsNullOrEmpty(sMessage) Then
      m_oACTApp.StatusBar.Panels(1).text = String.Empty
    Else
      m_oACTApp.StatusBar.Panels(1).text = "ACHTUNG!!! Vorgang läuft... " &amp; sMessage  
    End If
    System.Windows.Forms.Application.DoEvents
    Threading.Thread.Sleep(300)
  End Sub
  
  Private Shared Function GetHistoryType(oActivityType As Act.Framework.Activities.ActivityType, sHistoryType As String) As Act.Framework.Histories.HistoryType
    ' Auswahl HistoryResults
    Dim oHistoryType As Act.Framework.Histories.HistoryType
    Dim oHistoryTypes() As Act.Framework.Histories.HistoryType

    
    GetHistoryType = Nothing
    oHistoryTypes = m_oACTApp.ActFramework.Histories.GetHistoryTypes(oActivityType)
    For Each oHistoryType In oHistoryTypes
      If String.Compare(sHistoryType, oHistoryType.Name, True) = 0 Then
        GetHistoryType = oHistoryType
        Exit For
      End If
    Next
  End Function
  
  Private Shared Function GetSelectedHistory() As Act.Framework.Histories.History
    Dim oContactDetailView As Act.UI.IView
    Dim oContactForm As system.Windows.Forms.Form
    Dim oGrid As system.Windows.Forms.Control
    Dim oControl As system.Windows.Forms.Control
    oContactDetailView = m_oACTApp.CurrentView
    If oContactDetailView Is Nothing Then
      MsgBox("Bitte in ACT! eine Detailansicht öffnen.", MsgBoxStyle.Information, m_sScriptName)
      GoTo Abbruch
    End If
    oContactForm = CType(oContactDetailView, system.Windows.Forms.Form)
    oControl = Nothing
    If Not TryGetControlByPath(oContactForm.Controls, "PanelTabs.Tabs.historyTab.historyCtrl.splitContainer1..theGrid", oControl) Then GoTo Abbruch
    oGrid = CType(oControl, system.Windows.Forms.Control)
    Dim lRow As Integer
    Dim oHistories As Act.Framework.Histories.HistoryList
    Dim oTemp As Object
    oTemp = Nothing
    If Not TryGetPropertyValue(oGrid, "Row", oTemp) Then GoTo Abbruch
    lRow = CType(oTemp, Integer)
    If Not TryGetPropertyValue(oGrid, "DataSource", oTemp) Then GoTo Abbruch
    If oTemp Is Nothing Then
      MsgBox("Bitte rufen Sie zuvor die Historienregisterkarte auf.", MsgBoxStyle.Information, m_sScriptName)
      GoTo Abbruch
    End If
    oHistories = CType(oTemp, Act.Framework.Histories.HistoryList)
    If lRow &gt; 0 Then
      Return oHistories.Item(lRow - 1)
    End If
    Abbruch:
    
    Return Nothing
  End Function
  Shared Function TryGetControl(ByVal Controls As system.Windows.Forms.Control.ControlCollection, ByVal ControlName As String, ByRef ResultControl As system.Windows.Forms.Control) As Boolean
    Dim oControl As system.Windows.Forms.Control
    For Each oControl In Controls
      If String.Compare(oControl.Name, ControlName, True) = 0 Then
        ResultControl = oControl
        'MsgBox(oControl.GetType.ToString &amp; " " &amp; oControl.Name)
        Return True
      End If
    Next
    MsgBox(String.Format("Das Control '{0}' wurde nicht in der Control-Auflistung vom Control '{1}' gefunden.", ControlName, Controls.Owner.Name))
    Return False
  End Function
  Shared Function TryGetControlByPath(ByVal Controls As system.Windows.Forms.Control.ControlCollection, ByVal ControlPath As String, ByRef ResultControl As system.Windows.Forms.Control) As Boolean
    Dim oControl As system.Windows.Forms.Control
    Dim sControlName As String = String.Empty
    Dim lPos As Integer
    If Controls Is Nothing Then
      Return False
    End If
    If ControlPath = String.Empty Then
      Return False
    End If
    lPos = ControlPath.IndexOf("."c)
    Select Case lPos
      Case Is &lt; 0
        sControlName = ControlPath
        ControlPath = String.Empty
      Case 0
        sControlName = String.Empty
        ControlPath = ControlPath.Substring(1)
      Case Is &gt; 0
        sControlName = ControlPath.Substring(0, lPos)
        ControlPath = ControlPath.Substring(lPos + 1)
    End Select
    For Each oControl In Controls
      If String.Compare(oControl.Name, sControlName, True) = 0 Then
        'MsgBox(oControl.GetType.ToString &amp; " Name=" &amp; oControl.Name)
        If ControlPath = String.Empty Then
          ResultControl = oControl
          Return True
        Else
          Return TryGetControlByPath(oControl.Controls, ControlPath, ResultControl)
        End If
      End If
    Next
    MsgBox(String.Format("Das Control '{0}' wurde nicht in der Control-Auflistung vom Control '{1}' gefunden.", sControlName, Controls.Owner.Name))
    Return False
  End Function
  Shared Function TryGetPropertyValue(ByVal oObject As Object, ByVal PropertyName As String, ByRef ResultObject As Object) As Boolean
    Dim oPropertyInfo As System.Reflection.PropertyInfo
    Dim oType As Type
    oType = oObject.GetType
    ResultObject = Nothing
    For Each oPropertyInfo In oType.GetProperties
      If String.Compare(oPropertyInfo.Name, PropertyName, True) = 0 Then
        Try
          ResultObject = oPropertyInfo.GetValue(oObject, Nothing)
        Catch ex As Exception
          MsgBox(ex.ToString)
          Return False
        End Try
        'MsgBox(PropertyName &amp; "=" &amp; GetPropertyValue.ToString)
        Return True
      End If
    Next
    MsgBox(String.Format("Die Eigenschaft '{0}' wurde nicht bei den Eigenschaften der Klasse '{1}' gefunden.", PropertyName, oObject.ToString))
    Return False
  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>Historientyp umwandeln</AutoDataText>
        <TextAlign>MiddleCenter</TextAlign>
        <AutoDataFormSize>160; 270</AutoDataFormSize>
        <TooltipText>Erstellt eine E-Mail aus einem Historieneintrag</TooltipText>
        <RefreshLoadedViews>False</RefreshLoadedViews>
    </AutoDataControl>
</AutoDataItems>