VB控制Word文档实例精选二

前端之家收集整理的这篇文章主要介绍了VB控制Word文档实例精选二前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1、返回Word文档的段落文字,控制分页,设置页眉和页脚

  1. '先引用Microsoft Word 11.0 Object Library
  2. Option Explicit
  3.  
  4. Dim WordApp As Word.Application '创建Word应用程序
  5.  
  6. Private Sub Command1_Click()
  7. Dim i As Long
  8. On Error GoTo Errhandler
  9. CommonDialog1.Filter = "Word(*.Doc)|*.Doc|AllFile(*.*)|*.*"
  10. CommonDialog1.FilterIndex = 1
  11. CommonDialog1.ShowOpen
  12. Set WordApp = New Word.Application '实例化
  13. WordApp.Documents.Open CommonDialog1.FileName '打开Word文件
  14. WordApp.Visible = True '显示 Office Word 界面
  15. '或者Application.Visible = True
  16. WordApp.DisplayAlerts = False '不提示保存对话框
  17. '返回段落文字,返回的段落文字在文本框控件中
  18. Text1.Text = ""
  19. For i = 1 To ActiveDocument.Paragraphs.Count
  20. Text1.Text = Text1.Text & (ActiveDocument.Paragraphs(i).Range.Text & vbCrLf & vbCrLf)
  21. Next
  22. '控制分页
  23. WordApp.Selection.EndKey unit:=wdStory '将光标移到文档末尾
  24. WordApp.Selection.InsertBreak wdPageBreak '在文档末尾插入一页
  25. '设置图片格式的页眉
  26. If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
  27. ActiveWindow.Panes(2).Close
  28. End If
  29. If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow.ActivePane.View.Type = wdOutlineView Then
  30. ActiveWindow.ActivePane.View.Type = wdPrintView
  31. End If
  32. ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
  33. Selection.InlineShapes.AddPicture FileName:="F:\资料\My Pictures\2013年元旦.gif",LinkToFile:=False,SaveWithDocument:=True '加载一图片文件作为页眉
  34. Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
  35. ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
  36. '设置文本格式的页眉
  37. If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
  38. ActiveWindow.Panes(2).Close
  39. End If
  40. If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow.ActivePane.View.Type = wdOutlineView Then
  41. ActiveWindow.ActivePane.View.Type = wdPrintView
  42. End If
  43. ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
  44. Selection.TypeText Text:="办公室常用工具"
  45. ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
  46. '隐藏页眉的横线
  47. WordApp.ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Borders(wdBorderBottom).Visible = False
  48. '取得页眉的内容
  49. Debug.Print WordApp.ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Range.Text '获取WORD第一节的页眉的文字内容
  50. '设置页脚
  51. If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
  52. ActiveWindow.Panes(2).Close
  53. End If
  54. If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow.ActivePane.View.Type = wdOutlineView Then
  55. ActiveWindow.ActivePane.View.Type = wdPrintView
  56. End If
  57. ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
  58. If Selection.HeaderFooter.IsHeader = True Then
  59. ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
  60. Else
  61. ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
  62. End If
  63. Selection.TypeText Text:="2013年" '设置页脚
  64. Selection.Fields.Add Range:=Selection.Range,Type:=wdFieldNumPages
  65. ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
  66. ActiveDocument.SaveAs "c:\MyWord.doc" '保存最后生成word文档
  67. Errhandler:
  68. Exit Sub
  69. End Sub
  70.  
  71. Private Sub Form_Unload(Cancel As Integer)
  72. On Error Resume Next
  73. WordApp.Quit
  74. Set WordApp = Nothing
  75. End Sub


效果图如下:

