VB控制Word文档实例精选一

来源:互联网 发布:手机压缩图片软件 编辑:程序博客网 时间:2024/05/24 03:20

        1、在Word文档中插入表格,给单元格赋值,访问单元格内容,拆分及合并单元格

'先引用Microsoft Word 11.0 Object LibraryOption ExplicitDim WordApp As Word.Application '创建Word应用程序Private Sub Command1_Click()        Set WordApp = New Word.Application '实例化    WordApp.Visible = True '显示 Office Word 界面    '或者Application.Visible = True    WordApp.DisplayAlerts = False '不提示保存对话框    WordApp.Documents.Add '创建新的空白Word文档    WordApp.Selection.EndKey unit:=wdStory  '将光标移到文档末尾,在文本后面插入表格    Selection.TypeText Text:="我的Word表格" '表格的标题名称    Call WordApp.ActiveDocument.Tables.Add(WordApp.Application.Selection.Range, 10, 5, 1, 0) '插入一个10行5列的表格    Selection.Tables(1).Columns.Width = 80 '定义表格的列宽        '给单元格赋值    WordApp.ActiveDocument.Tables(1).Cell(1, 1).Range.InsertAfter "序号"    WordApp.ActiveDocument.Tables(1).Cell(1, 2).Range.InsertAfter "项目1"    WordApp.ActiveDocument.Tables(1).Cell(1, 3).Range.InsertAfter "项目2"    WordApp.ActiveDocument.Tables(1).Cell(1, 4).Range.InsertAfter "项目3"    WordApp.ActiveDocument.Tables(1).Cell(1, 5).Range.InsertAfter "项目4"        '合并单元格    WordApp.ActiveDocument.Tables(1).Cell(2, 2).Select '选中表格的第2行第2列    Call WordApp.Application.Selection.MoveDown(5, 3, 1) '向下移动3格    WordApp.Application.Selection.Cells.Merge '合并4个格子        '拆分单元格    WordApp.ActiveDocument.Tables(1).Cell(10, 2).Select '选中表格的第10行第2列    Call WordApp.Application.Selection.Cells.Split(7, 2, True) '拆分成7行2列            '访问单元格内容    Debug.Print WordApp.ActiveDocument.Tables(1).Cell(1, 1).Range.Text '第1行第1列的内容        ActiveDocument.SaveAs "c:\MyWord.doc" '保存最后生成的word文档End SubPrivate Sub Form_Unload(Cancel As Integer)    On Error Resume Next    WordApp.Quit    Set WordApp = NothingEnd Sub

        生成的表格如下图所示:


        2、在Word文档中插入和导出图片对象

