vb控制word实例2.doc_第1页
vb控制word实例2.doc_第2页
vb控制word实例2.doc_第3页
vb控制word实例2.doc_第4页
vb控制word实例2.doc_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

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

文档简介

1、返回Word文档的段落文字,控制分页,设置页眉和页脚vbview plaincopy1. 先引用MicrosoftWord11.0ObjectLibrary2. OptionExplicit3. 4. DimWordAppAsWord.Application创建Word应用程序5. 6. PrivateSubCommand1_Click()7. DimiAsLong8. OnErrorGoToErrhandler9. CommonDialog1.Filter=Word(*.Doc)|*.Doc|AllFile(*.*)|*.*10. CommonDialog1.FilterIndex=111. CommonDialog1.ShowOpen12. SetWordApp=NewWord.Application实例化13. WordApp.Documents.OpenCommonDialog1.FileName打开Word文件14. WordApp.Visible=True显示OfficeWord界面15. 或者Application.Visible=True16. WordApp.DisplayAlerts=False不提示保存对话框17. 18. 返回段落文字,返回的段落文字在文本框控件中19. Text1.Text=20. Fori=1ToActiveDocument.Paragraphs.Count21. Text1.Text=Text1.Text&(ActiveDocument.Paragraphs(i).Range.Text&vbCrLf&vbCrLf)22. Next23. 24. 控制分页25. WordApp.Selection.EndKeyunit:=wdStory将光标移到文档末尾26. WordApp.Selection.InsertBreakwdPageBreak在文档末尾插入一页27. 28. 设置图片格式的页眉29. IfActiveWindow.View.SplitSpecialwdPaneNoneThen30. ActiveWindow.Panes(2).Close31. EndIf32. IfActiveWindow.ActivePane.View.Type=wdNormalViewOrActiveWindow.ActivePane.View.Type=wdOutlineViewThen33. ActiveWindow.ActivePane.View.Type=wdPrintView34. EndIf35. ActiveWindow.ActivePane.View.SeekView=wdSeekCurrentPageHeader36. Selection.InlineShapes.AddPictureFileName:=F:资料MyPictures2013年元旦.gif,LinkToFile:=False,SaveWithDocument:=True加载一图片文件作为页眉37. Selection.ParagraphFormat.Alignment=wdAlignParagraphLeft38. ActiveWindow.ActivePane.View.SeekView=wdSeekMainDocument39. 40. 设置文本格式的页眉41. IfActiveWindow.View.SplitSpecialwdPaneNoneThen42. ActiveWindow.Panes(2).Close43. EndIf44. IfActiveWindow.ActivePane.View.Type=wdNormalViewOrActiveWindow.ActivePane.View.Type=wdOutlineViewThen45. ActiveWindow.ActivePane.View.Type=wdPrintView46. EndIf47. ActiveWindow.ActivePane.View.SeekView=wdSeekCurrentPageHeader48. Selection.TypeTextText:=办公室常用工具49. ActiveWindow.ActivePane.View.SeekView=wdSeekMainDocument50. 51. 隐藏页眉的横线52. WordApp.ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Borders(wdBorderBottom).Visible=False53. 54. 取得页眉的内容55. Debug.PrintWordApp.ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Range.Text获取WORD第一节的页眉的文字内容56. 57. 58. 设置页脚59. IfActiveWindow.View.SplitSpecialwdPaneNoneThen60. ActiveWindow.Panes(2).Close61. EndIf62. IfActiveWindow.ActivePane.View.Type=wdNormalViewOrActiveWindow.ActivePane.View.Type=wdOutlineViewThen63. ActiveWindow.ActivePane.View.Type=wdPrintView64. EndIf65. ActiveWindow.ActivePane.View.SeekView=wdSeekCurrentPageHeader66. IfSelection.HeaderFooter.IsHeader=TrueThen67. ActiveWindow.ActivePane.View.SeekView=wdSeekCurrentPageFooter68. Else69. ActiveWindow.ActivePane.View.SeekView=wdSeekCurrentPageHeader70. EndIf71. Selection.TypeTextText:=2013年设置页脚72. Selection.Fields.AddRange:=Selection.Range,Type:=wdFieldNumPages73. ActiveWindow.ActivePane.View.SeekView=wdSeekMainDocument74. 75. ActiveDocument.SaveAsc:MyWord.doc保存最后生成的word文档76. 77. Errhandler:78. ExitSub79. EndSub80. 81. PrivateSubForm_Unload(CancelAsInteger)82. OnErrorResumeNext83. WordApp.Quit84. SetWordApp=Nothing85. EndSub 效果图如下: 2、控制Word文档中的文本框对象vbview plaincopy1. 先引用MicrosoftWord11.0ObjectLibrary2. OptionExplicit3. 4. DimWordAppAsWord.Application创建Word应用程序5. 6. PrivateSubCommand1_Click()7. OnErrorGoToErrhandler8. CommonDialog1.Filter=MSOfficeWord(*.Doc)|*.Doc|AllFile(*.*)|*.*9. CommonDialog1.FilterIndex=110. CommonDialog1.ShowOpen11. SetWordApp=NewWord.Application实例化12. WordApp.Documents.OpenCommonDialog1.FileName打开Word文件13. IfDocuments.Count=1Then14. Text1.Text=打开的Word文件是:&ActiveDocument.Name&vbCrLf&vbCrLf15. EndIf16. WordApp.Visible=True显示OfficeWord界面17. 或者Application.Visible=True18. WordApp.DisplayAlerts=False不提示保存对话框19. 20. WordApp.Selection.EndKeyunit:=wdStory将光标移到文档末尾21. WordApp.Selection.Font.Bold=122. WordApp.Selection.Font.Name=黑体23. WordApp.Selection.Font.Size=1824. WordApp.Selection.TypeTextText:=在Word文件中插入文本框对象25. WordApp.Selection.ParagraphFormat.Alignment=Word.WdParagraphAlignment.wdAlignParagraphCenter居中显示26. 27. 创建文本框对象,座标(100,100),宽度200,高度20028. WithActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal,100,100,400,300).Fill29. .Transparency=1设置透明色30. .ForeColor=vbRed设置前景颜色31. .UserPicture(F:资料MyPictures758254_960x1000_0.jpg)设置文本框对象的背景图片32. EndWith33. ActiveDocument.Shapes(1).TextFrame.TextRange.Text=这是一个美女给文本框赋值34. ActiveDocument.Shapes(1).Line.Transparency=1设置透明边框线条35. 36. 再创建一个透明背景的文本框对象37. WithActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal,100,400,400,300).Fill38. .Transparency=1设置透明色背景39. .ForeColor=vbRed设置前景颜色40. EndWith41. ActiveDocument.Shapes(2).TextFrame.TextRange.Text=这是一个透明背景的文本框给文本框赋值42. ActiveDocument.Shapes(2).Line.Transparency=1设置透明边框线条43. 44. 下面是获取文本框对象的内容45. DimiAsLong46. Fori=1ToActiveDocument.Shapes.Count47. Text1.Text=Text1.Text&(第&i&个文本框的内容:&ActiveDocument.Shapes(i).TextFrame.TextRange.Text&vbCrLf)48. Next49. 50. ActiveDocument.SaveAsc:MyWord.doc保存最后生成的word文档51. 52. Errhandler:53. ExitSub54. EndSub55. 56. PrivateSubForm_Unload(CancelAsInteger)57. OnErrorResumeNext58. WordApp.Quit59. SetWordApp=Nothing60. EndSub 效果图如下: 3、在Word文档中设置Excel风格的页码vbview plaincopy1. 先引用MicrosoftWord11.0ObjectLibrary2. OptionExplicit3. 4. DimWordAppAsWord.Application创建Word应用程序5. DimWordDocAsWord.Document创建Word文档对象6. 7. PrivateSubCommand1_Click()8. DimiAsLong9. OnErrorGoToErrhandler10. CommonDialog1.Filter=Word(*.Doc)|*.Doc|AllFile(*.*)|*.*11. CommonDialog1.FilterIndex=112. CommonDialog1.ShowOpen13. SetWordApp=NewWord.Application实例化14. SetWordDoc=WordApp.Documents.Open(CommonDialog1.FileName)选择并打开Word文件15. WordApp.Visible=True显示OfficeWord界面16. 或者Application.Visible=True17. WordApp.DisplayAlerts=False不提示保存对话框18. 19. 设置Word文档第一页页码20. DimWordRangeAsRange21. SetWordRange=WordApp.ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range22. 23. WithWordRange24. .InsertAfter第25. .Font.Size=1426. .CollapseDirection:=wdCollapseEnd27. 28. 插入页码域29. .Fields.AddRange:=WordRange,Type:=wdFieldEmpty,Text:=PAGE*Arabic,PreserveFormatting:=True30. .Expandunit:=wdWord31. .InsertAfter页32

温馨提示

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

评论

0/150

提交评论