Imports Microsoft.Office.Interop.Word
Public Class ClassOfficeTemplate
    Public Shared Function Open_Word_Template(Template_ID As Integer, Record_ID As Integer)
        Try
            Dim sql_Template As String = "SELECT * FROM VWPMO_TEMPLATE_ENTITY WHERE TEMPLATE_ID = " & Template_ID
            Dim DT_TEMPLATE As System.Data.DataTable = ClassDatabase.Return_Datatable(sql_Template)
            Dim sql_patterns As String = String.Format("select * from TBPMO_TEMPLATE_PATTERN where TEMPLATE_ENT_ID = (SELECT GUID FROM TBPMO_TEMPLATE_ENTITY WHERE ENTITY_ID = {0}  AND TEMPLATE_ID = {1})", CURRENT_FORM_ID, Template_ID)
            Dim DT_PATTERNS As System.Data.DataTable = ClassDatabase.Return_Datatable(sql_patterns)

            If IsNothing(DT_PATTERNS) Then
                MsgBox("Keine Patterns für diese Vorlage definiert. Bitte prüfen Sie die Konfiguration!", MsgBoxStyle.Critical)
                Return False
            End If

            Dim Original_Vorlage As String = DT_TEMPLATE.Rows(0).Item("TEMPLATE_PATH")
            Dim DOCTYPE_ID As Integer = ClassDatabase.Execute_Scalar("SELECT DOCTYPE_ID FROM TBPMO_TEMPLATE WHERE GUID = " & Template_ID, True)

            If System.IO.File.Exists(Original_Vorlage) = False Then
                MsgBox("Achtung: die Vorlage " & vbNewLine & Original_Vorlage & vbNewLine & "existiert nicht!", MsgBoxStyle.Critical)
                Return False
            End If
            Dim tempdoc = ClassHelper.Get_TempFilename & ".docx"
            System.IO.File.Copy(Original_Vorlage, tempdoc)

            Dim word As New Microsoft.Office.Interop.Word.Application
            Dim doc As Microsoft.Office.Interop.Word.Document

            doc = word.Documents.Open(tempdoc)
            doc.Activate()
            Dim _BuiltInProperties As Object = doc.BuiltInDocumentProperties
            If Not _BuiltInProperties Is Nothing Then
                '_BuiltInProperties("Title").Value = txtTitle.Text.Trim()
                '_BuiltInProperties("Subject").Value = txtSubject.Text.Trim()
                ' _BuiltInProperties("Author").Value = txtAuthor.Text.Trim()
                '_BuiltInProperties("Manager").Value = txtManager.Text.Trim()
                '_BuiltInProperties("Company").Value = txtCompany.Text.Trim()
                _BuiltInProperties("Category").Value = DT_TEMPLATE.Rows(0).Item("TEMPLATE_NAME")
                '_BuiltInProperties("Keywords").Value = txtKeyWords.Text.Trim()
                _BuiltInProperties("Comments").Value = "RECORD_ID:" & CURRENT_RECORD_ID.ToString
            End If
            ' word.Visible = (True) 'Word sichtbar

            For Each cc In doc.ContentControls
                If LogErrorsOnly = False Then ClassLogger.Add(">> Office-Pattern: " & cc.Tag, False)
                For Each patternrow As System.Data.DataRow In DT_PATTERNS.Rows
                    If patternrow.Item("NAME_PATTERN").ToString.ToUpper = cc.Tag.ToString.ToUpper Then
                        If Not IsDBNull(patternrow.Item("FIXED_VALUE")) Then
                            'Fixed Value ist gefüllt
                            Dim fvalue As String = patternrow.Item("FIXED_VALUE").ToString.ToUpper
                            If fvalue <> "" Then
                                ' If fvalue.Contains("@") Then
                                'If fvalue.Contains("CONTROLID") Then
                                Dim Control_ID = fvalue '.Replace("@CONTROLID:", "")
                                Try
                                    If CInt(Control_ID) Then
                                        If LogErrorsOnly = False Then ClassLogger.Add(">> Get Value for Control-ID: " & Control_ID.ToString, False)
                                        Dim value_akt = ClassDatabase.Execute_Scalar("SELECT VALUE FROM TBPMO_CONTROL_VALUE WHERE CONTROL_ID = " & Control_ID & " AND RECORD_ID = " & Record_ID)
                                        If Not IsNothing(value_akt) Then
                                            cc.Range.Text = value_akt.ToString
                                        Else
                                            ClassLogger.Add("Für Control-ID '" & Control_ID & "' konnte kein Wert gelesen werden!", True)
                                        End If
                                    End If
                                Catch ex As Exception
                                    ClassLogger.Add("Unerwarteter Fehler beim Auslesen eines Control-Wertes in Pattern-For: " & ex.Message, True)
                                End Try
                                'End If
                                'Else
                                '    If LogErrorsOnly = False Then ClassLogger.Add(">> Pattern wird mit festem Wert '" & patternrow.Item("FIXED_VALUE") & "' gefüllt!", False)
                                '    cc.Range.Text = patternrow.Item("FIXED_VALUE").ToString
                                'End If
                            End If
                        Else 'Kein Fixed Value, also wohl eher sql-result
                            If Not IsDBNull(patternrow.Item("CONNECTION_ID")) Then
                                If Not IsDBNull(patternrow.Item("SQL_COMMAND")) Then

                                End If

                            End If
                        End If

                    End If
                Next
            Next cc
            '  doc.ContentControls().Item("Name").Range.Text = "Schreiber"
            'Textmarken füllen
            '    doc.().Item("Name").Range.Text = "Schreiber"
            'doc.Bookmarks().Item("Vorname").Range.Text = "Marlon"
            'doc.Bookmarks().Item("Produktion").Range.Text = "Testproduktion"

            doc.SaveAs(tempdoc)
            TEMP_FILES.Add(tempdoc)
            'doc.Close()
            'speichern / drucken
            'doc.SaveAs("c:\Proforma.doc")
            'doc.PrintOut()
            doc.Close()
            doc = Nothing
            word.Quit()

            If ClassImport_Windream.Import_File(tempdoc, DOCTYPE_ID) = True Then
                System.IO.File.Delete(tempdoc)
                Process.Start(CURRENT_FILEIN_WD)
                Return True
            Else
                MsgBox("Die Vorlage konnte nicht nach windream importiert werden. Die Datei wird dennoch geöffnet!", MsgBoxStyle.Exclamation)
                Process.Start(tempdoc)
                Return False
            End If
        Catch ex As Exception
            ClassLogger.Add("Error in Open_Word_Template: " & ex.Message, True)
            MsgBox("Unexpected Error in Open_Word_Template:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            Return False
        End Try
    End Function
    Private Func
End Class