'先引用Microsoft Word 11.0 Object LibraryOption ExplicitDim WordApp As Word.Application '创建Word应用程序Private Sub Command1_Click()    On Error GoTo Errhandler    CommonDialog1.Filter = "Word(*.Doc)|*.Doc|AllFile(*.*)|*.*"    CommonDialog1.FilterIndex = 1    CommonDialog1.ShowOpen    Set WordApp = New Word.Application '实例化    WordApp.Documents.Open CommonDialog1.FileName '打开Word文件    WordApp.Visible = True '显示 Office Word 界面    '或者Application.Visible = True    WordApp.DisplayAlerts = False '不提示保存对话框    WordApp.Selection.EndKey Unit:=wdStory  '将光标移到文档末尾,在文本后面插入图片对象    Selection.TypeText Text:="我的图片" '图片的标题名称        '插入图片对象    Selection.InlineShapes.AddPicture FileName:="C:\CommandPicture.jpg", LinkToFile:=False, SaveWithDocument:=True    Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend    Selection.InlineShapes(1).ConvertToShape.Select    Selection.ShapeRange.Fill.Visible = msoFalse    Selection.ShapeRange.Fill.Transparency = 0#    Selection.ShapeRange.Line.Weight = 0.75    Selection.ShapeRange.Line.DashStyle = msoLineSolid    Selection.ShapeRange.Line.Style = msoLineSingle    Selection.ShapeRange.Line.Transparency = 0#    Selection.ShapeRange.Line.Visible = msoFalse    Selection.ShapeRange.LockAspectRatio = msoTrue    Selection.ShapeRange.Height = 361.4    Selection.ShapeRange.Width = 481.6    Selection.ShapeRange.PictureFormat.Brightness = 0.5    Selection.ShapeRange.PictureFormat.Contrast = 0.5    Selection.ShapeRange.PictureFormat.ColorType = msoPictureAutomatic    Selection.ShapeRange.PictureFormat.CropLeft = 0#    Selection.ShapeRange.PictureFormat.CropRight = 0#    Selection.ShapeRange.PictureFormat.CropTop = 0#    Selection.ShapeRange.PictureFormat.CropBottom = 0#    Selection.ShapeRange.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn    Selection.ShapeRange.RelativeVerticalPosition = wdRelativeVerticalPositionPage    Selection.ShapeRange.Left = wdShapeCenter    Selection.ShapeRange.Top = wdShapeCenter    Selection.ShapeRange.LockAnchor = False    Selection.ShapeRange.WrapFormat.AllowOverlap = True    Selection.ShapeRange.WrapFormat.Side = wdWrapBoth    Selection.ShapeRange.WrapFormat.DistanceTop = CentimetersToPoints(0)    Selection.ShapeRange.WrapFormat.DistanceBottom = CentimetersToPoints(0)    Selection.ShapeRange.WrapFormat.DistanceLeft = CentimetersToPoints(0.32)    Selection.ShapeRange.WrapFormat.DistanceRight = CentimetersToPoints(0.32)    Selection.ShapeRange.WrapFormat.Type = 3    Selection.ShapeRange.ZOrder msoSendBehindText  '设置图片为衬托于文字下方    '判断文档中是否存在图片对象    If ActiveDocument.Shapes.Count + ActiveDocument.InlineShapes.Count > 0 Then       '取得图片的2种方法              '第1种方法:用下面命令将文件另存为网页格式的文件,文件夹“MyWord.files”将保存Word文档中所有的图片       '这种方法对所有的Word版本均适用       ActiveDocument.SaveAs "c:\MyWord.htm", wdFormatHTML '保存为网页格式              '第2种方法:引用ADO对象库,将所有的图片保存在数据库中,然后可以一张一张地显示出来              '另外:       '如果Word文档是docx格式的,那可以按这个办法解决:       '.docx 格式的文件本质上是一个ZIP压缩文件,.docx 格式文件的主要内容是保存为XML格式的,但文件并非直接保存于磁盘。       '它是保存在一个ZIP文件中,然后取扩展名为.docx。我们只需要用解压软件比如:WinZIP、WinRAR或者7ZIP等软件进行解压就可以了。       '方法有两种,一种是将.docx后缀名修改为.zip后缀名;另一个方法就是打开WinZIP然后,选择此文档即可。       '图片资源文件都被保存在word\media文件夹中。                      Else       Debug.Print "Word文档中不存在图片对象!"    End IfErrhandler:    Exit SubEnd SubPrivate Sub Form_Unload(Cancel As Integer)    On Error Resume Next    WordApp.Quit    Set WordApp = NothingEnd Sub


        3、统计Word文档页数、字数

'先引用Microsoft Word 11.0 Object LibraryOption ExplicitDim WordApp As Word.Application '创建Word应用程序Private Sub Command1_Click()    On Error GoTo Errhandler    CommonDialog1.Filter = "Word(*.Doc)|*.Doc|AllFile(*.*)|*.*"    CommonDialog1.FilterIndex = 1    CommonDialog1.ShowOpen    Set WordApp = New Word.Application '实例化    WordApp.Documents.Open CommonDialog1.FileName '打开Word文件    WordApp.Visible = False '不显示 Office Word 界面    '或者Application.Visible = True    'WordApp.DisplayAlerts = False '不提示保存对话框        Debug.Print WordApp.ActiveDocument.ComputeStatistics(Word.WdStatistic.wdStatisticPages) '页数    Debug.Print WordApp.ActiveDocument.Characters.Count '字数    Errhandler:    Exit SubEnd SubPrivate Sub Form_Unload(Cancel As Integer)    On Error Resume Next    WordApp.Quit    Set WordApp = NothingEnd Sub


 

原创粉丝点击