用VB操作word方法_第1页
用VB操作word方法_第2页
用VB操作word方法_第3页
用VB操作word方法_第4页
用VB操作word方法_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、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.    &#

2、160;Dim i As Long  8.     On Error GoTo Errhandler  9.     CommonDialog1.Filter = "Word(*.Doc)|*.Doc|AllFile(*.*)|*.*"  10.     CommonDialog1.FilterIndex =

3、 1  11.     CommonDialog1.ShowOpen  12.     Set WordApp = New Word.Application '实例化  13.      CommonDialog1.FileName '打开Word文件  14.    &#

4、160;WordApp.Visible = True '显示 Office Word 界面  15.     '或者Application.Visible = True  16.     WordApp.DisplayAlerts = False '不提示保存对话框  17.     

5、  18.     '返回段落文字,返回的段落文字在文本框控件中  19.     Text1.Text = ""  20.     For i = 1 To   21.         Text1.Text =

6、 Text1.Text & (ActiveDocument.Paragraphs(i).Range.Text & vbCrLf & vbCrLf)  22.     Next  23.       24.     '控制分页  25.      u

7、nit:=wdStory  '将光标移到文档末尾  26.      wdPageBreak '在文档末尾插入一页  27.       28.     '设置图片格式的页眉  29.     If  <> wdPaneNone Then

8、60; 30.        ActiveWindow.Panes(2).Close  31.     End If  32.     If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow.ActivePane.View.Type = w

9、dOutlineView Then  33.         = wdPrintView  34.     End If  35.     ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader  36.   

10、0;  FileName:="F:资料My Pictures2013年元旦.gif", LinkToFile:=False, SaveWithDocument:=True '加载一图片文件作为页眉  37.      = wdAlignParagraphLeft  38.      = wdSeekMainDocument  39

11、.       40.     '设置文本格式的页眉  41.     If  <> wdPaneNone Then  42.        ActiveWindow.Panes(2).Close  43.     End

12、0;If  44.     If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow.ActivePane.View.Type = wdOutlineView Then  45.         = wdPrintView  46.  

13、60;  End If  47.     ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader  48.     Selection.TypeText Text:="办公室常用工具"  49.      = wdSeekMainDocument&

14、#160; 50.       51.     '隐藏页眉的横线  52.     WordApp.ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Borders(wdBorderBottom).Visible = False  53.      &#

15、160;54.     '取得页眉的内容  55.     Debug.Print WordApp.ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Range.Text  '获取WORD第一节的页眉的文字内容  56.       57.      &

16、#160;58.     '设置页脚  59.     If  <> wdPaneNone Then  60.        ActiveWindow.Panes(2).Close  61.     End If  62.    

17、 If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow.ActivePane.View.Type = wdOutlineView Then  63.         = wdPrintView  64.     End If  

18、65.     ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader  66.     If  = True Then  67.        ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPag

19、eFooter  68.     Else  69.        ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader  70.     End If  71.     Selection.TypeText Tex

20、t:="2013年" '设置页脚  72.     Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldNumPages  73.      = wdSeekMainDocument  74.       75.    

21、0;ActiveDocument.SaveAs "c:MyWord.doc" '保存最后生成的word文档  76.           77. Errhandler:  78.     Exit Sub  79. End Sub  80.   81. Private Sub

22、60;Form_Unload(Cancel As Integer)  82.     On Error Resume Next  83.     WordApp.Quit  84.     Set WordApp = Nothing  85. End Sub  效果图如下: &#

23、160;       2、控制Word文档中的文本框对象vb view plaincopy1. '先引用Microsoft Word 11.0 Object Library  2. Option Explicit  3.   4. Dim WordApp As Word.Application '创建Word应用程序  5.  &#

24、160;6. Private Sub Command1_Click()  7.     On Error GoTo Errhandler  8.     CommonDialog1.Filter = "MS Office Word(*.Doc)|*.Doc|AllFile(*.*)|*.*"  9.     

25、CommonDialog1.FilterIndex = 1  10.     CommonDialog1.ShowOpen  11.     Set WordApp = New Word.Application '实例化  12.      CommonDialog1.FileName '打开Word文件

