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 效果预览
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。