1、添加引用->COM->Microsoft Word 11.0 Object Library
2、Imports Word
''' <summary> ''' 创建word文档 ''' </summary> ''' <remarks></remarks> Public Sub CreateDoc() Dim docApp As New Word.Application Dim doc As Word.Document Dim miss As Object = System.Reflection.Missing.Value doc = docApp.Documents.Add(miss,miss,miss) Dim strContext As String = "" '文件内容 strContext = "你好,我简单的幸福." '行距 docApp.Selection.ParagraphFormat.LineSpacing = 15 '页眉 With docApp .ActiveWindow.View.Type = Word.WdViewType.wdOutlineView .ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader .ActiveWindow.ActivePane.Selection.InsertAfter("[页眉内容]") .Selection.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphRight .ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument '跳出页眉设置 End With '页尾 With docApp .ActiveWindow.View.Type = Word.WdViewType.wdOutlineView .ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryFooter .ActiveWindow.ActivePane.Selection.InsertAfter("[页尾内容]") .Selection.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphRight .ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument End With '表格 Dim table As Word.Table = doc.Tables.Add(docApp.Selection.Range,10,3) '表格样式 With table .Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle .Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle .Columns(1).Width = 100 .Columns(2).Width = 220 .Columns(3).Width = 105 End With '填充表格内容 With table .Cell(1,1).Range.Text = "产品详细信息表" .Cell(1,1).Range.Bold = 2 '粗体 .Cell(1,1).Range.Font.Color = WdColor.wdColorBrown '合并单元格 .Cell(1,1).Merge(.Cell(2,3)) '横向合并 End With With docApp.Selection .Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter '垂直居中 .ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter '水平居中 End With With table .Cell(2,1).Range.Text = "品牌名称:" .Cell(3,1).Range.Text = "史丹奴" End With '纵向合并单元格 table.Cell(3,3).Select() '选中一行 Dim moveUnit As Object = Word.WdUnits.wdLine Dim moveCount As Object = 5 Dim moveExtend As Object = WdMovementType.wdExtend With docApp .Selection.MoveDown(moveUnit,moveCount,moveExtend) .Selection.Cells.Merge() End With '插入图片 Dim strPicPath As String = "http://www.google.com.hk/intl/zh-CN/images/logo_cn.png" Dim linkToFile As Object = False Dim saveWithDoc As Object = True Dim anchor As Object = doc.Application.Selection.Range With doc.Application.ActiveDocument .InlineShapes.AddPicture(strPicPath,linkToFile,saveWithDoc,anchor) .InlineShapes(1).Width = 100 '图片宽度 .InlineShapes(1).Height = 100 '图片高度 End With '设置图片为四周环绕型 Dim s As Word.Shape s = doc.Application.ActiveDocument.InlineShapes(1).ConvertToShape() s.WrapFormat.Type = WdWrapType.wdWrapSquare '添加新行 doc.Content.Tables(1).Rows.Add() doc.Paragraphs.Last.Range.Text = strContext doc.Paragraphs.Last.Range.Text = "文档创建时间:" & Format(Now,"yyyy-MM-dd HH:mm:ss") '“落款” doc.Paragraphs.Last.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight Dim f As Object = doc.SaveFormat '保存文件 doc.SaveAs("c:/新年第二天.doc") doc.Close() docApp.Quit() End Sub
3 效果预览