Imports Microsoft.Office.Interop

Public Class cGenWord
    Dim SQL As New SQL
    Dim kundenSQL As New kundenSQL

    Function openWord(pfadWord As String, visibleWord As Boolean, ByRef oWord As Microsoft.Office.Interop.Word.Application) As Microsoft.Office.Interop.Word.Document
        If Not My.Computer.FileSystem.DirectoryExists(Environment.GetFolderPath(Environment.SpecialFolder.Personal) & "\VERAG\Datentmp\") Then
            My.Computer.FileSystem.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.Personal) & "\VERAG\Datentmp\")
        End If
        ' Dim tmpPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal) & "\VERAG\tmp\tmpWord_" & Now.ToString("yyyyMMdd_HHmmss") & ".doc"
        Dim tmpPath As String = VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.getTMPPath("tmpWord", ".docx", True, False)


        Dim odoc As Microsoft.Office.Interop.Word.Document
        Try
            oWord.Visible = visibleWord 'Word sehen zum Test?
            ' MsgBox(pfadWord)
            odoc = oWord.Documents.Open(pfadWord)
            odoc.SaveAs(tmpPath)
            odoc.Activate()

            ' odoc.Save()

            Return odoc
        Catch ex As Exception
            MessageBox.Show(ex.ToString & ex.StackTrace)
        End Try
        Return Nothing
    End Function


    Public Function getWordRetourniert_SI(kdNr As Integer, datum As Date, autodruck As Boolean, Optional PrinterName As String = "", Optional AuftragsNr As Integer = 0) As Microsoft.Office.Interop.Word.Document
        Dim odoc As Microsoft.Office.Interop.Word.Document = Nothing
        Try
            Dim pfadWord = ""
            Dim oWord As New Microsoft.Office.Interop.Word.Application
            odoc = openWord(pfadWord, (Not autodruck), oWord)

            Dim dt As DataTable = SQL.loadDgvBySql("SELECT * FROM SDL WHERE KundenNr=" & kdNr & " AND SDLNr=210 AND Retourniert=1 AND RetourniertAm between '" & datum.ToShortDateString & " 00:00:00' and '" & datum.ToShortDateString & " 23:59:59'")
            Dim KUNDE = kundenSQL.getKundeFMZOLLByKdNr(kdNr)
            If KUNDE Is Nothing Then MsgBox("Kunde nicht gefunden!") : Return odoc
            '  Dim PartnerKdNrFromFMZOLL As cPartnerKdNRFMZoll = kundenSQL.getPartnerKdNrFromFMZOLL(kdNr)

            Dim oTable As Word.Table
            odoc.FormFields("Anzahl").Range.Text = dt.Rows.Count
            odoc.FormFields("KdName").Range.Text = KUNDE.Name_1 & " " & KUNDE.Name_2
            odoc.FormFields("KdStrasse").Range.Text = KUNDE.Straße
            odoc.FormFields("KdOrt").Range.Text = KUNDE.LandKz & " " & KUNDE.PLZ & " " & KUNDE.Ort
            '  odoc.FormFields("KdNrVERAG").Range.Text = KUNDE.KundenNr
            ' odoc.FormFields("KdNrMST").Range.Text = PartnerKdNrFromFMZOLL.MSEKundenNr
            odoc.FormFields("Sachbearbeiter").Range.Text = VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME
            odoc.FormFields("AuftragsNr").Range.Text = AuftragsNr.ToString("000000.##")

            If odoc.Range.Bookmarks.Exists("TabelleKarten2") Then
                If odoc.Bookmarks("TabelleKarten2").Range.Tables.Count > 0 Then
                    oTable = odoc.Bookmarks("TabelleKarten2").Range.Tables(1)
                    Dim i = 1
                    For Each r As DataRow In dt.Rows
                        oTable.Rows(i + 1).Cells(1).Range.Text = cSqlDb.checkNullStr(r("KfzKennzeichen"))
                        oTable.Rows(i + 1).Cells(2).Range.Text = cSqlDb.checkNullStr(r("SERIE-Nr"))
                        oTable.Rows(i + 1).Cells(3).Range.Text = cSqlDb.checkNullStr(r("KartenNr"))
                        oTable.Rows(i + 1).Cells(4).Range.Text = ""
                        If i < dt.Rows.Count Then oTable.Rows.Add()
                        i += 1
                    Next
                End If
            Else
                MessageBox.Show("Textmarke nicht vorhanden!")
            End If
            odoc.Save()
            odoc.Activate()

            If autodruck Then
                Dim t As New Threading.Thread(Sub()
                                                  print(odoc, PrinterName)
                                              End Sub)
                t.IsBackground = True
                t.Start()
            End If


        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try

        Return odoc
    End Function


    Public Function genUebernahmebestaetigung(dt As DataTable, kdNr As Integer, sachbearbeiter As String, autodruck As Boolean, Optional PrinterName As String = "", Optional AuftragsNr As Integer = 0, Optional ByRef pfadWord As String = "", Optional visible As Boolean = True) As Microsoft.Office.Interop.Word.Document
        Dim odoc As Microsoft.Office.Interop.Word.Document = Nothing
        Try
            pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Uebernahmebestätigung\ÜBERNAHMEBESTÄTIGUNG.dotx"
            Dim oWord As New Microsoft.Office.Interop.Word.Application
            odoc = openWord(pfadWord, (Not autodruck And visible), oWord)

            '    Dim dt As DataTable = SQL.loadDgvBySql("SELECT * FROM SDL WHERE KundenNr=" & kdNr & " AND SDLNr=210 AND Retourniert=1 AND RetourniertAm between '" & datum.ToShortDateString & " 00:00:00' and '" & datum.ToShortDateString & " 23:59:59'")
            Dim KUNDE = kundenSQL.getKundeFMZOLLByKdNr(kdNr)
            If KUNDE Is Nothing Then MsgBox("Kunde nicht gefunden!") : Return odoc
            '  Dim PartnerKdNrFromFMZOLL As cPartnerKdNRFMZoll = kundenSQL.getPartnerKdNrFromFMZOLL(kdNr)

            Dim oTable As Word.Table

            odoc.FormFields("Adresse1").Range.Text = KUNDE.Name_1 & " " & KUNDE.Name_2
            odoc.FormFields("Adresse2").Range.Text = KUNDE.Straße
            odoc.FormFields("Adresse3").Range.Text = KUNDE.LandKz & " " & KUNDE.PLZ & " " & KUNDE.Ort
            '  odoc.FormFields("KdNrVERAG").Range.Text = KUNDE.KundenNr
            ' odoc.FormFields("KdNrMST").Range.Text = PartnerKdNrFromFMZOLL.MSEKundenNr
            odoc.FormFields("Sachbearbeiter").Range.Text = VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME
            odoc.FormFields("Datum").Range.Text = Now.ToShortDateString
            odoc.FormFields("KundenNr").Range.Text = KUNDE.AdressenNr
            odoc.FormFields("AuftragsNr").Range.Text = AuftragsNr.ToString("000000.##")

            If odoc.Range.Bookmarks.Exists("TabelleKarten") Then
                If odoc.Bookmarks("TabelleKarten").Range.Tables.Count > 0 Then
                    oTable = odoc.Bookmarks("TabelleKarten").Range.Tables(1)
                    Dim i = 1
                    Dim kfzTmp = ""
                    For Each r As DataRow In dt.Rows
                        If r("KfzKennzeichen").ToString <> "" And r("KartenNr").ToString <> "" Then
                            If cSqlDb.checkNullStr(r("KfzKennzeichen")) <> kfzTmp Then
                                kfzTmp = cSqlDb.checkNullStr(r("KfzKennzeichen"))
                                oTable.Rows(i + 1).Cells(1).Range.Text = kfzTmp
                            Else
                                oTable.Rows(i + 1).Cells(1).Range.Text = ""
                            End If
                            oTable.Rows(i + 1).Cells(2).Range.Text = cSqlDb.checkNullStr(r("KarteBoxBezeichnung"))
                            oTable.Rows(i + 1).Cells(3).Range.Text = cSqlDb.checkNullStr(r("KartenNr"))
                            ' oTable.Rows(i + 1).Cells(4).Range.Text = cSqlDb.checkNullStr(r("PIN"))
                            If i < dt.Rows.Count Then oTable.Rows.Add()
                            i += 1
                        End If
                    Next
                End If
            Else
                MessageBox.Show("Textmarke nicht vorhanden!")
            End If

            odoc.Save() : odoc.Activate()


            If autodruck Then
                Dim t As New Threading.Thread(Sub()
                                                  print(odoc, PrinterName)
                                              End Sub)
                t.IsBackground = True
                t.Start()
            End If

        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
        Return odoc
    End Function



    Public Function genUebernahmebestaetigungKuvert(dt As DataTable, kdNr As Integer, sachbearbeiter As String, autodruck As Boolean, Optional PrinterName As String = "", Optional AuftragsNr As Integer = 0, Optional ByRef pfadWord As String = "", Optional visible As Boolean = True, Optional sprache As String = "") As Microsoft.Office.Interop.Word.Document
        Dim odoc As Microsoft.Office.Interop.Word.Document = Nothing
        Try
            Select Case sprache
                Case "RO"
                    pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Uebernahmebestätigung\ÜBERNAHMEBESTÄTIGUNG_Kuvert_RO.dotx"
                Case Else
                    pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Uebernahmebestätigung\ÜBERNAHMEBESTÄTIGUNG_Kuvert.dotx"
            End Select

            Dim oWord As New Microsoft.Office.Interop.Word.Application
            odoc = openWord(pfadWord, (Not autodruck And visible), oWord)

            '    Dim dt As DataTable = SQL.loadDgvBySql("SELECT * FROM SDL WHERE KundenNr=" & kdNr & " AND SDLNr=210 AND Retourniert=1 AND RetourniertAm between '" & datum.ToShortDateString & " 00:00:00' and '" & datum.ToShortDateString & " 23:59:59'")
            Dim KUNDE = kundenSQL.getKundeFMZOLLByKdNr(kdNr)
            If KUNDE Is Nothing Then MsgBox("Kunde nicht gefunden!") : Return odoc
            '  Dim PartnerKdNrFromFMZOLL As cPartnerKdNRFMZoll = kundenSQL.getPartnerKdNrFromFMZOLL(kdNr)

            Dim oTable As Word.Table

            odoc.FormFields("Adresse1").Range.Text = KUNDE.Name_1 & " " & KUNDE.Name_2
            odoc.FormFields("Adresse2").Range.Text = KUNDE.Straße
            odoc.FormFields("Adresse3").Range.Text = KUNDE.LandKz & " " & KUNDE.PLZ & " " & KUNDE.Ort
            '  odoc.FormFields("KdNrVERAG").Range.Text = KUNDE.KundenNr
            ' odoc.FormFields("KdNrMST").Range.Text = PartnerKdNrFromFMZOLL.MSEKundenNr
            odoc.FormFields("Sachbearbeiter").Range.Text = VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME
            odoc.FormFields("Datum").Range.Text = Now.ToShortDateString
            odoc.FormFields("KundenNr").Range.Text = KUNDE.AdressenNr
            odoc.FormFields("AuftragsNr").Range.Text = AuftragsNr.ToString("000000.##")

            If odoc.Range.Bookmarks.Exists("TabelleKarten") Then
                If odoc.Bookmarks("TabelleKarten").Range.Tables.Count > 0 Then
                    oTable = odoc.Bookmarks("TabelleKarten").Range.Tables(1)
                    Dim i = 1
                    Dim kfzTmp = ""
                    For Each r As DataRow In dt.Rows
                        If r("KfzKennzeichen").ToString <> "" And r("KartenNr").ToString <> "" Then
                            If cSqlDb.checkNullStr(r("KfzKennzeichen")) <> kfzTmp Then
                                kfzTmp = cSqlDb.checkNullStr(r("KfzKennzeichen"))
                                oTable.Rows(i + 1).Cells(1).Range.Text = kfzTmp
                            Else
                                oTable.Rows(i + 1).Cells(1).Range.Text = ""
                            End If
                            oTable.Rows(i + 1).Cells(2).Range.Text = cSqlDb.checkNullStr(r("KarteBoxBezeichnung"))
                            oTable.Rows(i + 1).Cells(3).Range.Text = cSqlDb.checkNullStr(r("KartenNr"))
                            ' oTable.Rows(i + 1).Cells(4).Range.Text = cSqlDb.checkNullStr(r("PIN"))
                            If i < dt.Rows.Count Then oTable.Rows.Add()
                            i += 1
                        End If
                    Next
                End If
            Else
                MessageBox.Show("Textmarke nicht vorhanden!")
            End If

            odoc.Save() : odoc.Activate()


            If autodruck Then
                Dim t As New Threading.Thread(Sub()
                                                  print(odoc, PrinterName)
                                              End Sub)
                t.IsBackground = True
                t.Start()
            End If

        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
        Return odoc
    End Function


    Sub print(odoc As Microsoft.Office.Interop.Word.Document, PrinterName As String)
        odoc.Application.WordBasic.FilePrintSetup(Printer:=PrinterName, DoNotSetAsSysDefault:=1)
        odoc.Application.PrintOut() ': odoc.Close()
        odoc.Application.Quit()
    End Sub

    Public Function genAbholbereit(dt As DataTable, kdNr As Integer, sachbearbeiter As String, language As String, autodruck As Boolean, Optional PrinterName As String = "", Optional AuftragsNr As Integer = 0, Optional ByRef pfadWord As String = "", Optional visible As Boolean = True) As Microsoft.Office.Interop.Word.Document
        Dim odoc As Microsoft.Office.Interop.Word.Document = Nothing
        Try
            pfadWord = ""
            Select Case language
                Case "DE" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\Abholbereit_DE.dotx"
                Case "YU" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\Abholbereit_YU.dotx"
                Case "RO" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\Abholbereit_RO.dotx"
                Case "TR" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\Abholbereit_TR.dotx"
                Case Else : MsgBox("Keine Sprache ausgewählt!") : Return odoc
            End Select

            Dim oWord As New Microsoft.Office.Interop.Word.Application
            odoc = openWord(pfadWord, (Not autodruck And visible), oWord)


            '    Dim dt As DataTable = SQL.loadDgvBySql("SELECT * FROM SDL WHERE KundenNr=" & kdNr & " AND SDLNr=210 AND Retourniert=1 AND RetourniertAm between '" & datum.ToShortDateString & " 00:00:00' and '" & datum.ToShortDateString & " 23:59:59'")
            Dim KUNDE = kundenSQL.getKundeFMZOLLByKdNr(kdNr)
            If KUNDE Is Nothing Then MsgBox("Kunde nicht gefunden!") : Return odoc
            '  Dim PartnerKdNrFromFMZOLL As cPartnerKdNRFMZoll = kundenSQL.getPartnerKdNrFromFMZOLL(kdNr)

            Dim oTable As Word.Table

            odoc.FormFields("Adresse1").Range.Text = KUNDE.Name_1 & " " & If(KUNDE.Name_2, "")
            odoc.FormFields("Adresse2").Range.Text = KUNDE.Straße
            odoc.FormFields("Adresse3").Range.Text = KUNDE.LandKz & " " & KUNDE.PLZ & " " & KUNDE.Ort
            If KUNDE.Telefax <> "" Then
                odoc.FormFields("Adresse5").Range.Text = "FAX: " & KUNDE.Telefax
            ElseIf KUNDE.E_Mail <> "" Then
                odoc.FormFields("Adresse5").Range.Text = "E-Mail: " & KUNDE.E_Mail
            End If


            '  odoc.FormFields("KdNrVERAG").Range.Text = KUNDE.KundenNr
            ' odoc.FormFields("KdNrMST").Range.Text = PartnerKdNrFromFMZOLL.MSEKundenNr
            odoc.FormFields("Sachbearbeiter").Range.Text = VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME
            odoc.FormFields("Datum").Range.Text = Now.ToShortDateString
            odoc.FormFields("KundenNr").Range.Text = KUNDE.AdressenNr
            odoc.FormFields("AuftragsNr").Range.Text = AuftragsNr.ToString("000000.##")

            Dim pinVisible = False
            If odoc.Range.Bookmarks.Exists("TabelleKarten") Then
                If odoc.Bookmarks("TabelleKarten").Range.Tables.Count > 0 Then
                    oTable = odoc.Bookmarks("TabelleKarten").Range.Tables(1)
                    Dim i = 1
                    Dim kfzTmp = ""
                    '   MsgBox(dt.Rows.Count)
                    For Each r As DataRow In dt.Rows
                        If r("KfzKennzeichen").ToString <> "" Or r("KartenNr").ToString <> "" Then
                            If cSqlDb.checkNullStr(r("KfzKennzeichen")) <> kfzTmp Then
                                kfzTmp = cSqlDb.checkNullStr(r("KfzKennzeichen"))
                                oTable.Rows(i + 1).Cells(1).Range.Text = kfzTmp
                            Else
                                oTable.Rows(i + 1).Cells(1).Range.Text = ""
                            End If
                            oTable.Rows(i + 1).Cells(2).Range.Text = cSqlDb.checkNullStr(r("KarteBoxBezeichnung"))
                            oTable.Rows(i + 1).Cells(3).Range.Text = cSqlDb.checkNullStr(r("KartenNr"))
                            oTable.Rows(i + 1).Cells(4).Range.Text = cSqlDb.checkNullStr(r("PIN"))
                            If i < dt.Rows.Count Then oTable.Rows.Add()
                            '    MsgBox(cSqlDb.checkNullStr(r("KarteBoxBezeichnung")))
                            i += 1

                            Select Case r("SDLNr").ToString
                                Case "100", "212", "213", "214" : pinVisible = True
                            End Select
                        End If
                    Next
                End If
            Else
                MessageBox.Show("Textmarke nicht vorhanden!")
            End If

            Try
                If Not pinVisible Then
                    odoc.FormFields("PIN").Range.Text = ""
                    odoc.FormFields("InfoPIN").Range.Text = ""
                End If
            Catch ex As Exception
            End Try

            odoc.Save() : odoc.Activate()

            If autodruck Then
                Dim t As New Threading.Thread(Sub()
                                                  print(odoc, PrinterName)
                                              End Sub)
                t.IsBackground = True
                t.Start()
            End If

        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
        Return odoc
    End Function

    Public Function genAbholbereitHUGO(dt As DataTable, kdNr As Integer, sachbearbeiter As String, language As String, autodruck As Boolean, Optional PrinterName As String = "", Optional AuftragsNr As Integer = 0, Optional ByRef pfadWord As String = "", Optional visible As Boolean = True) As Microsoft.Office.Interop.Word.Document
        Dim odoc As Microsoft.Office.Interop.Word.Document = Nothing
        Try
            pfadWord = ""
            Select Case language
                Case "DE" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\Abholbereit_HUGO_DE.dotx"
                Case "YU" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\Abholbereit_HUGO_YU.dotx"
                Case "RO" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\Abholbereit_HUGO_RO.dotx"
                    'Case "TR" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\Abholbereit_TR.dotx"
                Case Else : MsgBox("Keine Sprache ausgewählt!") : Return odoc
            End Select

            Dim oWord As New Microsoft.Office.Interop.Word.Application
            odoc = openWord(pfadWord, (Not autodruck And visible), oWord)

            '    Dim dt As DataTable = SQL.loadDgvBySql("SELECT * FROM SDL WHERE KundenNr=" & kdNr & " AND SDLNr=210 AND Retourniert=1 AND RetourniertAm between '" & datum.ToShortDateString & " 00:00:00' and '" & datum.ToShortDateString & " 23:59:59'")
            Dim KUNDE = kundenSQL.getKundeFMZOLLByKdNr(kdNr)
            If KUNDE Is Nothing Then MsgBox("Kunde nicht gefunden!") : Return odoc
            '  Dim PartnerKdNrFromFMZOLL As cPartnerKdNRFMZoll = kundenSQL.getPartnerKdNrFromFMZOLL(kdNr)

            Dim oTable As Word.Table

            odoc.FormFields("Adresse1").Range.Text = KUNDE.Name_1 & " " & KUNDE.Name_2
            odoc.FormFields("Adresse2").Range.Text = KUNDE.Straße
            odoc.FormFields("Adresse3").Range.Text = KUNDE.LandKz & " " & KUNDE.PLZ & " " & KUNDE.Ort
            If KUNDE.Telefax <> "" Then
                odoc.FormFields("Adresse5").Range.Text = "FAX: " & KUNDE.Telefax
            ElseIf KUNDE.E_Mail <> "" Then
                odoc.FormFields("Adresse5").Range.Text = "E-Mail: " & KUNDE.E_Mail
            End If


            '  odoc.FormFields("KdNrVERAG").Range.Text = KUNDE.KundenNr
            ' odoc.FormFields("KdNrMST").Range.Text = PartnerKdNrFromFMZOLL.MSEKundenNr
            odoc.FormFields("Sachbearbeiter").Range.Text = VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME
            odoc.FormFields("Datum").Range.Text = Now.ToShortDateString
            odoc.FormFields("KundenNr").Range.Text = KUNDE.AdressenNr
            odoc.FormFields("AuftragsNr").Range.Text = AuftragsNr.ToString("000000.##")

            If odoc.Range.Bookmarks.Exists("TabelleKarten") Then
                If odoc.Bookmarks("TabelleKarten").Range.Tables.Count > 0 Then
                    oTable = odoc.Bookmarks("TabelleKarten").Range.Tables(1)
                    Dim i = 1
                    For Each r As DataRow In dt.Rows
                        If r("KfzKennzeichen").ToString <> "" Then
                            oTable.Rows(i + 1).Cells(1).Range.Text = cSqlDb.checkNullStr(r("KfzKennzeichen"))
                            If i < dt.Rows.Count Then oTable.Rows.Add()
                            i += 1
                        End If
                    Next
                End If
            Else
                MessageBox.Show("Textmarke nicht vorhanden!")
            End If


            odoc.Save() : odoc.Activate()

            If autodruck Then
                Dim t As New Threading.Thread(Sub()
                                                  print(odoc, PrinterName)
                                              End Sub)
                t.IsBackground = True
                t.Start()
            End If

        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
        Return odoc
    End Function


    Public Function genAbholbereitCZ(dt As DataTable, kdNr As Integer, sachbearbeiter As String, language As String, GueltigBis As String, autodruck As Boolean, Optional PrinterName As String = "", Optional AuftragsNr As Integer = 0, Optional ByRef pfadWord As String = "", Optional visible As Boolean = True) As Microsoft.Office.Interop.Word.Document
        Dim odoc As Microsoft.Office.Interop.Word.Document = Nothing
        Try
            pfadWord = ""
            Select Case language
                Case "DE" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\CZ\Abholbereit_CZ_DE.dotx"
                Case "YU" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\CZ\Abholbereit_CZ_YU.dotx"
                Case "RO" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\CZ\Abholbereit_CZ_RO.dotx"
                Case "TR" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\CZ\Abholbereit_CZ_TR.dotx"
                Case Else : MsgBox("Keine Sprache ausgewählt!") : Return odoc
            End Select
            Dim oWord As New Microsoft.Office.Interop.Word.Application
            odoc = openWord(pfadWord, (Not autodruck And visible), oWord)

            '    Dim dt As DataTable = SQL.loadDgvBySql("SELECT * FROM SDL WHERE KundenNr=" & kdNr & " AND SDLNr=210 AND Retourniert=1 AND RetourniertAm between '" & datum.ToShortDateString & " 00:00:00' and '" & datum.ToShortDateString & " 23:59:59'")
            Dim KUNDE = kundenSQL.getKundeFMZOLLByKdNr(kdNr)
            If KUNDE Is Nothing Then MsgBox("Kunde nicht gefunden!") : Return odoc
            '  Dim PartnerKdNrFromFMZOLL As cPartnerKdNRFMZoll = kundenSQL.getPartnerKdNrFromFMZOLL(kdNr)

            Dim oTable As Word.Table

            odoc.FormFields("Adresse1").Range.Text = KUNDE.Name_1 & " " & KUNDE.Name_2
            odoc.FormFields("Adresse2").Range.Text = KUNDE.Straße
            odoc.FormFields("Adresse3").Range.Text = KUNDE.LandKz & " " & KUNDE.PLZ & " " & KUNDE.Ort
            If KUNDE.Telefax <> "" Then
                odoc.FormFields("Adresse5").Range.Text = "FAX: " & KUNDE.Telefax
            ElseIf KUNDE.E_Mail <> "" Then
                odoc.FormFields("Adresse5").Range.Text = "E-Mail: " & KUNDE.E_Mail
            End If

            '    odoc.FormFields("GueltigBis").Range.Text = GueltigBis

            '  odoc.FormFields("KdNrVERAG").Range.Text = KUNDE.KundenNr
            ' odoc.FormFields("KdNrMST").Range.Text = PartnerKdNrFromFMZOLL.MSEKundenNr
            odoc.FormFields("Sachbearbeiter").Range.Text = VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME
            odoc.FormFields("Datum").Range.Text = Now.ToShortDateString
            odoc.FormFields("KundenNr").Range.Text = KUNDE.AdressenNr
            odoc.FormFields("AuftragsNr").Range.Text = AuftragsNr.ToString("000000.##")

            If odoc.Range.Bookmarks.Exists("TabelleKarten") Then
                If odoc.Bookmarks("TabelleKarten").Range.Tables.Count > 0 Then
                    oTable = odoc.Bookmarks("TabelleKarten").Range.Tables(1)
                    Dim i = 1
                    For Each r As DataRow In dt.Rows
                        If r("KfzKennzeichen").ToString <> "" Then
                            oTable.Rows(i + 1).Cells(1).Range.Text = cSqlDb.checkNullStr(r("KfzKennzeichen"))
                            oTable.Rows(i + 1).Cells(2).Range.Text = cSqlDb.checkNullStr(r("KartenNr"))
                            If i < dt.Rows.Count Then oTable.Rows.Add()
                            i += 1
                        End If
                    Next
                End If
            Else
                MessageBox.Show("Textmarke nicht vorhanden!")
            End If


            odoc.Save() : odoc.Activate()

            If autodruck Then
                Dim t As New Threading.Thread(Sub()
                                                  print(odoc, PrinterName)
                                              End Sub)
                t.IsBackground = True
                t.Start()
            End If


        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
        Return odoc
    End Function


    Public Function genAbholbereitIDS(dt As DataTable, kdNr As Integer, sachbearbeiter As String, language As String, GueltigBis As String, autodruck As Boolean, verlaengerung As Boolean, Optional PrinterName As String = "", Optional AuftragsNr As Integer = 0, Optional ByRef pfadWord As String = "", Optional visible As Boolean = True) As Microsoft.Office.Interop.Word.Document
        Dim odoc As Microsoft.Office.Interop.Word.Document = Nothing
        Try
            pfadWord = ""
            If verlaengerung Then
                Select Case language
                    Case "DE" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\IDS\Abholbereit_IDS_DE.dotx"
                    Case "YU" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\IDS\Abholbereit_IDS_YU.dotx"
                    Case "RO" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\IDS\Abholbereit_IDS_RO.dotx"
                        '  Case "TR" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\CZ\Abholbereit_IDS_YU.dotx.dotx"
                    Case Else : MsgBox("Keine Sprache ausgewählt!") : Return odoc
                End Select
            Else
                Select Case language
                    Case "DE" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\Abholbereit_DE.dotx"
                    Case "YU" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\Abholbereit_YU.dotx"
                    Case "RO" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\Abholbereit_RO.dotx"
                        '  Case "TR" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\CZ\Abholbereit_IDS_YU.dotx.dotx"
                    Case Else : MsgBox("Keine Sprache ausgewählt!") : Return odoc
                End Select
            End If

            Dim oWord As New Microsoft.Office.Interop.Word.Application
            odoc = openWord(pfadWord, (Not autodruck And visible), oWord)

            '    Dim dt As DataTable = SQL.loadDgvBySql("SELECT * FROM SDL WHERE KundenNr=" & kdNr & " AND SDLNr=210 AND Retourniert=1 AND RetourniertAm between '" & datum.ToShortDateString & " 00:00:00' and '" & datum.ToShortDateString & " 23:59:59'")
            Dim KUNDE = kundenSQL.getKundeFMZOLLByKdNr(kdNr)
            If KUNDE Is Nothing Then MsgBox("Kunde nicht gefunden!") : Return odoc
            '  Dim PartnerKdNrFromFMZOLL As cPartnerKdNRFMZoll = kundenSQL.getPartnerKdNrFromFMZOLL(kdNr)

            Dim oTable As Word.Table

            odoc.FormFields("Adresse1").Range.Text = KUNDE.Name_1 & " " & KUNDE.Name_2
            odoc.FormFields("Adresse2").Range.Text = KUNDE.Straße
            odoc.FormFields("Adresse3").Range.Text = KUNDE.LandKz & " " & KUNDE.PLZ & " " & KUNDE.Ort
            If KUNDE.Telefax <> "" Then
                odoc.FormFields("Adresse5").Range.Text = "FAX: " & KUNDE.Telefax
            ElseIf KUNDE.E_Mail <> "" Then
                odoc.FormFields("Adresse5").Range.Text = "E-Mail: " & KUNDE.E_Mail
            End If


            '  odoc.FormFields("KdNrVERAG").Range.Text = KUNDE.KundenNr
            ' odoc.FormFields("KdNrMST").Range.Text = PartnerKdNrFromFMZOLL.MSEKundenNr
            odoc.FormFields("Sachbearbeiter").Range.Text = VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME
            odoc.FormFields("Datum").Range.Text = Now.ToShortDateString
            odoc.FormFields("KundenNr").Range.Text = KUNDE.AdressenNr
            odoc.FormFields("AuftragsNr").Range.Text = AuftragsNr.ToString("000000.##")

            If verlaengerung Then
                odoc.FormFields("GueltigBis").Range.Text = GueltigBis
            End If


            If odoc.Range.Bookmarks.Exists("TabelleKarten") Then
                If odoc.Bookmarks("TabelleKarten").Range.Tables.Count > 0 Then
                    oTable = odoc.Bookmarks("TabelleKarten").Range.Tables(1)
                    Dim i = 1
                    For Each r As DataRow In dt.Rows
                        If r("KfzKennzeichen").ToString <> "" Then
                            If verlaengerung Then
                                oTable.Rows(i + 1).Cells(1).Range.Text = cSqlDb.checkNullStr(r("KfzKennzeichen"))
                                oTable.Rows(i + 1).Cells(2).Range.Text = cSqlDb.checkNullStr(r("PIN"))
                            Else
                                oTable.Rows(i + 1).Cells(1).Range.Text = cSqlDb.checkNullStr(r("KfzKennzeichen"))
                                oTable.Rows(i + 1).Cells(2).Range.Text = cSqlDb.checkNullStr(r("KarteBoxBezeichnung"))
                                oTable.Rows(i + 1).Cells(3).Range.Text = cSqlDb.checkNullStr(r("KartenNr"))
                                oTable.Rows(i + 1).Cells(4).Range.Text = cSqlDb.checkNullStr(r("PIN"))
                            End If

                            '   oTable.Rows(i + 1).Cells(3).Range.Text = cSqlDb.checkNullStr(r(2))
                            If i < dt.Rows.Count Then oTable.Rows.Add()
                            i += 1
                        End If
                    Next
                End If
            Else
                MessageBox.Show("Textmarke nicht vorhanden!")
            End If


            odoc.Save() : odoc.Activate()

            If autodruck Then
                Dim t As New Threading.Thread(Sub()
                                                  print(odoc, PrinterName)
                                              End Sub)
                t.IsBackground = True
                t.Start()
            End If

        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
        Return odoc
    End Function

    Public Function genAbholbereitMSE(dt As DataTable, kdNr As Integer, sachbearbeiter As String, language As String, GueltigBis As String, autodruck As Boolean, verlaengerung As Boolean, Optional PrinterName As String = "", Optional AuftragsNr As Integer = 0, Optional ByRef pfadWord As String = "", Optional visible As Boolean = True) As Microsoft.Office.Interop.Word.Document
        Dim odoc As Microsoft.Office.Interop.Word.Document = Nothing
        Try
            pfadWord = ""
            If verlaengerung Then
                Select Case language
                    Case "DE" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\MSE\Abholbereit_MSE_DE.dotx"
                    Case "YU" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\MSE\Abholbereit_MSE_YU.dotx"
                    Case "RO" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\MSE\Abholbereit_MSE_RO.dotx"
                        '  Case "TR" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\CZ\Abholbereit_IDS_YU.dotx.dotx"
                    Case Else : MsgBox("Keine Sprache ausgewählt!") : Return odoc
                End Select
            Else
                Select Case language
                    Case "DE" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\Abholbereit_DE.dotx"
                    Case "YU" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\Abholbereit_YU.dotx"
                    Case "RO" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\Abholbereit_RO.dotx"
                        '  Case "TR" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\CZ\Abholbereit_IDS_YU.dotx.dotx"
                    Case Else : MsgBox("Keine Sprache ausgewählt!") : Return odoc
                End Select
            End If

            Dim oWord As New Microsoft.Office.Interop.Word.Application
            odoc = openWord(pfadWord, (Not autodruck And visible), oWord)


            '    Dim dt As DataTable = SQL.loadDgvBySql("SELECT * FROM SDL WHERE KundenNr=" & kdNr & " AND SDLNr=210 AND Retourniert=1 AND RetourniertAm between '" & datum.ToShortDateString & " 00:00:00' and '" & datum.ToShortDateString & " 23:59:59'")
            Dim KUNDE = kundenSQL.getKundeFMZOLLByKdNr(kdNr)
            If KUNDE Is Nothing Then MsgBox("Kunde nicht gefunden!") : Return odoc
            '  Dim PartnerKdNrFromFMZOLL As cPartnerKdNRFMZoll = kundenSQL.getPartnerKdNrFromFMZOLL(kdNr)

            Dim oTable As Word.Table

            odoc.FormFields("Adresse1").Range.Text = KUNDE.Name_1 & " " & KUNDE.Name_2
            odoc.FormFields("Adresse2").Range.Text = KUNDE.Straße
            odoc.FormFields("Adresse3").Range.Text = KUNDE.LandKz & " " & KUNDE.PLZ & " " & KUNDE.Ort
            If KUNDE.Telefax <> "" Then
                odoc.FormFields("Adresse5").Range.Text = "FAX: " & KUNDE.Telefax
            ElseIf KUNDE.E_Mail <> "" Then
                odoc.FormFields("Adresse5").Range.Text = "E-Mail: " & KUNDE.E_Mail
            End If

            If verlaengerung Then
                odoc.FormFields("GueltigBis").Range.Text = GueltigBis
            End If
            '  odoc.FormFields("KdNrVERAG").Range.Text = KUNDE.KundenNr
            ' odoc.FormFields("KdNrMST").Range.Text = PartnerKdNrFromFMZOLL.MSEKundenNr
            odoc.FormFields("Sachbearbeiter").Range.Text = VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME
            odoc.FormFields("Datum").Range.Text = Now.ToShortDateString
            odoc.FormFields("KundenNr").Range.Text = KUNDE.AdressenNr
            odoc.FormFields("AuftragsNr").Range.Text = AuftragsNr.ToString("000000.##")

            If odoc.Range.Bookmarks.Exists("TabelleKarten") Then
                If odoc.Bookmarks("TabelleKarten").Range.Tables.Count > 0 Then
                    oTable = odoc.Bookmarks("TabelleKarten").Range.Tables(1)
                    Dim i = 1
                    For Each r As DataRow In dt.Rows
                        If r("KfzKennzeichen").ToString <> "" Then
                            If verlaengerung Then
                                oTable.Rows(i + 1).Cells(1).Range.Text = cSqlDb.checkNullStr(r("KfzKennzeichen"))
                            Else
                                oTable.Rows(i + 1).Cells(1).Range.Text = cSqlDb.checkNullStr(r("KfzKennzeichen"))
                                oTable.Rows(i + 1).Cells(2).Range.Text = cSqlDb.checkNullStr(r("KarteBoxBezeichnung"))
                                oTable.Rows(i + 1).Cells(3).Range.Text = cSqlDb.checkNullStr(r("KartenNr"))
                                oTable.Rows(i + 1).Cells(4).Range.Text = cSqlDb.checkNullStr(r("PIN"))
                            End If

                            '   oTable.Rows(i + 1).Cells(3).Range.Text = cSqlDb.checkNullStr(r(2))
                            If i < dt.Rows.Count Then oTable.Rows.Add()
                            i += 1
                        End If
                    Next
                End If
            Else
                MessageBox.Show("Textmarke nicht vorhanden!")
            End If


            odoc.Save() : odoc.Activate()

            If autodruck Then
                Dim t As New Threading.Thread(Sub()
                                                  print(odoc, PrinterName)
                                              End Sub)
                t.IsBackground = True
                t.Start()
            End If


        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
        Return odoc
    End Function

    Public Function genAbholbereitUTA(dt As DataTable, kdNr As Integer, sachbearbeiter As String, language As String, GueltigBis As String, autodruck As Boolean, verlaengerung As Boolean, Optional PrinterName As String = "", Optional AuftragsNr As Integer = 0, Optional ByRef pfadWord As String = "", Optional visible As Boolean = True) As Microsoft.Office.Interop.Word.Document
        Dim odoc As Microsoft.Office.Interop.Word.Document = Nothing
        Try
            pfadWord = ""
            If verlaengerung Then
                Select Case language
                    Case "DE" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\UTA\Abholbereit_UTA_DE.dotx"
                    Case "YU" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\UTA\Abholbereit_UTA_YU.dotx"
                    Case "RO" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\UTA\Abholbereit_UTA_RO.dotx"
                    Case "TR" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\UTA\Abholbereit_UTA_TR.dotx"
                    Case Else : MsgBox("Keine Sprache ausgewählt!") : Return odoc
                End Select
            Else
                Select Case language
                    Case "DE" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\Abholbereit_DE.dotx"
                    Case "YU" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\Abholbereit_YU.dotx"
                    Case "RO" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\Abholbereit_RO.dotx"
                    Case "TR" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\Abholbereit_TR.dotx"
                        '  Case "TR" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\CZ\Abholbereit_IDS_YU.dotx.dotx"
                    Case Else : MsgBox("Keine Sprache ausgewählt!") : Return odoc
                End Select
            End If

            Dim oWord As New Microsoft.Office.Interop.Word.Application
            odoc = openWord(pfadWord, (Not autodruck And visible), oWord)


            '    Dim dt As DataTable = SQL.loadDgvBySql("SELECT * FROM SDL WHERE KundenNr=" & kdNr & " AND SDLNr=210 AND Retourniert=1 AND RetourniertAm between '" & datum.ToShortDateString & " 00:00:00' and '" & datum.ToShortDateString & " 23:59:59'")
            Dim KUNDE = kundenSQL.getKundeFMZOLLByKdNr(kdNr)
            If KUNDE Is Nothing Then MsgBox("Kunde nicht gefunden!") : Return odoc
            '  Dim PartnerKdNrFromFMZOLL As cPartnerKdNRFMZoll = kundenSQL.getPartnerKdNrFromFMZOLL(kdNr)

            Dim oTable As Word.Table

            odoc.FormFields("Adresse1").Range.Text = KUNDE.Name_1 & " " & KUNDE.Name_2
            odoc.FormFields("Adresse2").Range.Text = KUNDE.Straße
            odoc.FormFields("Adresse3").Range.Text = KUNDE.LandKz & " " & KUNDE.PLZ & " " & KUNDE.Ort
            If KUNDE.Telefax <> "" Then
                odoc.FormFields("Adresse5").Range.Text = "FAX: " & KUNDE.Telefax
            ElseIf KUNDE.E_Mail <> "" Then
                odoc.FormFields("Adresse5").Range.Text = "E-Mail: " & KUNDE.E_Mail
            End If

            If verlaengerung Then odoc.FormFields("GueltigBis").Range.Text = GueltigBis

            '  odoc.FormFields("KdNrVERAG").Range.Text = KUNDE.KundenNr
            ' odoc.FormFields("KdNrMST").Range.Text = PartnerKdNrFromFMZOLL.MSEKundenNr
            odoc.FormFields("Sachbearbeiter").Range.Text = VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME
            odoc.FormFields("Datum").Range.Text = Now.ToShortDateString
            odoc.FormFields("KundenNr").Range.Text = KUNDE.AdressenNr
            odoc.FormFields("AuftragsNr").Range.Text = AuftragsNr.ToString("000000.##")

            If odoc.Range.Bookmarks.Exists("TabelleKarten") Then
                If odoc.Bookmarks("TabelleKarten").Range.Tables.Count > 0 Then
                    oTable = odoc.Bookmarks("TabelleKarten").Range.Tables(1)
                    Dim i = 1
                    For Each r As DataRow In dt.Rows
                        If r("KfzKennzeichen").ToString <> "" Then
                            If verlaengerung Then
                                oTable.Rows(i + 1).Cells(1).Range.Text = cSqlDb.checkNullStr(r("KfzKennzeichen"))
                            Else
                                oTable.Rows(i + 1).Cells(1).Range.Text = cSqlDb.checkNullStr(r("KfzKennzeichen"))
                                oTable.Rows(i + 1).Cells(2).Range.Text = cSqlDb.checkNullStr(r("KarteBoxBezeichnung"))
                                oTable.Rows(i + 1).Cells(3).Range.Text = cSqlDb.checkNullStr(r("KartenNr"))
                                oTable.Rows(i + 1).Cells(4).Range.Text = cSqlDb.checkNullStr(r("PIN"))
                            End If

                            '   oTable.Rows(i + 1).Cells(3).Range.Text = cSqlDb.checkNullStr(r(2))
                            If i < dt.Rows.Count Then oTable.Rows.Add()
                            i += 1
                        End If
                    Next
                End If
            Else
                MessageBox.Show("Textmarke nicht vorhanden!")
            End If


            odoc.Save() : odoc.Activate()

            If autodruck Then
                Dim t As New Threading.Thread(Sub()
                                                  print(odoc, PrinterName)
                                              End Sub)
                t.IsBackground = True
                t.Start()
            End If


        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
        Return odoc
    End Function

    Public Function genAbholbereitPL(dt As DataTable, kdNr As Integer, sachbearbeiter As String, language As String, GueltigBis As String, autodruck As Boolean, Optional PrinterName As String = "", Optional AuftragsNr As Integer = 0, Optional ByRef pfadWord As String = "", Optional visible As Boolean = True) As Microsoft.Office.Interop.Word.Document
        Dim odoc As Microsoft.Office.Interop.Word.Document = Nothing
        Try
            pfadWord = ""
            Select Case language
                Case "DE" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\PL\Abholbereit_PL_DE.dotx"
                Case "YU" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\PL\Abholbereit_PL_YU.dotx"
                    ' Case "RO" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\PL\Abholbereit_PL_RO.dotx"
                Case "TR" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\PL\Abholbereit_PL_TR.dotx"
                Case Else : MsgBox("Keine Sprache ausgewählt!") : Return odoc
            End Select

            Dim oWord As New Microsoft.Office.Interop.Word.Application
            odoc = openWord(pfadWord, (Not autodruck And visible), oWord)

            '    Dim dt As DataTable = SQL.loadDgvBySql("SELECT * FROM SDL WHERE KundenNr=" & kdNr & " AND SDLNr=210 AND Retourniert=1 AND RetourniertAm between '" & datum.ToShortDateString & " 00:00:00' and '" & datum.ToShortDateString & " 23:59:59'")
            Dim KUNDE = kundenSQL.getKundeFMZOLLByKdNr(kdNr)
            If KUNDE Is Nothing Then MsgBox("Kunde nicht gefunden!") : Return odoc
            '  Dim PartnerKdNrFromFMZOLL As cPartnerKdNRFMZoll = kundenSQL.getPartnerKdNrFromFMZOLL(kdNr)

            Dim oTable As Word.Table

            odoc.FormFields("Adresse1").Range.Text = KUNDE.Name_1 & " " & KUNDE.Name_2
            odoc.FormFields("Adresse2").Range.Text = KUNDE.Straße
            odoc.FormFields("Adresse3").Range.Text = KUNDE.LandKz & " " & KUNDE.PLZ & " " & KUNDE.Ort
            If KUNDE.Telefax <> "" Then
                odoc.FormFields("Adresse5").Range.Text = "FAX: " & KUNDE.Telefax
            ElseIf KUNDE.E_Mail <> "" Then
                odoc.FormFields("Adresse5").Range.Text = "E-Mail: " & KUNDE.E_Mail
            End If

            '    odoc.FormFields("GueltigBis").Range.Text = GueltigBis

            '  odoc.FormFields("KdNrVERAG").Range.Text = KUNDE.KundenNr
            ' odoc.FormFields("KdNrMST").Range.Text = PartnerKdNrFromFMZOLL.MSEKundenNr
            odoc.FormFields("Sachbearbeiter").Range.Text = VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME
            odoc.FormFields("Datum").Range.Text = Now.ToShortDateString
            odoc.FormFields("KundenNr").Range.Text = KUNDE.AdressenNr
            odoc.FormFields("AuftragsNr").Range.Text = AuftragsNr.ToString("000000.##")


            Dim MSEKdNr = kundenSQL.getPartnerKdNrFromFMZOLL(kdNr).MSEKundenNr

            If odoc.Range.Bookmarks.Exists("TabelleKarten") Then
                If odoc.Bookmarks("TabelleKarten").Range.Tables.Count > 0 Then
                    oTable = odoc.Bookmarks("TabelleKarten").Range.Tables(1)
                    Dim i = 1
                    For Each r As DataRow In dt.Rows
                        If r("KfzKennzeichen").ToString <> "" Then

                            oTable.Rows(i + 1).Cells(1).Range.Text = cSqlDb.checkNullStr(r("KfzKennzeichen"))
                            oTable.Rows(i + 1).Cells(2).Range.Text = cSqlDb.checkNullStr(r("KartenNr"))
                            '    oTable.Rows(i + 1).Cells(2).Range.Text = MSEKdNr

                            '   oTable.Rows(i + 1).Cells(3).Range.Text = cSqlDb.checkNullStr(r(2))
                            If i < dt.Rows.Count Then oTable.Rows.Add()
                            i += 1

                        End If
                    Next
                End If
            Else
                MessageBox.Show("Textmarke nicht vorhanden!")
            End If


            odoc.Save() : odoc.Activate()

            If autodruck Then
                Dim t As New Threading.Thread(Sub()
                                                  print(odoc, PrinterName)
                                              End Sub)
                t.IsBackground = True
                t.Start()
            End If


        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
        Return odoc
    End Function

    Public Function genAbholbereitCZPL(dt As DataTable, kdNr As Integer, sachbearbeiter As String, language As String, GueltigBis As String, autodruck As Boolean, Optional PrinterName As String = "", Optional AuftragsNr As Integer = 0, Optional ByRef pfadWord As String = "", Optional visible As Boolean = True) As Microsoft.Office.Interop.Word.Document
        Dim odoc As Microsoft.Office.Interop.Word.Document = Nothing
        Try
            pfadWord = ""
            Select Case language
                '  Case "DE" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\CZ\Abholbereit_IDS_YU.dotx.dotx"
                Case "YU" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\CZ\Abholbereit_CZ+PL_YU.dotx"
                    ' Case "RO" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\PL\Abholbereit_PL_RO.dotx"
                Case "TR" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\CZ\Abholbereit_CZ+PL_TR.dotx"
                Case Else : MsgBox("Keine Sprache ausgewählt!") : Return odoc
            End Select

            Dim oWord As New Microsoft.Office.Interop.Word.Application
            odoc = openWord(pfadWord, (Not autodruck And visible), oWord)

            '    Dim dt As DataTable = SQL.loadDgvBySql("SELECT * FROM SDL WHERE KundenNr=" & kdNr & " AND SDLNr=210 AND Retourniert=1 AND RetourniertAm between '" & datum.ToShortDateString & " 00:00:00' and '" & datum.ToShortDateString & " 23:59:59'")
            Dim KUNDE = kundenSQL.getKundeFMZOLLByKdNr(kdNr)
            If KUNDE Is Nothing Then MsgBox("Kunde nicht gefunden!") : Return odoc
            '  Dim PartnerKdNrFromFMZOLL As cPartnerKdNRFMZoll = kundenSQL.getPartnerKdNrFromFMZOLL(kdNr)

            Dim oTable As Word.Table

            odoc.FormFields("Adresse1").Range.Text = KUNDE.Name_1 & " " & KUNDE.Name_2
            odoc.FormFields("Adresse2").Range.Text = KUNDE.Straße
            odoc.FormFields("Adresse3").Range.Text = KUNDE.LandKz & " " & KUNDE.PLZ & " " & KUNDE.Ort
            If KUNDE.Telefax <> "" Then
                odoc.FormFields("Adresse5").Range.Text = "FAX: " & KUNDE.Telefax
            ElseIf KUNDE.E_Mail <> "" Then
                odoc.FormFields("Adresse5").Range.Text = "E-Mail: " & KUNDE.E_Mail
            End If

            ' odoc.FormFields("GueltigBis").Range.Text = GueltigBis

            '  odoc.FormFields("KdNrVERAG").Range.Text = KUNDE.KundenNr
            ' odoc.FormFields("KdNrMST").Range.Text = PartnerKdNrFromFMZOLL.MSEKundenNr
            odoc.FormFields("Sachbearbeiter").Range.Text = VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME
            odoc.FormFields("Datum").Range.Text = Now.ToShortDateString
            odoc.FormFields("KundenNr").Range.Text = KUNDE.AdressenNr
            odoc.FormFields("AuftragsNr").Range.Text = AuftragsNr.ToString("000000.##")


            Dim MSEKdNr = kundenSQL.getPartnerKdNrFromFMZOLL(kdNr).MSEKundenNr

            If odoc.Range.Bookmarks.Exists("TabelleKarten") Then
                If odoc.Bookmarks("TabelleKarten").Range.Tables.Count > 0 Then
                    oTable = odoc.Bookmarks("TabelleKarten").Range.Tables(1)
                    Dim i = 1
                    For Each r As DataRow In dt.Rows
                        If r("KfzKennzeichen").ToString <> "" Then

                            oTable.Rows(i + 1).Cells(1).Range.Text = cSqlDb.checkNullStr(r("KfzKennzeichen"))
                            oTable.Rows(i + 1).Cells(2).Range.Text = cSqlDb.checkNullStr(r("KartenNr"))
                            '     oTable.Rows(i + 1).Cells(2).Range.Text = MSEKdNr

                            '   oTable.Rows(i + 1).Cells(3).Range.Text = cSqlDb.checkNullStr(r(2))
                            If i < dt.Rows.Count Then oTable.Rows.Add()
                            i += 1

                        End If
                    Next
                End If
            Else
                MessageBox.Show("Textmarke nicht vorhanden!")
            End If


            odoc.Save() : odoc.Activate()

            If autodruck Then
                Dim t As New Threading.Thread(Sub()
                                                  print(odoc, PrinterName)
                                              End Sub)
                t.IsBackground = True
                t.Start()
            End If


        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
        Return odoc
    End Function

    Public Function genBestellung_MAN(dt As DataTable, kdNr As Integer, sachbearbeiter As String, language As String, autodruck As Boolean, Optional PrinterName As String = "", Optional ByRef pfadWord As String = "", Optional visible As Boolean = True) As Microsoft.Office.Interop.Word.Document
        Dim odoc As Microsoft.Office.Interop.Word.Document = Nothing
        Try

            pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Bestellfomulare\Bestellung_MAN_Abgasplakette.dotx "


            Dim oWord As New Microsoft.Office.Interop.Word.Application
            odoc = openWord(pfadWord, (Not autodruck And visible), oWord)

            '    Dim dt As DataTable = SQL.loadDgvBySql("SELECT * FROM SDL WHERE KundenNr=" & kdNr & " AND SDLNr=210 AND Retourniert=1 AND RetourniertAm between '" & datum.ToShortDateString & " 00:00:00' and '" & datum.ToShortDateString & " 23:59:59'")
            Dim KUNDE = kundenSQL.getKundeFMZOLLByKdNr(kdNr)
            If KUNDE Is Nothing Then MsgBox("Kunde nicht gefunden!") : Return odoc
            '  Dim PartnerKdNrFromFMZOLL As cPartnerKdNRFMZoll = kundenSQL.getPartnerKdNrFromFMZOLL(kdNr)

            Dim oTable As Word.Table

            odoc.FormFields("Kunde").Range.Text = KUNDE.Ordnungsbegriff
           
            odoc.FormFields("Sachbearbeiter").Range.Text = VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME
            odoc.FormFields("Datum").Range.Text = Now.ToShortDateString
            odoc.FormFields("KundenNr").Range.Text = KUNDE.AdressenNr
            odoc.FormFields("KundenNr2").Range.Text = KUNDE.AdressenNr


            If odoc.Range.Bookmarks.Exists("TabelleKarten") Then
                If odoc.Bookmarks("TabelleKarten").Range.Tables.Count > 0 Then
                    oTable = odoc.Bookmarks("TabelleKarten").Range.Tables(1)
                    Dim i = 1
                    For Each r As DataRow In dt.Rows
                        If r("KfzKennzeichen").ToString <> "" Then

                            oTable.Rows(i + 1).Cells(1).Range.Text = i
                            oTable.Rows(i + 1).Cells(2).Range.Text = cSqlDb.checkNullStr(r("KfzKennzeichen"))
                             
                            If i < dt.Rows.Count Then oTable.Rows.Add()
                            i += 1

                        End If
                    Next
                End If
            Else
                MessageBox.Show("Textmarke nicht vorhanden!")
            End If


            odoc.Save() : odoc.Activate()

            If autodruck Then
                Dim t As New Threading.Thread(Sub()
                                                  print(odoc, PrinterName)
                                              End Sub)
                t.IsBackground = True
                t.Start()
            End If


        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
        Return odoc
    End Function

End Class
