Option Strict Off 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 Imports System.IO Public Class 估價單 ReadOnly ds As New DataSet : ReadOnly ds1 As New DataSet : Dim boo As Boolean : Dim 新項次 As String Dim NUM1, NUM2 As Integer Dim xlApp As New 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 Private Sub 估價單_Load(sender As Object, e As EventArgs) Handles MyBase.Load Me.MdiParent = ICS_ASMS_ERP_SYS : Me.WindowState = 2 : Me.AutoScroll = True 選擇日期_dtp.CustomFormat = "yyyy/MM/dd" 選擇日期_dtp.Format = DateTimePickerFormat.Custom 確認新增_bt.Enabled = False : 修改_bt.Enabled = False : boo = False Set_清單1() SQL_讀取公司名稱() If dr.Read() Then 公司_tb.Text = dr("公司名稱") End If End Sub Private Sub Set_清單1() 控制表_dgv.DataSource = Nothing : ds.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 SQL_估價單_控制表清單() da.Fill(ds) : 控制表_dgv.DataSource = ds.Tables(0) : conn.Close() 控制表_dgv.Columns(1).FillWeight = 50 : 控制表_dgv.Columns(3).FillWeight = 50 控制表_dgv.Columns(0).Visible = False : 控制表_dgv.Columns(2).Visible = False 控制表_dgv.Columns(4).Visible = False : 控制表_dgv.Columns(5).Visible = False : 控制表_dgv.Columns(6).Visible = False 控制表_dgv.Columns(9).Visible = False : 控制表_dgv.Columns(8).Visible = False : 控制表_dgv.Columns(7).Visible = False End Sub Private Sub 控制表_dgv_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles 控制表_dgv.CellClick If boo = False Then If e.RowIndex = -1 Then : Else 確認新增_bt.Enabled = False : 修改_bt.Enabled = True 單號_tb.Text = 控制表_dgv(0, e.RowIndex).Value.ToString : 客戶_tb.Text = 控制表_dgv(1, e.RowIndex).Value.ToString 選擇日期_dtp.Text = 控制表_dgv(2, e.RowIndex).Value.ToString 選擇日期_dtp.CustomFormat = "yyyy/MM/dd" 選擇日期_dtp.Format = DateTimePickerFormat.Custom 廠牌_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 Dim value As String = 控制表_dgv(7, e.RowIndex).Value.ToString() Dim intValue As Integer If Int32.TryParse(value, intValue) Then ' 值是整數,格式化為千分位 總價_tb.Text = Strings.Format(intValue, "#,##0") Else ' 值不是整數,不格式化 總價_tb.Text = value End If 備註_tb.Text = 控制表_dgv(8, e.RowIndex).Value.ToString : 付款_tb.Text = 控制表_dgv(9, e.RowIndex).Value.ToString Set_清單2() End If End If End Sub Private Sub Set_清單2() 明細表_dgv.DataSource = Nothing : ds1.Clear() 明細表_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing 明細表_dgv.ColumnHeadersHeight = 25 明細表_dgv.AllowUserToAddRows = False 明細表_dgv.RowTemplate.Height = 35 明細表_dgv.EditMode = DataGridViewEditMode.EditOnEnter SQL_估價單_查詢明細表() : da.Fill(ds1) : 明細表_dgv.DataSource = ds1.Tables(0) : conn.Close() 明細表_dgv.Columns("項次").ReadOnly = True 明細表_dgv.Columns(0).Visible = False : 明細表_dgv.Columns(1).FillWeight = 30 : 明細表_dgv.Columns(2).FillWeight = 400 End Sub Private Sub 明細表_dgv_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles 明細表_dgv.CellEnter If e.RowIndex = -1 Then : Else 項次_tb.Text = 明細表_dgv(1, e.RowIndex).Value.ToString End If End Sub Private Sub 總價_tb_Leave(sender As Object, e As EventArgs) Handles 總價_tb.Leave If Not String.IsNullOrEmpty(總價_tb.Text) AndAlso IsNumeric(總價_tb.Text) Then ' 將文本轉換為數字 Dim number As Double = CDbl(總價_tb.Text) ' 格式化數字為千分位 總價_tb.Text = number.ToString("#,##0") End If End Sub Private Sub 新增_bt_Click(sender As Object, e As EventArgs) Handles 新增_bt.Click 新增_bt.Enabled = False : 確認新增_bt.Enabled = True : 修改_bt.Enabled = False Set_日期格式轉換() : Dim NUM1 As Integer For i As Integer = 1 To 999 NUM1 = 0 + i If NUM1 < 10 Then : 單號_tb.Text = "GD-" & DTP1 & "-00" & NUM1 ElseIf NUM1 > 9 And NUM1 < 100 Then : 單號_tb.Text = "GD-" & DTP1 & "-0" & NUM1 ElseIf NUM1 > 99 Then : 單號_tb.Text = "GD-" & DTP1 & "-" & NUM1 : End If PA = 單號_tb.Text : SQL_估價單_單號查詢() If dr.Read() Then : Else : i = 999 : conn.Close() : End If : conn.Close() Next 客戶_tb.Text = "" : 進廠_tb.Text = "" : 總價_tb.Text = "0" : 業務_tb.Text = "" 廠牌_tb.Text = "" : 年分_tb.Text = "" : 備註_tb.Text = "" : 付款_tb.Text = "" SQL_估價單_控制表新增() : boo = True : Set_清單2() End Sub Private Sub Set_日期格式轉換() Dim selectedDate As DateTime = 選擇日期_dtp.Value DTP4 = Strings.Format(selectedDate, "yyyy/MM/dd") : DTP1 = Strings.Format(selectedDate, "yyyyMMdd") End Sub Private Sub 確認新增_bt_Click(sender As Object, e As EventArgs) Handles 確認新增_bt.Click If 客戶_tb.Text = "" Or 廠牌_tb.Text = "" Then MsgBox("客戶及廠牌車種未輸入完成!!") Else 新增_bt.Enabled = True : 確認新增_bt.Enabled = False : 修改_bt.Enabled = True : boo = False 選擇日期_dtp.CustomFormat = "yyyy/MM/dd" 選擇日期_dtp.Format = DateTimePickerFormat.Custom SQL_估價單_控制表修改() : conn.Close() : MsgBox("儲存成功!!") : Set_清單1() End If End Sub Private Sub 修改_bt_Click(sender As Object, e As EventArgs) Handles 修改_bt.Click 選擇日期_dtp.CustomFormat = "yyyy/MM/dd" 選擇日期_dtp.Format = DateTimePickerFormat.Custom SQL_估價單_控制表修改() : conn.Close() : MsgBox("儲存成功!!") : Set_清單1() End Sub Private Sub 刪除_bt_Click(sender As Object, e As EventArgs) Handles 刪除_bt.Click 新增_bt.Enabled = True : 確認新增_bt.Enabled = False : 修改_bt.Enabled = False : boo = False SQL_估價單_控制表刪除() : SQL_估價單_明細表刪除() : conn.Close() : MsgBox("刪除成功") 客戶_tb.Text = "" : 進廠_tb.Text = "" : 總價_tb.Text = "" : 業務_tb.Text = "" 廠牌_tb.Text = "" : 年分_tb.Text = "" : 備註_tb.Text = "" : 付款_tb.Text = "" 單號_tb.Text = "" Set_清單1() : Set_清單2() : 項次_tb.Text = "" End Sub Private Sub 刪除選中的一筆資料_bt_Click(sender As Object, e As EventArgs) Handles 刪除選中的一筆資料_bt.Click If 項次_tb.Text = "" Then MsgBox("未選擇需要刪除的資料。") Else For i As Integer = 0 To 明細表_dgv.Rows.Count - 1 PA1 = 明細表_dgv.Rows(i).Cells("項次").Value.ToString : PA2 = 明細表_dgv.Rows(i).Cells("內容").Value.ToString SQL_估價單_修改明細表() : conn.Close() Next SQL_估價單_刪除明細表項次() : SQL_估價單_明細表項次更新() : conn.Close() Set_清單2() : 項次_tb.Text = "" : MsgBox("刪除成功。") End If End Sub Private Sub 申請_bt_Click(sender As Object, e As EventArgs) Handles 申請_bt.Click For i As Integer = 0 To 明細表_dgv.Rows.Count - 1 PA1 = 明細表_dgv.Rows(i).Cells("項次").Value.ToString : PA2 = 明細表_dgv.Rows(i).Cells("內容").Value.ToString SQL_估價單_修改明細表() : conn.Close() Next : Set_清單2() : MsgBox("儲存成功。") End Sub Private Sub 查詢_bt_Click(sender As Object, e As EventArgs) Handles 查詢_bt.Click CHB("請輸入要查詢的單號資料。") : PA56 = 訊息回應 : Set_清單1() : PA56 = "" End Sub Private Sub 收支單中新增一筆空白資料_bt_Click(sender As Object, e As EventArgs) Handles 收支單中新增一筆空白資料_bt.Click If 單號_tb.Text = "" Then MsgBox("未選擇單號!!") Else If 明細表_dgv.Rows.Count > 0 Then For i As Integer = 0 To 明細表_dgv.Rows.Count - 1 PA1 = 明細表_dgv.Rows(i).Cells("項次").Value.ToString : PA2 = 明細表_dgv.Rows(i).Cells("內容").Value.ToString SQL_估價單_修改明細表() : conn.Close() Next End If SQL_估價單_查詢最後一筆單號項次() If dr.Read() Then 新項次 = dr("項次") NUM1 = Double.Parse(新項次) + 1 Else NUM1 = 1 End If : conn.Close() PA15 = NUM1.ToString() SQL_估價單_新增明細表() : conn.Close() Set_清單2() MsgBox("明細表新增成功!!") End If 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 刪除_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 匯出EXCEL_bt.MouseEnter ToolTip1.SetToolTip(Me.匯出EXCEL_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 刪除選中的一筆資料_bt_MouseEnter(sender As Object, e As EventArgs) Handles 刪除選中的一筆資料_bt.MouseEnter ToolTip1.SetToolTip(Me.刪除選中的一筆資料_bt, "刪除選中的一筆資料") End Sub Private Sub 匯出EXCEL_bt_Click(sender As Object, e As EventArgs) Handles 匯出EXCEL_bt.Click 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, 3) = 公司_tb.Text xlSheet.Cells(2, 3) = "估 價 單" Dim selectedDate As Date = 選擇日期_dtp.Value Dim rocYear As Integer = selectedDate.Year - 1911 Dim rocDate As String = rocYear.ToString() & "年" & selectedDate.Month.ToString() & "月" & selectedDate.Day.ToString() & "日" xlSheet.Cells(3, 2) = "客戶 : " & 客戶_tb.Text : xlSheet.Cells(3, 4) = "報價日期 : " & rocDate xlSheet.Cells(4, 2) = "廠牌/車種 : " & 廠牌_tb.Text : xlSheet.Cells(4, 4) = "進廠 : " & 進廠_tb.Text xlSheet.Cells(5, 2) = "車身型式 : " & 年分_tb.Text : xlSheet.Cells(5, 4) = "業務 : " & 業務_tb.Text xlSheet.Cells(7, 3) = "施工說明 : (以合格證標準規格承製)" For i As Integer = 0 To 明細表_dgv.Rows.Count - 1 xlSheet.Cells(i + 8, 2) = 明細表_dgv.Rows(i).Cells("項次").Value : xlSheet.Cells(i + 8, 3) = 明細表_dgv.Rows(i).Cells("內容").Value N2 = 8 + i Next i Dim number As Double = CDbl(總價_tb.Text) xlSheet.Cells(N2 + 1, 3) = "總計 :" & number.ToString("#,##0") & " 元(不含稅)" xlSheet.Cells(N2 + 2, 3) = "備註 :" & 備註_tb.Text xlSheet.Cells(N2 + 3, 3) = "付款方式 :" & 付款_tb.Text BB(xlApp, xlSheet) '未改 xlApp.Sheets(2).Delete : xlSheet.PageSetup.PrintArea = "" '----轉成PDF----- Dim pdfFolderPath As String = Path.Combine(System.Windows.Forms.Application.StartupPath, "pdf") If Not Directory.Exists(pdfFolderPath) Then Directory.CreateDirectory(pdfFolderPath) End If Dim pdfFilePath As String = Path.Combine(pdfFolderPath, 單號_tb.Text & "-估價單.pdf") xlBook.Sheets(1).ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, pdfFilePath, XlFixedFormatQuality.xlQualityStandard, True) '-------- xlApp.Cells.Select() : xlApp.Application.WindowState = xlMinimized : xlBook.Close(False) Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet) Runtime.InteropServices.Marshal.ReleaseComObject(xlBook) Runtime.InteropServices.Marshal.ReleaseComObject(xlApp) xlApp.Quit() : xlSheet = Nothing : xlBook = Nothing : xlApp = Nothing : GC.Collect() Try Dim chromePath As String = "C:\Program Files\Google\Chrome\Application\chrome.exe" If System.IO.File.Exists(chromePath) Then Dim process As New Process() process.StartInfo.FileName = "chrome" process.StartInfo.Arguments = """" & pdfFilePath & """" process.Start() Else : Dim edgePath As String = "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" If System.IO.File.Exists(edgePath) Then Dim process As New Process() process.StartInfo.FileName = "msedge" process.StartInfo.Arguments = """" & pdfFilePath & """" process.Start() Else Process.Start(pdfFilePath) End If End If Catch ex As Exception MsgBox("以存檔至桌面 。" & vbCrLf & "檔案名稱:" & 單號_tb.Text & "-估價單.pdf") End Try End Sub Private Sub BB(ByVal myExcel As Microsoft.Office.Interop.Excel.Application, ByVal xlSheet As Microsoft.Office.Interop.Excel.Worksheet) xlSheet.Cells.Select() With myExcel.Selection.Font .Name = "微軟正黑體" .Size = 14 .Strikethrough = False .Superscript = False .Subscript = False .OutlineFont = False .Shadow = False .Underline = xlUnderlineStyleNone .ThemeColor = xlThemeColorLight1 .TintAndShade = 0 .ThemeFont = xlThemeFontNone End With xlSheet.Range("B7:D" & N2 + 3).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("C1").Select() With myExcel.Selection.Font .Name = "微軟正黑體" .Size = 20 .Strikethrough = False .Superscript = False .Subscript = False .OutlineFont = False .Shadow = False .Underline = xlUnderlineStyleNone .ThemeColor = xlThemeColorLight1 .TintAndShade = 0 .ThemeFont = xlThemeFontNone End With xlSheet.Range("C2").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.Rows("1:1").EntireRow.AutoFit xlSheet.Rows("2:2").EntireRow.AutoFit xlSheet.Columns("A:A").ColumnWidth = 8 xlSheet.Columns("B:B").ColumnWidth = 5 xlSheet.Columns("C:C").ColumnWidth = 35 xlSheet.Columns("D:D").ColumnWidth = 40 xlSheet.Range("B1:D1").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("B2:D2").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("B3:C3").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("B4:C4").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("B5:C5").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("B7:D7").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 For i As Integer = 8 To N2 + 3 xlSheet.Range("C" & i & ":D" & i).Select() If i > N2 Then With myExcel.Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Else With myExcel.Selection .HorizontalAlignment = xlLeft .VerticalAlignment = xlCenter .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With End If myExcel.Selection.Merge xlSheet.Range("B" & i).Select() With myExcel.Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Next i For i = 1 To N2 + 3 xlSheet.Rows(i).RowHeight = xlSheet.Rows(i).RowHeight + 5 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 = False .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 End Class