Option Strict Off
Imports Microsoft.Office.Interop.Excel.XlUnderlineStyle
Imports Microsoft.Office.Interop.Excel.Constants
Imports Microsoft.Office.Interop.Excel.XlThemeFont
Imports Microsoft.Office.Interop.Excel.XlThemeColor
Imports Microsoft.Office.Interop.Excel.XlWindowState
Imports Microsoft.Office.Interop.Excel
Imports System.Drawing.Printing
Public Class 公函
    Dim xlApp As Microsoft.Office.Interop.Excel.Application
    Dim xlBook As Microsoft.Office.Interop.Excel.Workbook
    Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet
    Dim 位置1 As String
    Dim N2 As Integer : Dim N3 As Integer
    ReadOnly ds As New DataSet : ReadOnly ds1 As New DataSet
    ReadOnly QRCODE As DotNetBarcode = New DotNetBarcode
    Private Sub 公函_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.MdiParent = YS_ERP_SYS : Me.WindowState = 2 : Me.AutoScroll = True
        SQL_讀取公司資料()
        If dr.Read() Then
            公司名稱.Text = dr("公司名稱")
            地址.Text = dr("地址")
            電話.Text = dr("電話")
            傳真.Text = dr("傳真")
            簡稱.Text = dr("簡稱")
        End If
        conn.Close()
        已儲存的公函_rb.Checked = True
        已作廢的公函_rb.Checked = False
        Set_清單1()
        刪除_bt.Enabled = False : 儲存_bt.Enabled = False : 作廢_bt.Enabled = False : 列印_bt.Enabled = False
        If 公函狀態 = "公函審核" Then
            公函審核_pl.Visible = True : 公函核准_pl.Visible = False : 公函申請_pl.Visible = False
            已儲存的公函_rb.Enabled = False : 已作廢的公函_rb.Enabled = False : 已過審_rb.Enabled = False : 選擇日期_dtp.Enabled = False
            手機_tb.Enabled = False : 標題_tb.Enabled = False : 複製_bt.Enabled = False : 受文者_tb.Enabled = False : 速別_tb.Enabled = False : 密等_tb.Enabled = False
            附件_tb.Enabled = False : 主旨_tb.Enabled = False : 正本_tb.Enabled = False : 副本_tb.Enabled = False : 說明_rtb.Enabled = False
        Else
            公函審核_pl.Visible = False : 公函核准_pl.Visible = False : 公函申請_pl.Visible = True
            已儲存的公函_rb.Enabled = True : 已作廢的公函_rb.Enabled = True : 已過審_rb.Enabled = True : 選擇日期_dtp.Enabled = True
            手機_tb.Enabled = True : 標題_tb.Enabled = True : 複製_bt.Enabled = True : 受文者_tb.Enabled = True : 速別_tb.Enabled = True : 密等_tb.Enabled = True
            附件_tb.Enabled = True : 主旨_tb.Enabled = True : 正本_tb.Enabled = True : 副本_tb.Enabled = True : 說明_rtb.Enabled = True
        End If
    End Sub
    Private Sub 介面關閉_Closed(sender As Object, e As EventArgs) Handles MyBase.Closed
        MyModule1.虛擬桌面開啟()
    End Sub
    Private Sub Set_清單1()
        控制表_dgv.DataSource = Nothing : ds1.Clear()
        控制表_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        控制表_dgv.ColumnHeadersHeight = 25
        控制表_dgv.AllowUserToAddRows = False
        控制表_dgv.RowTemplate.Height = 30
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        If 已作廢的公函_rb.Checked = True And 已儲存的公函_rb.Checked = False And 已過審_rb.Checked = False And 審核中的公函_rb.Checked = False Then
            SQL_公函_控制表清單("1", "0", "0")
        ElseIf 已作廢的公函_rb.Checked = False And 已儲存的公函_rb.Checked = True And 已過審_rb.Checked = False And 審核中的公函_rb.Checked = False Then
            SQL_公函_控制表清單("0", "0", "0")
        ElseIf 已作廢的公函_rb.Checked = False And 已儲存的公函_rb.Checked = False And 已過審_rb.Checked = True And 審核中的公函_rb.Checked = False Then
            SQL_公函_控制表清單("0", "1", "1")
        ElseIf 已作廢的公函_rb.Checked = False And 已儲存的公函_rb.Checked = False And 已過審_rb.Checked = False And 審核中的公函_rb.Checked = True Then
            SQL_公函_控制表清單("0", "1", "0")
        End If
        da.Fill(ds1) : 控制表_dgv.DataSource = ds1.Tables(0) : conn.Close()
        控制表_dgv.Columns(0).FillWeight = 20 : 控制表_dgv.Columns(1).Visible = False : 控制表_dgv.Columns(2).Visible = False
        控制表_dgv.Columns(3).Visible = False : 控制表_dgv.Columns(4).Visible = False : 控制表_dgv.Columns(5).Visible = False
        控制表_dgv.Columns(6).Visible = False : 控制表_dgv.Columns(8).Visible = False : 控制表_dgv.Columns(7).FillWeight = 20
        控制表_dgv.Columns(9).Visible = False : 控制表_dgv.Columns(10).Visible = False : 控制表_dgv.Columns(11).Visible = False
        控制表_dgv.Columns(12).Visible = False : 控制表_dgv.Columns(13).Visible = False : 控制表_dgv.Columns(14).Visible = False
    End Sub
    Private Sub 縮放_tb_Click(sender As Object, e As EventArgs) Handles 縮放_tb.Click
        If 控制表_dgv.Size.Width = 200 Then
            控制表_dgv.Size = New Size(620, 控制表_dgv.Size.Height)
            控制表_dgv.Columns(7).FillWeight = 120
            控制表_dgv.Columns(0).FillWeight = 20
        Else
            控制表_dgv.Size = New Size(200, 控制表_dgv.Size.Height)
            控制表_dgv.Columns(7).FillWeight = 20
            控制表_dgv.Columns(0).FillWeight = 20
        End If
    End Sub
    Private Sub 控制表_dgv_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles 控制表_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            單號_tb.Text = 控制表_dgv(0, e.RowIndex).Value.ToString
            Dim dateString As String = 控制表_dgv(1, e.RowIndex).Value.ToString
            Dim dateParts() As String = dateString.Split("/"c)
            Dim year As Integer = Integer.Parse(dateParts(0)) + 1911 ' 將民國年轉換為西元年
            Dim month As Integer = Integer.Parse(dateParts(1))
            Dim day As Integer = Integer.Parse(dateParts(2))
            Dim dateTime As New DateTime(year, month, day)
            選擇日期_dtp.Value = dateTime
            選擇日期_dtp.CustomFormat = "yyyy/MM/dd"
            選擇日期_dtp.Format = DateTimePickerFormat.Custom
            承辦人_tb.Text = 控制表_dgv(2, e.RowIndex).Value.ToString
            受文者_tb.Text = 控制表_dgv(3, e.RowIndex).Value.ToString
            速別_tb.Text = 控制表_dgv(4, e.RowIndex).Value.ToString
            密等_tb.Text = 控制表_dgv(5, e.RowIndex).Value.ToString
            附件_tb.Text = 控制表_dgv(6, e.RowIndex).Value.ToString
            主旨_tb.Text = 控制表_dgv(7, e.RowIndex).Value.ToString
            說明_tb.Text = 控制表_dgv(8, e.RowIndex).Value.ToString
            說明_rtb.Text = 控制表_dgv(8, e.RowIndex).Value.ToString
            正本_tb.Text = 控制表_dgv(9, e.RowIndex).Value.ToString
            副本_tb.Text = 控制表_dgv(10, e.RowIndex).Value.ToString
            手機_tb.Text = 控制表_dgv(13, e.RowIndex).Value.ToString
            標題_tb.Text = 控制表_dgv(14, e.RowIndex).Value.ToString
            If 登入人級別 <> "00" Then
                If 控制表_dgv(12, e.RowIndex).Value = True Then
                    刪除_bt.Enabled = False : 儲存_bt.Enabled = False : 作廢_bt.Enabled = False : 列印_bt.Enabled = True : 新增_bt.Enabled = True
                ElseIf 控制表_dgv(11, e.RowIndex).Value = True And 控制表_dgv(12, e.RowIndex).Value = False Then
                    刪除_bt.Enabled = False : 儲存_bt.Enabled = False : 作廢_bt.Enabled = True : 列印_bt.Enabled = True : 新增_bt.Enabled = True
                ElseIf 控制表_dgv(11, e.RowIndex).Value = False And 控制表_dgv(12, e.RowIndex).Value = False Then
                    刪除_bt.Enabled = True : 儲存_bt.Enabled = True : 作廢_bt.Enabled = True : 列印_bt.Enabled = True : 新增_bt.Enabled = True
                End If
            Else
                刪除_bt.Enabled = True : 儲存_bt.Enabled = True : 作廢_bt.Enabled = True : 列印_bt.Enabled = True : 新增_bt.Enabled = True
            End If
        End If
    End Sub
    Private Sub 複製_bt_Click(sender As Object, e As EventArgs) Handles 複製_bt.Click
        刪除_bt.Enabled = True : 儲存_bt.Enabled = True : 作廢_bt.Enabled = False : 列印_bt.Enabled = False : 新增_bt.Enabled = False
        受文者_tb.Text = "" : 正本_tb.Text = ""
        Dim NUM1 As Integer
        Dim selectedDate As DateTime = 選擇日期_dtp.Value
        Dim taiwanYear As Integer = selectedDate.Year - 1911
        Dim str As String = String.Format("{0}{1:00}{2:00}", taiwanYear, selectedDate.Month, selectedDate.Day)
        PA1 = String.Format("{0}/{1:00}/{2:00}", taiwanYear, selectedDate.Month, selectedDate.Day)
        For i As Integer = 1 To 999
            NUM1 = 0 + i
            If NUM1 < 10 Then : 單號_tb.Text = str & "0" & NUM1
            ElseIf NUM1 > 9 Then : 單號_tb.Text = str & NUM1
            End If
            conn.Close()
            PA = 單號_tb.Text
            SQL_公函_查詢第一筆單號()
            If dr.Read() Then : Else : i = 999 : conn.Close() : End If : conn.Close()
        Next
        PA = 單號_tb.Text
        SQL_公函_新增一筆公函控制表() : conn.Close()
    End Sub
    Private Sub 新增_bt_Click(sender As Object, e As EventArgs) Handles 新增_bt.Click
        單號_tb.Text = "" : 承辦人_tb.Text = gUserName : 手機_tb.Text = "" : 受文者_tb.Text = ""
        速別_tb.Text = "" : 密等_tb.Text = "" : 主旨_tb.Text = "" : 說明_tb.Text = "" : 說明_rtb.Text = ""
        正本_tb.Text = "" : 副本_tb.Text = "" : 附件_tb.Text = "" : 標題_tb.Text = 公司名稱.Text
        Dim NUM1 As Integer
        Dim selectedDate As DateTime = 選擇日期_dtp.Value
        Dim taiwanYear As Integer = selectedDate.Year - 1911
        Dim str As String = String.Format("{0}{1:00}{2:00}", taiwanYear, selectedDate.Month, selectedDate.Day)
        PA1 = String.Format("{0}/{1:00}/{2:00}", taiwanYear, selectedDate.Month, selectedDate.Day)
        For i As Integer = 1 To 999
            NUM1 = 0 + i
            If NUM1 < 10 Then : 單號_tb.Text = str & "0" & NUM1
            ElseIf NUM1 > 9 Then : 單號_tb.Text = str & NUM1
            End If
            conn.Close()
            PA = 單號_tb.Text
            SQL_公函_查詢第一筆單號()
            If dr.Read() Then : Else : i = 999 : conn.Close() : End If : conn.Close()
        Next
        PA = 單號_tb.Text
        SQL_公函_新增一筆公函控制表() : conn.Close()
        刪除_bt.Enabled = True : 儲存_bt.Enabled = True : 作廢_bt.Enabled = False : 列印_bt.Enabled = False : 新增_bt.Enabled = False
    End Sub
    Private Sub 刪除_bt_Click(sender As Object, e As EventArgs) Handles 刪除_bt.Click
        SQL_公函_控制表刪除() : conn.Close() : MsgBox("刪除成功!!")
        單號_tb.Text = "" : 承辦人_tb.Text = "" : 手機_tb.Text = "" : 受文者_tb.Text = ""
        速別_tb.Text = "" : 密等_tb.Text = "" : 主旨_tb.Text = "" : 說明_tb.Text = "" : 說明_rtb.Text = ""
        正本_tb.Text = "" : 副本_tb.Text = "" : 附件_tb.Text = "" : Set_清單1()
        If 登入人級別 <> "00" Then
            刪除_bt.Enabled = False : 儲存_bt.Enabled = False : 作廢_bt.Enabled = False : 列印_bt.Enabled = False : 新增_bt.Enabled = True
        End If
    End Sub
    Private Sub 儲存_bt_Click(sender As Object, e As EventArgs) Handles 儲存_bt.Click
        If 承辦人_tb.Text = gUserName Or 登入人級別 = "00" Then
            Dim selectedDate As DateTime = 選擇日期_dtp.Value
            Dim taiwanYear As Integer = selectedDate.Year - 1911
            PA1 = String.Format("{0}/{1:00}/{2:00}", taiwanYear, selectedDate.Month, selectedDate.Day)
            SQL_公函_控制表修改() : conn.Close() : MsgBox("儲存成功!!") : Set_清單1()
            If 登入人級別 <> "00" Then
                刪除_bt.Enabled = True : 儲存_bt.Enabled = True : 作廢_bt.Enabled = True : 列印_bt.Enabled = True : 新增_bt.Enabled = True
            End If
        Else : MsgBox("請確認開單人是否為本人") : End If
    End Sub
    Private Sub 作廢_bt_Click(sender As Object, e As EventArgs) Handles 作廢_bt.Click
        SQL_公函_控制表作廢() : conn.Close() : MsgBox("作廢成功!!") : Set_清單1()
        If 登入人級別 <> "00" Then
            刪除_bt.Enabled = False : 儲存_bt.Enabled = False : 作廢_bt.Enabled = False : 列印_bt.Enabled = True : 新增_bt.Enabled = True
        End If
    End Sub
    Private Sub 作廢的營運成本收支單_rb_CheckedChanged(sender As Object, e As EventArgs) Handles 已作廢的公函_rb.Click
        已作廢的公函_rb.Checked = True : 已儲存的公函_rb.Checked = False : 已過審_rb.Checked = False : 審核中的公函_rb.Checked = False
        公函申請_pl.Visible = True : 公函核准_pl.Visible = False : 公函審核_pl.Visible = False
        單號_tb.Text = "" : 承辦人_tb.Text = gUserName : 手機_tb.Text = "" : 受文者_tb.Text = "" : 速別_tb.Text = "" : 密等_tb.Text = "" : 主旨_tb.Text = ""
        說明_tb.Text = "" : 說明_rtb.Text = "" : 正本_tb.Text = "" : 副本_tb.Text = "" : 附件_tb.Text = "" : 標題_tb.Text = "" : 承辦人_tb.Text = ""
        Set_清單1()
    End Sub
    Private Sub 已核准的營運成本收支單_rb_CheckedChanged(sender As Object, e As EventArgs) Handles 已儲存的公函_rb.Click
        已作廢的公函_rb.Checked = False : 已儲存的公函_rb.Checked = True : 已過審_rb.Checked = False : 審核中的公函_rb.Checked = False
        公函申請_pl.Visible = True : 公函核准_pl.Visible = False : 公函審核_pl.Visible = False
        單號_tb.Text = "" : 承辦人_tb.Text = gUserName : 手機_tb.Text = "" : 受文者_tb.Text = "" : 速別_tb.Text = "" : 密等_tb.Text = "" : 主旨_tb.Text = ""
        說明_tb.Text = "" : 說明_rtb.Text = "" : 正本_tb.Text = "" : 副本_tb.Text = "" : 附件_tb.Text = "" : 標題_tb.Text = "" : 承辦人_tb.Text = ""
        Set_清單1()
    End Sub
    Private Sub 已過審_rb_Click(sender As Object, e As EventArgs) Handles 已過審_rb.Click
        已作廢的公函_rb.Checked = False : 已儲存的公函_rb.Checked = False : 已過審_rb.Checked = True : 審核中的公函_rb.Checked = False
        公函申請_pl.Visible = False : 公函核准_pl.Visible = True : 公函審核_pl.Visible = False
        單號_tb.Text = "" : 承辦人_tb.Text = gUserName : 手機_tb.Text = "" : 受文者_tb.Text = "" : 速別_tb.Text = "" : 密等_tb.Text = "" : 主旨_tb.Text = ""
        說明_tb.Text = "" : 說明_rtb.Text = "" : 正本_tb.Text = "" : 副本_tb.Text = "" : 附件_tb.Text = "" : 標題_tb.Text = "" : 承辦人_tb.Text = ""
        Set_清單1()
    End Sub
    Private Sub 審核中的公函_rb_Click(sender As Object, e As EventArgs) Handles 審核中的公函_rb.Click
        已作廢的公函_rb.Checked = False : 已儲存的公函_rb.Checked = False : 已過審_rb.Checked = False : 審核中的公函_rb.Checked = True
        公函申請_pl.Visible = False : 公函核准_pl.Visible = False : 公函審核_pl.Visible = False
        單號_tb.Text = "" : 承辦人_tb.Text = gUserName : 手機_tb.Text = "" : 受文者_tb.Text = "" : 速別_tb.Text = "" : 密等_tb.Text = "" : 主旨_tb.Text = ""
        說明_tb.Text = "" : 說明_rtb.Text = "" : 正本_tb.Text = "" : 副本_tb.Text = "" : 附件_tb.Text = "" : 標題_tb.Text = "" : 承辦人_tb.Text = ""
        Set_清單1()
    End Sub
    Private Sub 字體_NUD_ValueChanged(sender As Object, e As EventArgs) Handles 字體_NUD.ValueChanged
        受文者_tb.Font = New System.Drawing.Font("微軟正黑體", 字體_NUD.Value)
        主旨_tb.Font = New System.Drawing.Font("微軟正黑體", 字體_NUD.Value)
        說明_tb.Font = New System.Drawing.Font("微軟正黑體", 字體_NUD.Value)
        說明_rtb.Font = New System.Drawing.Font("微軟正黑體", 字體_NUD.Value)
        正本_tb.Font = New System.Drawing.Font("微軟正黑體", 字體_NUD.Value)
        副本_tb.Font = New System.Drawing.Font("微軟正黑體", 字體_NUD.Value)
        附件_tb.Font = New System.Drawing.Font("微軟正黑體", 字體_NUD.Value)
    End Sub
    Private Sub 查詢_tb_Click(sender As Object, e As EventArgs) Handles 查詢_tb.Click
        PA56 = InputBox("請輸入要查詢的單號資料。") : Set_清單1() : PA56 = ""
    End Sub
    Private Sub 複製_bt_MouseEnter(sender As Object, e As EventArgs) Handles 複製_bt.MouseEnter
        ToolTip1.SetToolTip(Me.複製_bt, "複製文件")
    End Sub
    Private Sub 確縮放_tb_MouseEnter(sender As Object, e As EventArgs) Handles 縮放_tb.MouseEnter
        ToolTip1.SetToolTip(Me.縮放_tb, "主旨縮放")
    End Sub
    Private Sub 查詢_tb_MouseEnter(sender As Object, e As EventArgs) Handles 查詢_tb.MouseEnter
        ToolTip1.SetToolTip(Me.查詢_tb, "關鍵字搜尋")
    End Sub
    Private Sub 新增_bt_MouseEnter(sender As Object, e As EventArgs) Handles 新增_bt.MouseEnter
        ToolTip1.SetToolTip(Me.新增_bt, "新增")
    End Sub
    Private Sub 確認新增_bt_MouseEnter(sender As Object, e As EventArgs) Handles 儲存_bt.MouseEnter
        ToolTip1.SetToolTip(Me.儲存_bt, "儲存")
    End Sub
    Private Sub 修改_bt_MouseEnter(sender As Object, e As EventArgs) Handles 作廢_bt.MouseEnter
        ToolTip1.SetToolTip(Me.作廢_bt, "作廢")
    End Sub
    Private Sub 刪除_bt_MouseEnter(sender As Object, e As EventArgs) Handles 刪除_bt.MouseEnter
        ToolTip1.SetToolTip(Me.刪除_bt, "刪除")
    End Sub
    Private Sub 匯出EXCEL_bt_MouseEnter(sender As Object, e As EventArgs) Handles 列印_bt.MouseEnter
        ToolTip1.SetToolTip(Me.列印_bt, "匯出PDF")
    End Sub
    Private Sub 送審確認_bt_MouseEnter(sender As Object, e As EventArgs) Handles 送審確認_bt.MouseEnter
        ToolTip1.SetToolTip(Me.送審確認_bt, "公函送審")
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        QRCode2_pb.Image = Nothing
        QRCode2_pb.Image = New Bitmap(QRCode2_pb.Width, QRCode2_pb.Height)
        Dim GP2 As Graphics = Graphics.FromImage(QRCode2_pb.Image)
        QRCODE.Type = DotNetBarcode.Types.QRCode : QRCODE.QRQuitZone = 0 : QRCODE.QRWriteBar("http://106.1.48.106:8080/ICS-SCADA-SYS/index2.html", 1, 1, 2.5, GP2)
        QRCode2_pb.SizeMode = 4
        Dim printDocument As New PrintDocument()
        AddHandler printDocument.PrintPage, AddressOf PrintPageHandler
        Dim printDialog As New PrintDialog()
        printDialog.Document = printDocument

        If printDialog.ShowDialog() = DialogResult.OK Then
            printDocument.Print()
        End If
    End Sub
    Private Sub PrintPageHandler(sender As Object, e As PrintPageEventArgs)
        ' 在這個事件處理程序中，你可以使用 e.Graphics 繪製要列印的內容
        Dim graphics As Graphics = e.Graphics
        Dim newFont As New System.Drawing.Font("標楷體", 18)
        Dim brush As New SolidBrush(Color.Black)
        Dim printText3 As String = "王詩文"
        Dim printText4 As String = "王詩文"
        Dim printText5 As String = "112"
        Dim printText6 As String = "10"
        Dim printText7 As String = "08"
        Dim newFont1 As New System.Drawing.Font("標楷體", 14)
        Dim printText As String = "明細如下"
        Dim printText1 As String = "玖仟玖佰元整"
        Dim printText2 As String = "王詩文(闔家人等)"
        Dim printText31 As String = "光明燈*3"
        Dim printText41 As String = "太歲燈*3"
        Dim printText51 As String = "智慧燈*3"
        Dim printText61 As String = "財利燈*3"
        Dim printText71 As String = "姻緣燈*3"
        Dim printText81 As String = "延壽燈*3"
        Dim printText91 As String = "解厄燈*3"
        graphics.DrawString(printText, newFont, brush, 370, 75)
        graphics.DrawString(printText2, newFont, brush, 0, 230)
        graphics.DrawString(printText31, newFont1, brush, 0, 275)
        graphics.DrawString(printText41, newFont1, brush, 0, 300)
        graphics.DrawString(printText51, newFont1, brush, 0, 325)
        graphics.DrawString(printText61, newFont1, brush, 0, 350)
        graphics.DrawString(printText71, newFont1, brush, 0, 375)
        graphics.DrawString(printText81, newFont1, brush, 0, 400)
        graphics.DrawString(printText91, newFont1, brush, 0, 425)
        graphics.DrawString(printText1, newFont, brush, 70, 125)
        graphics.DrawString(printText3, newFont, brush, 380, 380)
        graphics.DrawString(printText4, newFont, brush, 350, 430)
        graphics.DrawString(printText5, newFont, brush, 140, 485)
        graphics.DrawString(printText6, newFont, brush, 300, 485)
        graphics.DrawString(printText7, newFont, brush, 440, 485)
        Dim qrCodeImage As Image = QRCode2_pb.Image
        graphics.DrawImage(qrCodeImage, 520, 5)
        e.HasMorePages = False ' 如果有多頁要列印，設置為 True
    End Sub

    Private Sub 列印_bt_Click(sender As Object, e As EventArgs) Handles 列印_bt.Click
        If 單號_tb.Text = "" Then
            MsgBox("請先選擇要列印的公函")
        Else
            SQL_公函_控制表鎖定() : conn.Close()
            If 登入人級別 <> "00" Then
                刪除_bt.Enabled = False : 儲存_bt.Enabled = False : 作廢_bt.Enabled = True : 列印_bt.Enabled = True : 新增_bt.Enabled = True
            End If
            Set_清單1()
            xlApp = CType(CreateObject("Excel.Application"), Microsoft.Office.Interop.Excel.Application)
            xlApp.Visible = True
            xlApp.DisplayAlerts = True
            xlApp.Application.WindowState = xlMaximized
            xlBook = xlApp.Workbooks.Add
            xlSheet = NewMethod(xlBook)
            xlBook.Activate()
            xlSheet.Activate()
            xlSheet.Cells(1, 1) = 標題_tb.Text & "   函"
            xlSheet.Cells(2, 7) = "機關地址：" : xlSheet.Cells(2, 8) = 地址.Text
            xlSheet.Cells(3, 7) = "電    話：" : xlSheet.Cells(3, 8) = 電話.Text : xlSheet.Cells(3, 9) = "傳    真：" : xlSheet.Cells(3, 10) = 傳真.Text
            xlSheet.Cells(4, 7) = "承 辦 人：" : xlSheet.Cells(4, 8) = 承辦人_tb.Text
            If 手機_tb.Text <> "" Then
                xlSheet.Cells(4, 9) = "手    機：" : xlSheet.Cells(4, 10) = "'" & 手機_tb.Text
            End If
            xlSheet.Cells(5, 1) = "受文者：" : xlSheet.Cells(5, 5) = 受文者_tb.Text : xlSheet.Cells(5, 11) = 受文者_tb.Text
            Dim selectedDate As DateTime = 選擇日期_dtp.Value
            Dim taiwanYear As Integer = selectedDate.Year - 1911

            xlSheet.Cells(6, 1) = "發文日期：" : xlSheet.Cells(6, 4) = "中華民國" & taiwanYear & "年" & selectedDate.Month & "月" & selectedDate.Day & "日"
            xlSheet.Cells(7, 1) = "發文字號：" : xlSheet.Cells(7, 4) = 簡稱.Text & "字第 " & 單號_tb.Text & " 號"
            xlSheet.Cells(8, 1) = "速別：" : xlSheet.Cells(8, 2) = 速別_tb.Text : xlSheet.Cells(8, 11) = 速別_tb.Text
            xlSheet.Cells(9, 1) = "密等及解密條件：" : xlSheet.Cells(9, 6) = 密等_tb.Text : xlSheet.Cells(9, 11) = 密等_tb.Text
            xlSheet.Cells(10, 1) = "附件：" : xlSheet.Cells(10, 2) = 附件_tb.Text : xlSheet.Cells(10, 11) = 附件_tb.Text
            xlSheet.Cells(11, 1) = "主旨：" : xlSheet.Cells(11, 3) = 主旨_tb.Text : xlSheet.Cells(11, 11) = 主旨_tb.Text
            If 說明_rtb.Text <> "" Then
                Dim lines As New List(Of String)()
                Dim text As String = 說明_rtb.Text
                lines.Clear()
                ' 使用 Graphics.MeasureString 测量文本的宽度
                Using g As Graphics = CreateGraphics()
                    Dim currentLine As String = ""
                    For Each c As Char In text
                        If c = ControlChars.Lf Then
                            ' 如果遇到换行符号，添加一个新的段落
                            lines.Add(currentLine)
                            currentLine = ""
                        Else
                            currentLine += c
                            ' 计算当前行的宽度
                            Dim width As Integer = TextRenderer.MeasureText(currentLine, 說明_rtb.Font).Width
                            ' 当宽度接近 RichTextBox 的宽度时，添加一个分段符号
                            If width >= 450 Then
                                lines.Add(currentLine)
                                currentLine = ""
                            End If
                        End If
                    Next
                    If currentLine <> "" Then
                        lines.Add(currentLine)
                    End If
                End Using
                For i As Integer = 0 To lines.Count - 1
                    字串處理(lines(i))
                    lines(i) = 已處理字串
                Next
                xlSheet.Cells(12, 1) = "說明："
                For i = 0 To lines.Count - 1
                    xlSheet.Cells(12 + i, 3) = lines(i) : xlSheet.Cells(12 + i, 11) = lines(i)
                    N2 = i + 12
                Next
                xlSheet.Cells(N2 + 1, 1) = "正本：" : xlSheet.Cells(N2 + 1, 2) = 正本_tb.Text : xlSheet.Cells(N2 + 1, 11) = 正本_tb.Text
                xlSheet.Cells(N2 + 2, 1) = "副本：" : xlSheet.Cells(N2 + 2, 2) = 副本_tb.Text : xlSheet.Cells(N2 + 2, 11) = 副本_tb.Text

            Else
                xlSheet.Cells(12, 1) = "正本：" : xlSheet.Cells(12, 2) = 正本_tb.Text : xlSheet.Cells(12, 11) = 正本_tb.Text
                xlSheet.Cells(13, 1) = "副本：" : xlSheet.Cells(13, 2) = 副本_tb.Text : xlSheet.Cells(13, 11) = 副本_tb.Text
            End If

            BB(xlApp, xlSheet)

            xlApp.Sheets(2).Delete : xlSheet.PageSetup.PrintArea = ""
            '----轉成PDF-----
            Dim SERT As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
            xlBook.Sheets(1).ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, SERT & "\" & 單號_tb.Text & "-公函.pdf", XlFixedFormatQuality.xlQualityStandard, True)
            '--------
            xlApp.Cells.Select() : xlApp.Application.WindowState = xlMinimized : xlBook.Close(False)
            Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet)
            Runtime.InteropServices.Marshal.ReleaseComObject(xlBook)
            xlApp.Quit() : Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
            xlSheet = Nothing : xlBook = Nothing : xlApp = Nothing : GC.Collect() : GC.WaitForPendingFinalizers()
            Try
                Dim pdfFilePath As String = SERT & "\" & 單號_tb.Text & "-公函.pdf"
                Dim acrobatReaderPath As String = "C:\Program Files\Google\Chrome\Application\chrome.exe"
                If System.IO.File.Exists(acrobatReaderPath) Then : Process.Start(acrobatReaderPath, pdfFilePath)
                Else : Dim chromePath As String = "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
                    If System.IO.File.Exists(chromePath) Then : Process.Start(chromePath, pdfFilePath)
                    Else : Dim edgePath As String = "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
                        If System.IO.File.Exists(edgePath) Then : Process.Start(edgePath, pdfFilePath)
                        Else : MsgBox("以存檔至桌面 。" & vbCrLf & "檔案名稱：" & 單號_tb.Text & "-公函.pdf")
                        End If : End If : End If
            Catch ex As Exception
                MsgBox("以存檔至桌面 。" & vbCrLf & "檔案名稱：" & 單號_tb.Text & "-公函.pdf")
            End Try
        End If
    End Sub
    Private Sub BB(ByVal myExcel As Microsoft.Office.Interop.Excel.Application, ByVal xlSheet As Microsoft.Office.Interop.Excel.Worksheet)
        xlSheet.Rows("1:1").Select
        With myExcel.Selection.Font
            .Name = "標楷體"
            .Size = 18
            .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .Underline = xlUnderlineStyleNone
            .ThemeColor = xlThemeColorLight1
            .TintAndShade = 0
            .ThemeFont = xlThemeFontNone
        End With
        xlSheet.Range("2:4").Select()

        With myExcel.Selection.Font
            .Name = "標楷體"
            .Size = 10
            .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .Underline = xlUnderlineStyleNone
            .ThemeColor = xlThemeColorLight1
            .TintAndShade = 0
            .ThemeFont = xlThemeFontNone
        End With
        If 說明_rtb.Text <> "" Then
            xlSheet.Range("6:10," & N2 + 1 & ":" & N2 + 2).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.Range("5:5,11:" & N2).Select()

            With myExcel.Selection.Font
                .Name = "標楷體"
                .Size = 16
                .Strikethrough = False
                .Superscript = False
                .Subscript = False
                .OutlineFont = False
                .Shadow = False
                .Underline = xlUnderlineStyleNone
                .ThemeColor = xlThemeColorLight1
                .TintAndShade = 0
                .ThemeFont = xlThemeFontNone
            End With
        Else
            xlSheet.Range("6:10,12:13").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.Range("5:5,11:11").Select()

            With myExcel.Selection.Font
                .Name = "標楷體"
                .Size = 16
                .Strikethrough = False
                .Superscript = False
                .Subscript = False
                .OutlineFont = False
                .Shadow = False
                .Underline = xlUnderlineStyleNone
                .ThemeColor = xlThemeColorLight1
                .TintAndShade = 0
                .ThemeFont = xlThemeFontNone
            End With
        End If

        xlSheet.Columns("A:A").ColumnWidth = 5.5
        xlSheet.Columns("B:B").ColumnWidth = 1.5
        xlSheet.Columns("C:C").ColumnWidth = 2
        xlSheet.Columns("D:D").ColumnWidth = 0.38
        xlSheet.Columns("E:E").ColumnWidth = 5
        xlSheet.Columns("F:F").ColumnWidth = 41.5
        xlSheet.Columns("G:G").ColumnWidth = 8
        xlSheet.Columns("H:H").ColumnWidth = 10
        xlSheet.Columns("I:I").ColumnWidth = 8
        xlSheet.Columns("J:J").ColumnWidth = 10



        xlSheet.Range("A1:J1").Select()
        With myExcel.Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        myExcel.Selection.Merge

        xlSheet.Range("H2:J2").Select()

        With myExcel.Selection
            .HorizontalAlignment = xlLeft
            .VerticalAlignment = xlCenter
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        myExcel.Selection.Merge

        xlSheet.Range("G2,G3:J4").Select()
        With myExcel.Selection
            .HorizontalAlignment = xlLeft
            .VerticalAlignment = xlCenter
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With

        xlSheet.Range("A5:D5").Select()

        With myExcel.Selection
            .HorizontalAlignment = xlLeft
            .VerticalAlignment = xlTop
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        myExcel.Selection.Merge

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

        With myExcel.Selection
            .HorizontalAlignment = xlLeft
            .VerticalAlignment = xlTop
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        myExcel.Selection.Merge

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

        With myExcel.Selection
            .HorizontalAlignment = xlLeft
            .VerticalAlignment = xlTop
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        myExcel.Selection.Merge

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

        xlSheet.Range("A8").Select()

        With myExcel.Selection
            .HorizontalAlignment = xlLeft
            .VerticalAlignment = xlTop
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With

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

        xlSheet.Range("A9:E9").Select()

        With myExcel.Selection
            .HorizontalAlignment = xlLeft
            .VerticalAlignment = xlTop
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        myExcel.Selection.Merge

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

        xlSheet.Range("A10").Select()

        With myExcel.Selection
            .HorizontalAlignment = xlLeft
            .VerticalAlignment = xlTop
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With

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

        xlSheet.Range("A11:B11").Select()

        With myExcel.Selection
            .HorizontalAlignment = xlLeft
            .VerticalAlignment = xlTop
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        myExcel.Selection.Merge

        xlSheet.Range("C11:J11").Select()
        With myExcel.Selection
            .HorizontalAlignment = xlLeft
            .VerticalAlignment = xlTop
            .WrapText = True
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        myExcel.Selection.Merge
        If 說明_rtb.Text <> "" Then
            xlSheet.Range("A12:B12").Select()

            With myExcel.Selection
                .HorizontalAlignment = xlLeft
                .VerticalAlignment = xlTop
                .WrapText = False
                .Orientation = 0
                .AddIndent = False
                .IndentLevel = 0
                .ShrinkToFit = False
                .ReadingOrder = xlContext
                .MergeCells = False
            End With
            myExcel.Selection.Merge

            For i = 12 To N2
                xlSheet.Range("C" & i & ":J" & i).Select()
                With myExcel.Selection
                    .HorizontalAlignment = xlLeft
                    .VerticalAlignment = xlTop
                    .WrapText = True
                    .Orientation = 0
                    .AddIndent = False
                    .IndentLevel = 0
                    .ShrinkToFit = False
                    .ReadingOrder = xlContext
                    .MergeCells = False
                End With
                myExcel.Selection.Merge
            Next


            xlSheet.Range("A" & N2 + 1).Select()

            With myExcel.Selection
                .HorizontalAlignment = xlLeft
                .VerticalAlignment = xlTop
                .WrapText = False
                .Orientation = 0
                .AddIndent = False
                .IndentLevel = 0
                .ShrinkToFit = False
                .ReadingOrder = xlContext
                .MergeCells = False
            End With
            myExcel.Selection.Merge

            xlSheet.Range("B" & N2 + 1 & ":J" & N2 + 1).Select()
            With myExcel.Selection
                .HorizontalAlignment = xlLeft
                .VerticalAlignment = xlTop
                .WrapText = True
                .Orientation = 0
                .AddIndent = False
                .IndentLevel = 0
                .ShrinkToFit = False
                .ReadingOrder = xlContext
                .MergeCells = False
            End With
            myExcel.Selection.Merge

            xlSheet.Range("A" & N2 + 2).Select()

            With myExcel.Selection
                .HorizontalAlignment = xlLeft
                .VerticalAlignment = xlTop
                .WrapText = False
                .Orientation = 0
                .AddIndent = False
                .IndentLevel = 0
                .ShrinkToFit = False
                .ReadingOrder = xlContext
                .MergeCells = False
            End With
            myExcel.Selection.Merge

            xlSheet.Range("B" & N2 + 2 & ":J" & N2 + 2).Select()
            With myExcel.Selection
                .HorizontalAlignment = xlLeft
                .VerticalAlignment = xlTop
                .WrapText = True
                .Orientation = 0
                .AddIndent = False
                .IndentLevel = 0
                .ShrinkToFit = False
                .ReadingOrder = xlContext
                .MergeCells = False
            End With
            myExcel.Selection.Merge
        Else
            xlSheet.Range("A12").Select()

            With myExcel.Selection
                .HorizontalAlignment = xlLeft
                .VerticalAlignment = xlTop
                .WrapText = False
                .Orientation = 0
                .AddIndent = False
                .IndentLevel = 0
                .ShrinkToFit = False
                .ReadingOrder = xlContext
                .MergeCells = False
            End With
            myExcel.Selection.Merge

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

            xlSheet.Range("A13").Select()

            With myExcel.Selection
                .HorizontalAlignment = xlLeft
                .VerticalAlignment = xlTop
                .WrapText = False
                .Orientation = 0
                .AddIndent = False
                .IndentLevel = 0
                .ShrinkToFit = False
                .ReadingOrder = xlContext
                .MergeCells = False
            End With
            myExcel.Selection.Merge

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

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

        xlSheet.Columns("K:K").ColumnWidth = 75.63
        xlSheet.Rows("5:5").Select
        xlSheet.Rows("5:5").EntireRow.AutoFit


        xlSheet.Columns("K:K").ColumnWidth = 70
        xlSheet.Rows("9:9").Select
        xlSheet.Rows("9:9").EntireRow.AutoFit


        xlSheet.Columns("K:K").ColumnWidth = 81
        xlSheet.Rows("8:8").Select
        xlSheet.Rows("8:8").EntireRow.AutoFit
        xlSheet.Rows("10:10").Select
        xlSheet.Rows("10:10").EntireRow.AutoFit

        If 說明_rtb.Text <> "" Then
            xlSheet.Rows(N2 + 1 & ":" & N2 + 1).Select
            xlSheet.Rows(N2 + 1 & ":" & N2 + 1).EntireRow.AutoFit
            xlSheet.Rows(N2 + 2 & ":" & N2 + 2).Select
            xlSheet.Rows(N2 + 2 & ":" & N2 + 2).EntireRow.AutoFit

            'xlSheet.Columns("K:K").ColumnWidth = 73.8
            xlSheet.Columns("K:K").ColumnWidth = 88.8
            For i = 11 To N2
                xlSheet.Rows(i & ":" & i).Select
                xlSheet.Rows(i & ":" & i).EntireRow.AutoFit
            Next

        Else
            xlSheet.Rows("12:12").Select
            xlSheet.Rows("12:12").EntireRow.AutoFit
            xlSheet.Rows("13:13").Select
            xlSheet.Rows("13:13").EntireRow.AutoFit

            xlSheet.Columns("K:K").ColumnWidth = 88.8
            xlSheet.Rows("11:11").Select
            xlSheet.Rows("11:11").EntireRow.AutoFit

        End If
        xlSheet.Rows("6:6").Select
        xlSheet.Rows("6:6").EntireRow.AutoFit
        xlSheet.Rows("7:7").Select
        xlSheet.Rows("7:7").EntireRow.AutoFit
        xlSheet.Columns("K:K").Select
        myExcel.Selection.EntireColumn.Hidden = True

        For i = 1 To N2 + 2
            If xlSheet.Rows(i).RowHeight < 400 Then
                If i = 10 Or i = 11 Or i = N2 Then
                    xlSheet.Rows(i).RowHeight = xlSheet.Rows(i).RowHeight + 8
                Else
                    xlSheet.Rows(i).RowHeight = xlSheet.Rows(i).RowHeight + 5
                End If
            End If
        Next i

        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.25)
            .RightMargin = myExcel.Application.InchesToPoints(0.25)
            .TopMargin = myExcel.Application.InchesToPoints(0.75)
            .BottomMargin = myExcel.Application.InchesToPoints(0.75)
            .HeaderMargin = myExcel.Application.InchesToPoints(0.3)
            .FooterMargin = myExcel.Application.InchesToPoints(0.3)
            .PrintHeadings = False
            .PrintGridlines = False
            .CenterHorizontally = False
            .CenterVertically = False
            .Draft = False
            .FirstPageNumber = xlAutomatic
            .BlackAndWhite = False
            .Zoom = False
            .FitToPagesWide = 1
            .FitToPagesTall = 0
            .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 = True
    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 送審確認_bt_Click(sender As Object, e As EventArgs) Handles 送審確認_bt.Click
        Dim result As DialogResult = MessageBox.Show("請確定是否要將該公函送審?", "請選擇", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If result = DialogResult.Yes Then
            PA11 = InputBox("請輸入大寫 [YES]，並按下確認來要公函送審!!")
            If PA11 = "YES" Then
                SQL_公函_控制表送審(Today().ToString("yyyy/MM/dd"), 單號_tb.Text)
                Set_清單1() : MsgBox("公函已送審!!")
            End If
        End If
    End Sub
    Private Sub 公函退件_bt_Click(sender As Object, e As EventArgs) Handles 公函退件_bt.Click
        Dim result As DialogResult = MessageBox.Show("請確定是否要將該公函退件?", "請選擇", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If result = DialogResult.Yes Then
            SQL_公函_控制表退件(單號_tb.Text)
            Set_清單1() : MsgBox("公函已退件!!")
        End If
    End Sub
    Private Sub 作廢2_bt_Click(sender As Object, e As EventArgs) Handles 作廢2_bt.Click
        Dim result As DialogResult = MessageBox.Show("請確定該公函使否要作廢?", "請選擇", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If result = DialogResult.Yes Then
            SQL_公函_控制表作廢() : conn.Close() : MsgBox("作廢成功!!") : Set_清單1()
            If 登入人級別 <> "00" Then
                刪除_bt.Enabled = False : 儲存_bt.Enabled = False : 作廢_bt.Enabled = False : 列印_bt.Enabled = True : 新增_bt.Enabled = True
            End If
        End If
    End Sub
    Private Sub 審核通過_bt_Click(sender As Object, e As EventArgs) Handles 審核通過_bt.Click
        Dim result As DialogResult = MessageBox.Show("請確定該公函使否審核通過?", "請選擇", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If result = DialogResult.Yes Then
            SQL_公函_控制表過審(單號_tb.Text, Today().ToString("yyyy/MM/dd"), gUserName)
            Set_清單1() : MsgBox("公函已過審!!")
        End If
    End Sub
End Class