2、控制Word文档中的文本框对象

  1. '先引用Microsoft Word 11.0 Object Library
  2. Option Explicit
  3.  
  4. Dim WordApp As Word.Application '创建Word应用程序
  5.  
  6. Private Sub Command1_Click()
  7. On Error GoTo Errhandler
  8. CommonDialog1.Filter = "MS Office Word(*.Doc)|*.Doc|AllFile(*.*)|*.*"
  9. CommonDialog1.FilterIndex = 1
  10. CommonDialog1.ShowOpen
  11. Set WordApp = New Word.Application '实例化
  12. WordApp.Documents.Open CommonDialog1.FileName '打开Word文件
  13. If Documents.Count >= 1 Then
  14. Text1.Text = "打开的Word文件是:" & ActiveDocument.Name & vbCrLf & vbCrLf
  15. End If
  16. WordApp.Visible = True '显示 Office Word 界面
  17. '或者Application.Visible = True
  18. WordApp.DisplayAlerts = False '不提示保存对话框
  19. WordApp.Selection.EndKey unit:=wdStory '将光标移到文档末尾
  20. WordApp.Selection.Font.Bold = 1
  21. WordApp.Selection.Font.Name = "黑体"
  22. WordApp.Selection.Font.Size = 18
  23. WordApp.Selection.TypeText Text:="在Word文件中插入文本框对象"
  24. WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter '居中显示
  25. '创建文本框对象,座标(100100),宽度200,高度200
  26. With ActiveDocument.Shapes.AddTextBox(msoTextOrientationHorizontal,100,400,300).Fill
  27. '.Transparency = 1 '设置透明色
  28. .ForeColor = vbRed '设置前景颜色
  29. .UserPicture ("F:\资料\My Pictures\758254_960x1000_0.jpg") '设置文本框对象的背景图片
  30. End With
  31. ActiveDocument.Shapes(1).TextFrame.TextRange.Text = "这是一个美女" '给文本框赋值
  32. 'ActiveDocument.Shapes(1).Line.Transparency = 1 '设置透明边框线条
  33. '再创建一个透明背景的文本框对象
  34. With ActiveDocument.Shapes.AddTextBox(msoTextOrientationHorizontal,300).Fill
  35. .Transparency = 1 '设置透明色背景
  36. .ForeColor = vbRed '设置前景颜色
  37. End With
  38. ActiveDocument.Shapes(2).TextFrame.TextRange.Text = "这是一个透明背景的文本框" '给文本框赋值
  39. 'ActiveDocument.Shapes(2).Line.Transparency = 1 '设置透明边框线条
  40. '下面是获取文本框对象的内容
  41. Dim i As Long
  42. For i = 1 To ActiveDocument.Shapes.Count
  43. Text1.Text = Text1.Text & ("第" & i & "个文本框的内容:" & ActiveDocument.Shapes(i).TextFrame.TextRange.Text & vbCrLf)
  44. Next
  45. ActiveDocument.SaveAs "c:\MyWord.doc" '保存最后生成的word文档
  46. Errhandler:
  47. Exit Sub
  48. End Sub
  49.  
  50. Private Sub Form_Unload(Cancel As Integer)
  51. On Error Resume Next
  52. WordApp.Quit
  53. Set WordApp = Nothing
  54. End Sub


效果图如下:

3、在Word文档中设置Excel风格的页码

  1. '先引用Microsoft Word 11.0 Object Library
  2. Option Explicit
  3.  
  4. Dim WordApp As Word.Application '创建Word应用程序
  5. Dim WordDoc As Word.Document '创建Word文档对象
  6.  
  7. Private Sub Command1_Click()
  8. Dim i As Long
  9. On Error GoTo Errhandler
  10. CommonDialog1.Filter = "Word(*.Doc)|*.Doc|AllFile(*.*)|*.*"
  11. CommonDialog1.FilterIndex = 1
  12. CommonDialog1.ShowOpen
  13. Set WordApp = New Word.Application '实例化
  14. Set WordDoc = WordApp.Documents.Open(CommonDialog1.FileName) '选择并打开Word文件
  15. WordApp.Visible = True '显示 Office Word 界面
  16. '或者Application.Visible = True
  17. WordApp.DisplayAlerts = False '提示保存对话框
  18. '设置Word文档第一页页码
  19. Dim WordRange As Range
  20. Set WordRange = WordApp.ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range
  21. With WordRange
  22. .InsertAfter "第"
  23. .Font.Size = 14
  24. .Collapse Direction:=wdCollapseEnd
  25. '插入页码
  26. .Fields.Add Range:=WordRange,Type:=wdFieldEmpty,Text:="PAGE \* Arabic ",PreserveFormatting:=True
  27. .Expand unit:=wdWord
  28. .InsertAfter "页 "
  29. .InsertAfter "共"
  30. .Collapse Direction:=wdCollapseEnd
  31. '插入页数域
  32. .Fields.Add Range:=WordRange,Text:="NUMPAGES \* Arabic ",PreserveFormatting:=True
  33. .Expand unit:=wdWord
  34. .InsertAfter "页"
  35.  
  36. .InsertAfter "【我的Word文件 作者:ChenJL1031(东方之珠)】"
  37. .ParagraphFormat.Alignment = wdAlignParagraphRight '右对齐
  38. End With
  39. 'Text1.Text = WordApp.ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Range.Text
  40. Set WordRange = Nothing
  41. ActiveDocument.SaveAs "c:\MyWord.doc" '保存最后生成word文档
  42. Errhandler:
  43. Exit Sub
  44. End Sub
  45.  
  46. Private Sub Form_Unload(Cancel As Integer)
  47. On Error Resume Next
  48. WordApp.Quit
  49. Set WordApp = Nothing
  50. End Sub
  51.  
  52.  


效果图如下:

猜你在找的VB相关文章