26、0; 13.     If Documents.Count >= 1 Then  14.        Text1.Text = "打开的Word文件是:" & ActiveDocument.Name & vbCrLf & vbCrLf  15.   

27、;  End If  16.     WordApp.Visible = True '显示 Office Word 界面  17.     '或者Application.Visible = True  18.     WordApp.DisplayAlerts = Fals

28、e '不提示保存对话框  19.       20.      unit:=wdStory  '将光标移到文档末尾  21.      = 1  22.      = "黑体"  23.    

29、  = 18  24.      Text:="在Word文件中插入文本框对象"  25.     WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter '居中显示  26.     &

30、#160;     27.     '创建文本框对象,座标(100,100),宽度200,高度200  28.     With ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 100, 100, 400, 300).Fill  29.     

31、0;    '.Transparency = 1 '设置透明色  30.          .ForeColor = vbRed '设置前景颜色  31.          .UserPicture ("F:资料My Pict

32、ures758254_960x1000_0.jpg") '设置文本框对象的背景图片  32.     End With  33.     ActiveDocument.Shapes(1). = "这是一个美女" '给文本框赋值  34.     'ActiveDocument.Shapes(1).Line.Tran

33、sparency = 1 '设置透明边框线条  35.       36.     '再创建一个透明背景的文本框对象  37.     With ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 100, 400, 400, 300).Fill&#

34、160; 38.          .Transparency = 1 '设置透明色背景  39.          .ForeColor = vbRed '设置前景颜色  40.     End With  41. 

35、60;   ActiveDocument.Shapes(2). = "这是一个透明背景的文本框" '给文本框赋值  42.     'ActiveDocument.Shapes(2).Line.Transparency = 1 '设置透明边框线条  43.       44.     

36、'下面是获取文本框对象的内容  45.     Dim i As Long      46.     For i = 1 To   47.         Text1.Text = Text1.Text &

37、0;("第" & i & "个文本框的内容:" & ActiveDocument.Shapes(i).TextFrame.TextRange.Text & vbCrLf)  48.     Next  49.       50.     ActiveDocument.

38、SaveAs "c:MyWord.doc" '保存最后生成的word文档  51.       52. Errhandler:  53.     Exit Sub  54. End Sub  55.   56. Private Sub Form_Unload(Cancel As Intege

39、r)  57.     On Error Resume Next  58.     WordApp.Quit  59.     Set WordApp = Nothing  60. End Sub          效果图如下: 

40、        3、在Word文档中设置Excel风格的页码vb view plaincopy1. '先引用Microsoft Word 11.0 Object Library  2. Option Explicit  3.   4. Dim WordApp As Word.Application '创建Word应用程序  5. D

41、im WordDoc As Word.Document    '创建Word文档对象  6.   7. Private Sub Command1_Click()  8.     Dim i As Long  9.     On Error GoTo Errhandler 

42、 10.     CommonDialog1.Filter = "Word(*.Doc)|*.Doc|AllFile(*.*)|*.*"  11.     CommonDialog1.FilterIndex = 1  12.     CommonDialog1.ShowOpen  13.     Set

43、60;WordApp = New Word.Application '实例化  14.     Set WordDoc = WordApp.Documents.Open(CommonDialog1.FileName) '选择并打开Word文件  15.     WordApp.Visible = True '显示 Office

44、0;Word 界面  16.     '或者Application.Visible = True  17.     WordApp.DisplayAlerts = False '不提示保存对话框  18.       19.     '设置Word文档第一页页码 &

45、#160;20.     Dim WordRange As Range  21.     Set WordRange = WordApp.ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range  22.            23. &#

46、160;   With WordRange  24.          .InsertAfter "第"  25.          .Font.Size = 14  26.         

47、; .Collapse Direction:=wdCollapseEnd  27.       28.          '插入页码域  29.          .Fields.Add Range:=WordRange, Type:=wdFieldEmpty, 

48、;Text:="PAGE  * Arabic ", PreserveFormatting:=True  30.          .Expand unit:=wdWord  31.          .InsertAfter "页 "  3

49、2.       33.          .InsertAfter "共"  34.          .Collapse Direction:=wdCollapseEnd  35.        36.          '插入页数域  37.          .Fields.Add Range:=

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论