References 裡加入 Microsoft.Office.Interop.Word.Application()
References->Add->Assemblies->Extensions-> Check Microsoft.Office.Interop.Word
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | Imports Microsoft.Office.Interop Private Function ExportDOCG() As Boolean Try pgbExport.Value = 0 pgbExport.Maximum = Val(frmScreenChart.lvwScreenChart.Items.Count + 1) * Val(frmScreenChart.lvwScreenChart.Columns.Count) Dim objApp As Word.Application Dim objDoc As Word.Document objApp = New Word.Application() objDoc = objApp.Documents.Add objDoc.Activate() If cboSize.Text = "Letter" And cboOrientation.Text = "Vertical" Then objDoc.PageSetup.PaperSize = Word.WdPaperSize.wdPaperLetter objDoc.PageSetup.Orientation = Word.WdOrientation.wdOrientPortrait ElseIf cboSize.Text = "Letter" And cboOrientation.Text = "Horizontal" Then objDoc.PageSetup.PaperSize = Word.WdPaperSize.wdPaperLetter objDoc.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape ElseIf cboSize.Text = "A4" And cboOrientation.Text = "Vertical" Then objDoc.PageSetup.PaperSize = Word.WdPaperSize.wdPaperA4 objDoc.PageSetup.Orientation = Word.WdOrientation.wdOrientPortrait ElseIf cboSize.Text = "A4" And cboOrientation.Text = "Horizontal" Then objDoc.PageSetup.PaperSize = Word.WdPaperSize.wdPaperA4 objDoc.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape End If objDoc.PageSetup.TopMargin = Val(nudTop.Value) objDoc.PageSetup.BottomMargin = Val(nudDown.Value) objDoc.PageSetup.LeftMargin = Val(nudLeft.Value) objDoc.PageSetup.RightMargin = Val(nudRight.Value) Dim rng As Word.Range = objDoc.Range(0, 0) frmScreenChart.chtScreenChart.SaveImage(Application.StartupPath & "\Chart.jpg", 1) 'Dim ObjPic As Microsoft.Office.Interop.Word.InlineShape = rng.InlineShapes.AddPicture(Application.StartupPath & "\Chart.jpg") rng.InlineShapes.AddPicture(Application.StartupPath & "\Chart.jpg") rng.InsertBefore(frmScreenChart.Text) rng.Font.Size = 20 rng.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter rng.Font.Bold = True rng.InsertParagraphAfter() rng.InsertParagraphAfter() rng.SetRange(rng.End, rng.End) objDoc.Tables.Add(Range:=rng, NumRows:=frmScreenChart.lvwScreenChart.Items.Count + 1, NumColumns:=frmScreenChart.lvwScreenChart.Columns.Count) Dim tbl As Word.Table = objDoc.Tables(1) Dim rngCell As Word.Range For i As Integer = 1 To frmScreenChart.lvwScreenChart.Columns.Count rngCell = tbl.Cell(1, i).Range rngCell.Text = frmScreenChart.lvwScreenChart.Columns.Item(i - 1).Text rngCell.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter rngCell.Shading.ForegroundPatternColor = Word.WdColor.wdColorGray15 rngCell.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle rngCell.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle pgbExport.Value = Val(pgbExport.Value) + 1 Next For i As Integer = 1 To frmScreenChart.lvwScreenChart.Items.Count If frmScreenChart.lvwScreenChart.Items(i - 1).BackColor = Drawing.Color.SkyBlue Then For j As Integer = 1 To frmScreenChart.lvwScreenChart.Columns.Count rngCell = tbl.Cell(i + 1, j).Range rngCell.Shading.ForegroundPatternColor = Word.WdColor.wdColorGray15 rngCell.Text = frmScreenChart.lvwScreenChart.Items.Item(i - 1).SubItems(j - 1).Text rngCell.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft rngCell.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle rngCell.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle pgbExport.Value = Val(pgbExport.Value) + 1 Next j Else For j As Integer = 1 To frmScreenChart.lvwScreenChart.Columns.Count rngCell = tbl.Cell(i + 1, j).Range rngCell.Text = frmScreenChart.lvwScreenChart.Items.Item(i - 1).SubItems(j - 1).Text rngCell.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft rngCell.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle rngCell.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle pgbExport.Value = Val(pgbExport.Value) + 1 Next j End If Next i For Each section As Word.Section In objDoc.Sections Dim footerRange As Word.Range = section.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range footerRange.Text = "-" & frmScreenChart.Text & "- generado por ""Corporation Inventory Management""" footerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight Dim headerRange As Word.Range = section.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range headerRange.Text = "Fecha generado: " & DateTime.Now headerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight Next pgbExport.Value = pgbExport.Maximum Dim folderPath As String = txtDir.Text & "\" If Not Directory.Exists(folderPath) Then Directory.CreateDirectory(folderPath) End If objDoc.SaveAs(folderPath & txtFile.Text & ".docx") objDoc.Close() objApp.Quit() objDoc = Nothing objApp = Nothing Return True Catch ex As Exception MsgBox("Error Desconocido", 16, "Error") Return False End Try End Function |
沒有留言:
張貼留言