Option Strict Off
Imports System.Data.SqlClient
Imports System.IO
Imports System.Net
Imports Microsoft.Office.Interop.Excel.XlUnderlineStyle
Imports Microsoft.Office.Interop.Excel.Constants
Imports Microsoft.Office.Interop.Excel.XlBordersIndex
Imports Microsoft.Office.Interop.Excel.XlLineStyle
Imports Microsoft.Office.Interop.Excel.XlBorderWeight
Imports Microsoft.Office.Interop.Excel.XlThemeFont
Imports Microsoft.Office.Interop.Excel.XlThemeColor
Imports Microsoft.Office.Interop.Excel.XlWindowState
Imports Microsoft.Office.Interop.Excel
Public Class 模具開發控制表
    Public Property Credentials As ICredentials
    ReadOnly ds As New DataSet : ReadOnly ds1 As New DataSet : ReadOnly ds2 As New DataSet : ReadOnly ds3 As New DataSet
    Dim DTP As String
    Dim xlApp As Application : Dim xlBook As Workbook : Dim xlSheet As Worksheet
    Private Sub Set_DGV1載入前設定()
        DataGridView1.DataSource = Nothing : ds.Clear()
        DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView1.ColumnHeadersHeight = 25
        DataGridView1.AllowUserToAddRows = False
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
    End Sub
    Private Sub Set_DGV1載入後設定()
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds) : DataGridView1.DataSource = ds.Tables(0) : conn.Close()
    End Sub
    Private Sub Set_楦頭規格表清單()
        Set_DGV1載入前設定()
        If PA = "" Then
            SQL1 = "SELECT   模具開發單號, 結構, 材質, 楦頭編號, 模具編號, 參考原樣鞋編號, 品牌, 大底廠, 尺寸, 季節, 備註1 AS 備註, 制表, 制表日期, 模具廠, CN
                    FROM     模具開發控制表 
                    ORDER BY 楦頭編號"
        Else
            SQL1 = "SELECT   模具開發單號, 結構, 材質, 楦頭編號, 模具編號, 參考原樣鞋編號, 品牌, 大底廠, 尺寸, 季節, 備註1 AS 備註, 制表, 制表日期, 模具廠, CN
                    FROM     模具開發控制表 
                    WHERE    模具開發單號 LIKE N'%" & PA & "%' OR 結構 LIKE N'%" & PA & "%' OR 材質 LIKE N'%" & PA & "%' OR 楦頭編號 LIKE N'%" & PA & "%' OR 
                             模具編號 LIKE N'%" & PA & "%' OR 參考原樣鞋編號 LIKE N'%" & PA & "%' OR 品牌 LIKE N'%" & PA & "%' OR 大底廠 LIKE N'%" & PA & "%' OR 
                             尺寸 LIKE N'%" & PA & "%' OR 季節 LIKE N'%" & PA & "%'
                    ORDER BY 楦頭編號"
        End If
        Set_DGV1載入後設定() : Set_grid()
    End Sub
    Private Sub Set_grid()
        DataGridView1.Columns(0).Width = 142 : DataGridView1.Columns(1).Visible = False : DataGridView1.Columns(2).Visible = False : DataGridView1.Columns(3).Visible = False
        DataGridView1.Columns(4).Width = 142 : DataGridView1.Columns(5).Visible = False : DataGridView1.Columns(6).Visible = False : DataGridView1.Columns(7).Visible = False
        DataGridView1.Columns(8).Visible = False : DataGridView1.Columns(9).Visible = False : DataGridView1.Columns(10).Visible = False : DataGridView1.Columns(11).Visible = False
        DataGridView1.Columns(12).Visible = False : DataGridView1.Columns(13).Visible = False : DataGridView1.Columns(14).Visible = False
    End Sub
    Private Sub ComboBox1下拉表單資料載入()
        conn.Close()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 廠商 FROM 供應商清單 WHERE (公司傳真3 LIKE '模具廠')"
        cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
        ComboBox1.Items.Clear() : While (dr.Read()) : ComboBox1.Items.Add(dr("廠商")) : End While : conn.Close()
    End Sub
    Private Sub ComboBox2下拉表單資料載入()
        conn.Close()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 結構 FROM 模具開發用結構清單"
        cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
        ComboBox2.Items.Clear() : While (dr.Read()) : ComboBox2.Items.Add(dr("結構")) : End While : conn.Close()
    End Sub
    Private Sub ComboBox3下拉表單資料載入()
        conn.Close()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 材質 FROM 模具開發用材質清單"
        cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
        ComboBox3.Items.Clear() : While (dr.Read()) : ComboBox3.Items.Add(dr("材質")) : End While : conn.Close()
    End Sub
    Private Sub ComboBox4下拉表單資料載入()
        conn.Close()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 楦頭編號 FROM 楦頭規格控制表 ORDER BY 楦頭編號 DESC"
        cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
        ComboBox4.Items.Clear() : While (dr.Read()) : ComboBox4.Items.Add(dr("楦頭編號")) : End While : conn.Close()
    End Sub
    Private Sub ComboBox5下拉表單資料載入()
        conn.Close()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT  CUST FROM MTTOOCUST清單 ORDER BY CUST"
        cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
        ComboBox5.Items.Clear() : While (dr.Read()) : ComboBox5.Items.Add(dr("CUST")) : End While : conn.Close()
    End Sub
    Private Sub ComboBox6下拉表單資料載入()
        conn.Close()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 廠商 FROM 供應商清單 WHERE (公司傳真3 LIKE '大底廠')"
        cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
        ComboBox6.Items.Clear() : While (dr.Read()) : ComboBox6.Items.Add(dr("廠商")) : End While : conn.Close()
    End Sub
    Private Sub ComboBox7下拉表單資料載入()
        conn.Close()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT SEASON FROM 季節清單 ORDER BY SEASON"
        cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
        ComboBox7.Items.Clear() : While (dr.Read()) : ComboBox7.Items.Add(dr("SEASON")) : End While : conn.Close()
    End Sub
    Private Sub 模具開發控制表_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.MdiParent = WINPROFIT_ERP_SYS : Me.WindowState = 2 : Me.AutoScroll = True
        ComboBox1下拉表單資料載入() : ComboBox2下拉表單資料載入() : ComboBox3下拉表單資料載入() : ComboBox4下拉表單資料載入() : ComboBox5下拉表單資料載入() : ComboBox6下拉表單資料載入()
        ComboBox7下拉表單資料載入() : Set_楦頭規格表清單()
        TextBox4.Enabled = False : TextBox7.Visible = False : TextBox8.Visible = False : TextBox9.Visible = False
        ComboBox1.Enabled = False : ComboBox2.Enabled = False : ComboBox3.Enabled = False : ComboBox4.Enabled = False : ComboBox5.Enabled = False : ComboBox6.Enabled = False : ComboBox7.Enabled = False
        TextBox6.Enabled = False : TextBox1.Enabled = False : TextBox2.Enabled = False : TextBox3.Enabled = False : Button3.Enabled = False : Button6.Enabled = False
        Button2.Enabled = True : Button5.Enabled = True
        Target1 = Target & AA(13) & "/"
        WebBrowser1.Url = New Uri(Target1)
        If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
            WebBrowser2.Navigate(FolderBrowserDialog1.SelectedPath)
        End If
    End Sub
    Private Sub 圖片導入()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 圖片 FROM 模具開發控制表 WHERE 模具開發單號 LIKE N'" & TextBox4.Text & "'"
        cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
        PictureBox1.Image = Nothing
        While dr.Read() = True
            Dim unused As Byte() = New Byte(-1) {}
            Dim bytes As Byte() = DirectCast(dr.Item("圖片"), Byte())
            Dim oStream As New MemoryStream(bytes)
            PictureBox1.Image = Bitmap.FromStream(oStream)
        End While
        conn.Close()
        PictureBox1.SizeMode = 4
    End Sub
    Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        If Button2.Enabled = True Or Button5.Enabled = True Then
            If e.RowIndex = -1 Then : Else
                TextBox4.Text = DataGridView1.Rows(e.RowIndex).Cells("模具開發單號").Value : ComboBox1.Text = DataGridView1.Rows(e.RowIndex).Cells("模具廠").Value
                ComboBox2.Text = DataGridView1.Rows(e.RowIndex).Cells("結構").Value : ComboBox3.Text = DataGridView1.Rows(e.RowIndex).Cells("材質").Value
                ComboBox4.Text = DataGridView1.Rows(e.RowIndex).Cells("楦頭編號").Value : TextBox6.Text = DataGridView1.Rows(e.RowIndex).Cells("模具編號").Value
                TextBox1.Text = DataGridView1.Rows(e.RowIndex).Cells("參考原樣鞋編號").Value : ComboBox5.Text = DataGridView1.Rows(e.RowIndex).Cells("品牌").Value
                ComboBox6.Text = DataGridView1.Rows(e.RowIndex).Cells("大底廠").Value : TextBox2.Text = DataGridView1.Rows(e.RowIndex).Cells("尺寸").Value
                ComboBox7.Text = DataGridView1.Rows(e.RowIndex).Cells("季節").Value : TextBox3.Text = DataGridView1.Rows(e.RowIndex).Cells("備註").Value
                TextBox8.Text = DataGridView1.Rows(e.RowIndex).Cells("制表").Value : TextBox9.Text = DataGridView1.Rows(e.RowIndex).Cells("制表日期").Value
                TextBox10.Text = DataGridView1.Rows(e.RowIndex).Cells("CN").Value
                圖片導入()
            End If
        End If
    End Sub
    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
        PA = InputBox("請輸入要查詢的關鍵字") : Set_楦頭規格表清單() : PA = ""
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        ComboBox1.Enabled = True : ComboBox2.Enabled = True : ComboBox3.Enabled = True : ComboBox4.Enabled = True : ComboBox5.Enabled = True : ComboBox6.Enabled = True : ComboBox7.Enabled = True
        TextBox6.Enabled = True : TextBox1.Enabled = True : TextBox2.Enabled = True : TextBox3.Enabled = True : Button3.Enabled = True : Button6.Enabled = False
        Button2.Enabled = False : Button5.Enabled = False
        ComboBox1下拉表單資料載入() : ComboBox2下拉表單資料載入() : ComboBox3下拉表單資料載入() : ComboBox4下拉表單資料載入() : ComboBox5下拉表單資料載入() : ComboBox6下拉表單資料載入()
        ComboBox7下拉表單資料載入()
        TextBox6.Text = "" : TextBox6.Text = "" : TextBox1.Text = "" : TextBox2.Text = "" : TextBox3.Text = "" : PictureBox1.Image = Nothing
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT TOP (1) 模具開發單號 FROM 模具開發控制表 ORDER BY 模具開發單號 DESC"
        cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
        If dr.Read() Then : TextBox4.Text = dr("模具開發單號").ToString : Else : TextBox4.Text = "MD00000000" : End If
        conn.Close()
        Dim NUM1 As Integer
        NUM1 = Double.Parse(Strings.Right(TextBox4.Text, 8)) + 1
        If NUM1 < 10 Then : TextBox4.Text = "MD" & "0000000" & NUM1
        ElseIf NUM1 > 9 And NUM1 < 100 Then : TextBox4.Text = "MD" & "000000" & NUM1
        ElseIf NUM1 > 99 And NUM1 < 1000 Then : TextBox4.Text = "MD" & "00000" & NUM1
        ElseIf NUM1 > 999 And NUM1 < 10000 Then : TextBox4.Text = "MD" & "0000" & NUM1
        ElseIf NUM1 > 9999 And NUM1 < 100000 Then : TextBox4.Text = "MD" & "000" & NUM1
        ElseIf NUM1 > 99999 And NUM1 < 100000 Then : TextBox4.Text = "MD" & "00" & NUM1
        ElseIf NUM1 > 999999 And NUM1 < 1000000 Then : TextBox4.Text = "MD" & "0" & NUM1
        ElseIf NUM1 > 9999999 Then : TextBox4.Text = "MD" & NUM1
        End If
    End Sub
    Private Sub Set_日期格式轉換()
        DTP = Strings.Format(Today(), "yyyy/MM/dd")
    End Sub
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        If ComboBox1.Text = "" Or ComboBox2.Text = "" Or ComboBox3.Text = "" Or ComboBox4.Text = "" Or ComboBox5.Text = "" Or ComboBox6.Text = "" Or ComboBox7.Text = "" Or TextBox4.Text = "" Or
            TextBox6.Text = "" Or TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox5.Text = "" Then
            MsgBox("資料不齊全，除了備註欄位其他位置不可空白，並請注意圖片是否有準備好。")
        Else
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "SELECT 模具開發單號 FROM 模具開發控制表 WHERE 楦頭編號 LIKE N'" & ComboBox4.Text & "' AND 模具編號 LIKE N'" & TextBox6.Text & "'"
            cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
            If dr.Read() Then
                conn.Close() : MsgBox("資料重複，不予許新建")
            Else
                conn.Close()
                Set_日期格式轉換()
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "SELECT TOP (1) 模具開發單號 FROM 模具開發控制表 WHERE 模具開發單號 LIKE N'" & TextBox4.Text & "'"
                cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
                If dr.Read() Then
                    conn.Close()
                    If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                    SQL1 = "SELECT TOP (1) 模具開發單號 FROM 模具開發控制表 ORDER BY 模具開發單號 DESC"
                    cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
                    If dr.Read() Then : TextBox4.Text = dr("模具開發單號").ToString : Else : TextBox4.Text = "MD00000000" : End If
                    conn.Close()
                    Dim NUM1 As Integer
                    NUM1 = Double.Parse(Strings.Right(TextBox4.Text, 8)) + 1
                    If NUM1 < 10 Then : TextBox4.Text = "MD" & "0000000" & NUM1
                    ElseIf NUM1 > 9 And NUM1 < 100 Then : TextBox4.Text = "MD" & "000000" & NUM1
                    ElseIf NUM1 > 99 And NUM1 < 1000 Then : TextBox4.Text = "MD" & "00000" & NUM1
                    ElseIf NUM1 > 999 And NUM1 < 10000 Then : TextBox4.Text = "MD" & "0000" & NUM1
                    ElseIf NUM1 > 9999 And NUM1 < 100000 Then : TextBox4.Text = "MD" & "000" & NUM1
                    ElseIf NUM1 > 99999 And NUM1 < 100000 Then : TextBox4.Text = "MD" & "00" & NUM1
                    ElseIf NUM1 > 999999 And NUM1 < 1000000 Then : TextBox4.Text = "MD" & "0" & NUM1
                    ElseIf NUM1 > 9999999 Then : TextBox4.Text = "MD" & NUM1
                    End If

                    If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                    SQL1 = "INSERT INTO 模具開發控制表 (模具開發單號, 模具廠, 結構, 材質, 楦頭編號, 模具編號, 參考原樣鞋編號, 品牌, 大底廠, 尺寸, 季節, 備註1, 制表, 制表日期, CN, 圖片) 
                            SELECT N'" & TextBox4.Text & "',N'" & ComboBox1.Text & "',N'" & ComboBox2.Text & "',N'" & ComboBox3.Text & "',N'" & ComboBox4.Text & "',N'" & TextBox6.Text & "',
                                   N'" & TextBox1.Text & "',N'" & ComboBox5.Text & "',N'" & ComboBox6.Text & "',N'" & TextBox2.Text & "',N'" & ComboBox7.Text & "',N'" & TextBox3.Text & "',
                                   N'" & gUserName & "',N'" & DTP & "', N'" & TextBox10.Text & "', 
                                   BULKCOLUMN FROM OPENROWSET ( BULK '" & FTP實體位置 & AA(13) & "\" & TextBox5.Text & "', SINGLE_BLOB) AS EMPLOYEEPICTURE"
                    cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
                Else
                    conn.Close()
                    If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                    SQL1 = "INSERT INTO 模具開發控制表 (模具開發單號, 模具廠, 結構, 材質, 楦頭編號, 模具編號, 參考原樣鞋編號, 品牌, 大底廠, 尺寸, 季節, 備註1, 制表, 制表日期, CN, 圖片) 
                            SELECT N'" & TextBox4.Text & "',N'" & ComboBox1.Text & "',N'" & ComboBox2.Text & "',N'" & ComboBox3.Text & "',N'" & ComboBox4.Text & "',N'" & TextBox6.Text & "',
                                   N'" & TextBox1.Text & "',N'" & ComboBox5.Text & "',N'" & ComboBox6.Text & "',N'" & TextBox2.Text & "',N'" & ComboBox7.Text & "',N'" & TextBox3.Text & "',
                                   N'" & gUserName & "', N'" & DTP & "',N'" & TextBox10.Text & "', 
                                   BULKCOLUMN FROM OPENROWSET ( BULK '" & FTP實體位置 & AA(13) & "\" & TextBox5.Text & "', SINGLE_BLOB) AS EMPLOYEEPICTURE"
                    cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
                End If
                conn.Close()

                '---FTP方式刪除檔案----------------------------------------------------------------------------------------------------------------------------------------------------------------------
                Dim frq As FtpWebRequest, frp As FtpWebResponse, fcr As NetworkCredential
                frq = CType(WebRequest.Create(New Uri(Target1 & TextBox5.Text)), FtpWebRequest)
                fcr = New NetworkCredential(FTP帳號, FTP密碼)
                frq.Credentials = fcr
                frq.Method = WebRequestMethods.Ftp.DeleteFile
                frq.UseBinary = True
                frp = CType(frq.GetResponse, FtpWebResponse)
                frp.Close()
                WebBrowser1.Refresh()
                '----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                圖片導入() : Set_楦頭規格表清單() : MsgBox("新增完成")
                ComboBox1.Enabled = False : ComboBox2.Enabled = False : ComboBox3.Enabled = False : ComboBox4.Enabled = False : ComboBox5.Enabled = False : ComboBox6.Enabled = False
                ComboBox7.Enabled = False : TextBox6.Enabled = False : TextBox1.Enabled = False : TextBox2.Enabled = False : TextBox3.Enabled = False : Button3.Enabled = False
                Button6.Enabled = False : Button2.Enabled = True : Button5.Enabled = True : TextBox5.Text = ""
            End If
        End If
    End Sub
    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        ComboBox1.Enabled = True : ComboBox2.Enabled = True : ComboBox3.Enabled = True : ComboBox4.Enabled = True : ComboBox5.Enabled = True : ComboBox6.Enabled = True : ComboBox7.Enabled = True
        TextBox6.Enabled = True : TextBox1.Enabled = True : TextBox2.Enabled = True : TextBox3.Enabled = True : Button3.Enabled = False : Button6.Enabled = True
        Button2.Enabled = False : Button5.Enabled = False
    End Sub
    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        Set_日期格式轉換()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "UPDATE 模具開發控制表 SET 模具廠 = N'" & ComboBox1.Text & "', 結構 = N'" & ComboBox2.Text & "', 材質 = N'" & ComboBox3.Text & "', 楦頭編號 = N'" & ComboBox4.Text &
               "', 模具編號 = N'" & TextBox6.Text & "', 參考原樣鞋編號 = N'" & TextBox1.Text & "', 品牌 = N'" & ComboBox5.Text & "', 大底廠 = N'" & ComboBox6.Text & "', 尺寸 = N'" & TextBox2.Text &
               "', 季節 = N'" & ComboBox7.Text & "', 備註1 = N'" & TextBox3.Text & "', 制表 = N'" & gUserName & "', 制表日期 = N'" & DTP & "', CN = N'" & TextBox10.Text & "' 
               WHERE 模具開發單號 LIKE N'" & TextBox4.Text & "'"
        cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
        Set_楦頭規格表清單()
        MsgBox("修改完成")
        ComboBox1.Enabled = False : ComboBox2.Enabled = False : ComboBox3.Enabled = False : ComboBox4.Enabled = False : ComboBox5.Enabled = False : ComboBox6.Enabled = False : ComboBox7.Enabled = False
        TextBox6.Enabled = False : TextBox1.Enabled = False : TextBox2.Enabled = False : TextBox3.Enabled = False : Button3.Enabled = False : Button6.Enabled = False
        Button2.Enabled = True : Button5.Enabled = True
    End Sub
    Private Sub TextBox5_DragEnter(sender As Object, e As DragEventArgs) Handles TextBox5.DragEnter
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            Dim files As String()
            Try
                files = CType(e.Data.GetData(DataFormats.FileDrop), String())
                TextBox5.Text = files(files.Length - 1)
            Catch ex As Exception
                MessageBox.Show(ex.Message)
                Return
            End Try
        End If

        Dim STR2 As Integer = 0
        TextBox7.Text = TextBox5.Text
        If TextBox5.Text = "" Then
        Else
            For i As Integer = 0 To 9999
                Dim STR1 As Integer = Strings.Len(TextBox7.Text)
                If Strings.Right((TextBox7.Text), 1) <> "\" Then
                    STR1 -= 1 : STR2 += 1
                    If STR1 = 0 Then
                        TextBox7.Text = ""
                        TextBox5.Text = ""
                        i = 9999
                    Else
                        TextBox7.Text = Strings.Left((TextBox7.Text), STR1)
                    End If
                Else
                    TextBox7.Text = Strings.Right((TextBox5.Text), STR2)
                    TextBox5.Text = TextBox7.Text
                    i = 9999
                End If
            Next
        End If
    End Sub
    Private Sub TextBox5_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles TextBox5.DragDrop
        Dim files As Array = e.Data.GetData(DataFormats.FileDrop)
        For Each file As String In files
            TextBox5.AppendText(file + Environment.NewLine)
        Next
    End Sub
    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
            WebBrowser2.Navigate(FolderBrowserDialog1.SelectedPath)
        End If
    End Sub
    Private Sub Button24_Click(sender As Object, e As EventArgs) Handles Button24.Click
        If TextBox4.Text = "" Or TextBox5.Text = "" Then
            MsgBox("請確定需要修改圖片的模具資料已選擇與圖片資料已準備完畢。")
        Else
            conn.Close() : Set_日期格式轉換()
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "UPDATE 模具開發控制表 SET 制表 = N'" & gUserName & "', 制表日期 = N'" & DTP & "', 
                           圖片 = BULKCOLUMN FROM OPENROWSET ( BULK '" & FTP實體位置 & AA(13) & "\" & TextBox5.Text & "', SINGLE_BLOB) AS 
                    EMPLOYEEPICTURE WHERE (模具開發單號 = '" & TextBox4.Text & "')"
            cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
            '---FTP方式刪除檔案----------------------------------------------------------------------------------------------------------------------------------------------------------------------
            Dim frq As FtpWebRequest, frp As FtpWebResponse, fcr As NetworkCredential
            frq = CType(WebRequest.Create(New Uri(Target1 & TextBox5.Text)), FtpWebRequest)
            fcr = New NetworkCredential(FTP帳號, FTP密碼)
            frq.Credentials = fcr
            frq.Method = WebRequestMethods.Ftp.DeleteFile
            frq.UseBinary = True
            frp = CType(frq.GetResponse, FtpWebResponse)
            frp.Close()
            WebBrowser1.Refresh()
            '----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
            TextBox5.Text = ""
            圖片導入()
            MsgBox("修改完成")
        End If
    End Sub
    Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
        xlApp = CType(CreateObject("Excel.Application"), Microsoft.Office.Interop.Excel.Application)
        xlBook = xlApp.Workbooks.Add
        xlApp.DisplayAlerts = True
        xlBook.Activate()
        xlSheet = NewMethod(xlBook)
        xlSheet.Activate()
        xlApp.Visible = True
        xlApp.Application.WindowState = xlMaximized

        xlSheet.Cells(1, 1) = "巨益模具開發計畫表" : xlSheet.Cells(2, 1) = "模具廠 : " : xlSheet.Cells(3, 1) = "結構 : " : xlSheet.Cells(4, 1) = "材質 : " : xlSheet.Cells(5, 1) = "楦頭編號 : "
        xlSheet.Cells(6, 1) = "模具編號 : " : xlSheet.Cells(2, 2) = ComboBox1.Text : xlSheet.Cells(3, 2) = ComboBox2.Text : xlSheet.Cells(4, 2) = ComboBox3.Text : xlSheet.Cells(5, 2) = ComboBox4.Text
        xlSheet.Cells(6, 2) = TextBox6.Text : xlSheet.Cells(2, 3) = "參考漾鞋編號 : " : xlSheet.Cells(3, 3) = "品牌 : " : xlSheet.Cells(4, 3) = "大底廠 : " : xlSheet.Cells(5, 3) = "尺寸 : "
        xlSheet.Cells(6, 3) = "季節 : " : xlSheet.Cells(2, 4) = TextBox6.Text : xlSheet.Cells(3, 4) = ComboBox5.Text : xlSheet.Cells(4, 4) = ComboBox6.Text : xlSheet.Cells(5, 4) = TextBox2.Text
        xlSheet.Cells(6, 4) = ComboBox7.Text : xlSheet.Cells(7, 4) = "備註 : " : xlSheet.Cells(8, 4) = TextBox3.Text : xlSheet.Cells(52, 1) = "製表人 : " : xlSheet.Cells(52, 2) = TextBox8.Text
        xlSheet.Cells(52, 3) = "製表人 : " : xlSheet.Cells(52, 4) = TextBox9.Text

        CC(xlApp, xlSheet) : BB(xlApp, xlSheet)
        xlSheet.PageSetup.PrintArea = ""
        xlApp.Cells.Select()
        xlSheet.Range("B1").Select()
        xlApp.Application.WindowState = xlMinimized
        'xlApp.ActiveSheet.Protect(DrawingObjects:=True, Contents:=True, Scenarios:=True, Password:="0911274990")
        'xlApp.ActiveSheet.EnableSelection = 1
        MsgBox("列印完成")
    End Sub
    Private Shared Function NewMethod(xlBook As Microsoft.Office.Interop.Excel.Workbook) As Microsoft.Office.Interop.Excel.Worksheet
        Return CType(xlBook.Worksheets.Add, Microsoft.Office.Interop.Excel.Worksheet)
    End Function
    Private Sub CC(ByVal myExcel As Application, ByVal xlSheet As Worksheet)
        xlSheet.Cells.Select()
        With myExcel.Selection.Font : .Name = "微軟正黑體" : .Size = 12 : .Strikethrough = False : .Superscript = False : .Subscript = False : .OutlineFont = False
            .Shadow = False : .Underline = xlUnderlineStyleNone : .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0 : .ThemeFont = xlThemeFontNone : End With

        xlSheet.Columns("A:A").Select : myExcel.Selection.ColumnWidth = 15 : xlSheet.Columns("B:B").Select : myExcel.Selection.ColumnWidth = 50
        xlSheet.Columns("C:C").Select : myExcel.Selection.ColumnWidth = 15 : xlSheet.Columns("D:D").Select : myExcel.Selection.ColumnWidth = 40
        xlSheet.Rows("2:6").Select : myExcel.Selection.RowHeight = 30 : xlSheet.Rows("1:1").Select : myExcel.Selection.RowHeight = 35

        xlSheet.Range("D8:D51").Select() : myExcel.Selection.Merge
        With myExcel.Selection : .HorizontalAlignment = xlLeft : .VerticalAlignment = xlTop : .WrapText = True : .Orientation = 0 : .AddIndent = False
            .IndentLevel = 0 : .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = True : End With

        xlSheet.Range("A2:D6").Select()
        myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With myExcel.Selection.Borders(xlEdgeLeft) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeTop) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeBottom) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeRight) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlInsideVertical) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlInsideHorizontal) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        xlSheet.Range("A7:C51").Select()
        myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With myExcel.Selection.Borders(xlEdgeLeft) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeTop) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeBottom) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeRight) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        myExcel.Selection.Borders(xlInsideVertical).LineStyle = xlNone : myExcel.Selection.Borders(xlInsideHorizontal).LineStyle = xlNone

        xlSheet.Range("D7:D51").Select()
        myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With myExcel.Selection.Borders(xlEdgeLeft) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeTop) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeBottom) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeRight) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        myExcel.Selection.Borders(xlInsideVertical).LineStyle = xlNone : myExcel.Selection.Borders(xlInsideHorizontal).LineStyle = xlNone

        xlSheet.Rows("8:8").Select : myExcel.Selection.RowHeight = 15.75

        xlSheet.Range("A1:D1").Select() : myExcel.Selection.Merge
        With myExcel.Selection.Font : .Name = "微軟正黑體" : .Size = 26 : .Strikethrough = False : .Superscript = False : .Subscript = False : .OutlineFont = False
            .Shadow = False : .Underline = xlUnderlineStyleNone : .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0 : .ThemeFont = xlThemeFontNone : End With
        With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False
            .IndentLevel = 0 : .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = True : End With

        myExcel.ActiveWindow.DisplayGridlines = False

        myExcel.Application.PrintCommunication = False
        With myExcel.ActiveSheet.PageSetup
            .PrintTitleRows = ""
            .PrintTitleColumns = ""
        End With
        myExcel.Application.PrintCommunication = True
        myExcel.ActiveSheet.PageSetup.PrintArea = ""
        myExcel.Application.PrintCommunication = False
        With myExcel.ActiveSheet.PageSetup
            .LeftHeader = ""
            .CenterHeader = ""
            .RightHeader = ""
            .LeftFooter = ""
            .CenterFooter = ""
            .RightFooter = ""
            .LeftMargin = myExcel.Application.InchesToPoints(0.236220472440945)
            .RightMargin = myExcel.Application.InchesToPoints(0.236220472440945)
            .TopMargin = myExcel.Application.InchesToPoints(0.748031496062992)
            .BottomMargin = myExcel.Application.InchesToPoints(0.748031496062992)
            .HeaderMargin = myExcel.Application.InchesToPoints(0.31496062992126)
            .FooterMargin = myExcel.Application.InchesToPoints(0.31496062992126)
            .PrintHeadings = False
            .PrintGridlines = False
            .PrintQuality = 600
            .CenterHorizontally = True
            .CenterVertically = True
            .Draft = False
            .FirstPageNumber = xlAutomatic
            .BlackAndWhite = False
            .Zoom = False
            .FitToPagesWide = 1
            .FitToPagesTall = 1
            .OddAndEvenPagesHeaderFooter = False
            .DifferentFirstPageHeaderFooter = False
            .ScaleWithDocHeaderFooter = True
            .AlignMarginsHeaderFooter = True
            .EvenPage.LeftHeader.Text = ""
            .EvenPage.CenterHeader.Text = ""
            .EvenPage.RightHeader.Text = ""
            .EvenPage.LeftFooter.Text = ""
            .EvenPage.CenterFooter.Text = ""
            .EvenPage.RightFooter.Text = ""
            .FirstPage.LeftHeader.Text = ""
            .FirstPage.CenterHeader.Text = ""
            .FirstPage.RightHeader.Text = ""
            .FirstPage.LeftFooter.Text = ""
            .FirstPage.CenterFooter.Text = ""
            .FirstPage.RightFooter.Text = ""
        End With
        myExcel.Application.PrintCommunication = False
    End Sub
    Private Sub BB(ByVal myExcel As Application, ByVal xlSheet As Worksheet)
        Clipboard.SetDataObject(PictureBox1.Image)
        xlSheet.Range("A7").Select()
        myExcel.ActiveSheet.Pictures.Paste.Select
        myExcel.Selection.ShapeRange.Width = 485
        myExcel.Selection.ShapeRange.IncrementLeft(3)
        myExcel.Selection.ShapeRange.IncrementTop(3)
    End Sub
    Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
        PA = "" : 供應商管理_製鞋樣品.Show() : 供應商管理_製鞋樣品.BringToFront()
    End Sub
    Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click
        PA = "" : 供應商管理_製鞋樣品.Show() : 供應商管理_製鞋樣品.BringToFront()
    End Sub
    Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click
        PA = "" : 楦頭規格表.Show() : 楦頭規格表.BringToFront()
    End Sub
    Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
        清單修改.Close() : 季節清單修改 = True : 清單修改.ShowDialog() : ComboBox7下拉表單資料載入() : 季節清單修改 = False
    End Sub
    Private Sub Button18_Click(sender As Object, e As EventArgs) Handles Button18.Click
        清單修改.Close() : 模具用結構 = True : 清單修改.ShowDialog() : ComboBox2下拉表單資料載入() : 模具用結構 = False
    End Sub
    Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
        清單修改.Close() : 模具用材質 = True : 清單修改.ShowDialog() : ComboBox3下拉表單資料載入() : 模具用材質 = False
    End Sub
End Class