vb控制word实例2

相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

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

[vb] view plaincopy

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.'返回段落文字,返回的段落文字在文本框控件中

19. Text1.Text = ""

20.For i = 1 To ActiveDocument.Paragraphs.Count

21. Text1.Text = Text1.Text & (ActiveDocument.Paragraphs(i).Range.Tex

t & vbCrLf & vbCrLf)

22.Next

23.

24.'控制分页

25. WordApp.Selection.EndKey unit:=wdStory '将光标移到文档末尾

26. WordApp.Selection.InsertBreak wdPageBreak '在文档末尾插入一页

27.

28.'设置图片格式的页眉

29.If ActiveWindow.View.SplitSpecial <> wdPaneNone Then

30. ActiveWindow.Panes(2).Close

31.End If

32.If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow.Acti

vePane.View.Type = wdOutlineView Then

33. ActiveWindow.ActivePane.View.Type = wdPrintView

34.End If

35. ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader

36. Selection.InlineShapes.AddPicture FileName:="F:\资料\My Pictures\2013年元

旦.gif", LinkToFile:=False, SaveWithDocument:=True'加载一图片文件作为页眉

37. Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft

38. ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

40.'设置文本格式的页眉

41.If ActiveWindow.View.SplitSpecial <> wdPaneNone Then

42. ActiveWindow.Panes(2).Close

43.End If

44.If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow.Acti

vePane.View.Type = wdOutlineView Then

45. ActiveWindow.ActivePane.View.Type = wdPrintView

46.End If

47. ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader

48. Selection.TypeText Text:="办公室常用工具"

49. ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

50.

51.'隐藏页眉的横线

52. WordApp.ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.

Borders(wdBorderBottom).Visible = False

53.

54.'取得页眉的内容

55. Debug.Print WordApp.ActiveDocument.Sections(1).Headers(wdHeaderFooterFir

stPage).Range.Text '获取WORD第一节的页眉的文字内容

56.

57.

58.'设置页脚

59.If ActiveWindow.View.SplitSpecial <> wdPaneNone Then

60. ActiveWindow.Panes(2).Close

61.End If

62.If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow.Acti

vePane.View.Type = wdOutlineView Then

63. ActiveWindow.ActivePane.View.Type = wdPrintView

64.End If

65. ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader

66.If Selection.HeaderFooter.IsHeader = True Then

67. ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter

68.Else

69. ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader

70.End If

71. Selection.TypeText Text:="2013年"'设置页脚

72. Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldNumPages

73. ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

74.

75. ActiveDocument.SaveAs "c:\MyWord.doc"'保存最后生成的word文档

76.

77.Errhandler:

78.Exit Sub

相关文档
最新文档