【常用案例】Sub 打开word文档()Dim sfname As StringDim strfilt As StringDim strtitle As StringDim docapp As Object
strfilt = "word文档(*.doc;*.docx;*.docm),*.doc;*.docx;*.docm,"strtitle = "打开word文档"sfname = Application.GetOpenFilename(filefilter:=strfilt, Title:=strtitle)
If sfname = "False" Then Exit SubSet docapp = CreateObject("word.application")docapp.documents.Open sfname
docapp.Visible = TrueSet docapp = NothingEnd SubSub 获取word的数据()Dim sfname As StringDim strfilt As String
Dim strtitle As StringDim docapp As word.ApplicationDim pg As word.paragraphDim i As LongDim str1 As String
strfilt = "word文档(*.doc;*.docx;*.docm),*.doc;*.docx;*.docm,"strtitle = "打开word文档"sfname = Application.GetOpenFilename(filefilter:=strfilt, Title:=strtitle)
If sfname = "Fasle" Then Exit SubSet docapp = CreateObject("word.application")docapp.document.Open sfname
i = 1With docapp.activedocument For Each pg In .Paragraphs str1 = pg.Range.Text i = i + 1
ActiveSheet.Cells(i, 1) = str1 NextEnd Withdocapp.QuitSet docapp = NothingEnd Sub --------------------------------------Excel VBA 操作 Word涉及到的基础---------------------------
一、新建Word引用需要首先创建一个对 Word Application 对象的引用在VBA中,工具-引用,选取“MicroSoft Word 11.0 Object Library”根据自己的版本确定。
方法一、New Word.Application Dim Wordapp As Word.Application Set Wordapp = NewWord.Application Wordapp.Visible = True 可见
Wordapp.ScreenUpdating =False 屏幕刷新 Dim WordD As Word.Document 定义word类 Set WordD = Wordapp.Documents.Add 新建文档
Set WordD = Wordapp.Documents.open(filename) 打开文档 …… WordD.Close 关闭文档
Set WordD = Nothing WordApp.Quit 退出Word对象方法二、CreateObject Dim WordApp As Object
Set WordApp =CreateObject("Word.Application") 新建Word对象‘后续操作及退出一样……方法三、GetObject文件已打开的情况下,使用:SetWordD=GetObject(filename),可建立对文档的引用,如果文件没有打开,则还需要先用方法一或二来操作。
方法一:前期绑定,好处是在对象后输入句点可以给出快速提示,因为需要先引用对象,所以容易出现版本兼容问题方法二:后期绑定,没有提示,根据运行代码机器上对象的版本创建对象,兼容性好建议编写代码时使用前期绑定,发布时使用后期绑定。
二、认识Word的结构Excel有:Excel.Application ’Excel引用Excel.Application. Workbooks ’工作簿
Excel.Application. Workbooks.Sheets(1) ’工作表工作表下是Range,区域;Cells(row,col),单元格Word有:Word.Application
Word.Application.Documents ’文档文档下有字符、单词、句子、段落和节字符组成单词,单词组成句子,句子组成段落此外,每个文档具有一个包含一个或多个节的 Sections 集合,每一个节都有一个包含该节页眉和页脚的HeadersFooters 集合。
Characters(index)Words(index)Sentences(index)Paragraphs(index)Sections(index)前三个返回Range对象,能直接使用任何区域属性或方法修改该Range 对象。
后面二个返回该集合的单个成员,而不是 Range 对象,不能直接使用区域属性或方法如下使用例子:Words(1)后面直接.Copy,而.Paragraphs(1)和.Copy之间多了一个RangeSelection.Words(1).Copy
ActiveDocument.Paragraphs(1).Range.CopyCharacters:字符,ActiveDocument.Sentences(1).Characters.Count,第一句的字符总数。
Words:单词,对于英文来说是二个空格之间的字母加空格,对于中文,一个标点符号,一个汉字,或一个词Sentences:句子,以句号结束?感觉也不是一个很可靠的范围,感觉还是字符、段落、节,控制起来靠谱一些。
Range 对象表示文档中的一个连续范围,由一个起始字符位置和一个终止字符位置定义这个连续范围可以小到一个插入点,大到整个文档 Dim rngPa As Range Set rngPa =ActiveDocument. Characters (1) 第一个字符。
Set rngPa = ActiveDocument.Range( _ Start:=ActiveDocument.Paragraphs(1).Range.Start, _ End:=ActiveDocument.Paragraphs(4).Range.End) ‘第1段头到第4段尾
Set rngPa = ActiveDocument.Range(Start:=0,End:=10) ‘当前文档前10个字符 rngPa.Selectrange对象的赋值:(包括任意的对象,Set是对对象赋值的标准语句)
set a=b和变量的赋值:a=1不一样三、通过录制宏生成代码 有了对Word基本结构的认识,想操作这些对象应该使用什么方法、修改哪些属性?不知道就“录制宏”录制宏是我们认识未知对象的很好方法之一,通过宏录制器将操作译成Word的 Visual Basic 代码,再根据需要修改代码。
Word中录制与Excel不同的是,不能使用鼠标移动光标或选中一行,只能使用键盘来移动,或用Shift+方向键来选中以下几句话就是键盘的:上、下、左、右、Home、End、Shift+左选中5个字符、Shift+右选中5个字符。
Selection.MoveUp Unit:=wdLine, Count:=1 Selection.MoveDown Unit:=wdLine, Count:=1 Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=1 Selection.HomeKey Unit:=wdLine Selection.EndKey Unit:=wdLine
Selection.MoveLeft Unit:=wdCharacter, Count:=5, Extend:=wdExtend Selection.MoveRight Unit:=wdCharacter, Count:=5, Extend:=wdExtend
录制的宏使用 Selection 属性返回 Selection 对象即:录制的宏总是以Selection.开头的,如上要想使用这个Selection.,有时候我们就不得不先对特定的对象.Select,选中。
当然,Selection是一个Range,Characters、Words、Sentences也是Range,Paragraphs(n). Range, Sections(2). Range也是Range,那我们就可以将Selection.后面的语句嫁接到前面这些Range之后,就不用先.Select了。
录制的宏,通过嫁接或者复制到EXCEL VBA之后,有的运行会出错,此时应检查以下几项: 1、第一项中要求的“引用”建立了没? 2、利用VBA提醒功能检查语句VBA编辑过程中,通常在打下. 之后(需要前期绑定?),该对象所有的方法、属性都会显示出来,利用这个特点,可以检查录制的宏,能否嫁接到需要操作的对象之后。
提示里有就能,没有就不能 3、部分转换函数,Word VBA里有,Excel VBA里可能没有,遇到这样的情况,也可能出错 例: WordD.Paragraphs(1).Range.ParagraphFormat.FirstLineIndent = CentimetersToPoints(0.35) 。
Selection.ParagraphFormat.FirstLineIndent = CentimetersToPoints(0.35)是“首行缩进2字符”操作录制的,嫁接后,运行出错,按方法2检查:.ParagraphFormat.FirstLineIndent能用在Range之后,那么就是CentimetersToPoints(0.35)出问题了?这显然是一个函数,字面意思是“厘米转换成点数”,(录制时我明明输入的是“2字符”,录下来咋成了厘米为单位呢?)那是否是Excel VBA里没有这个函数呢?(我不知道),将=后面直接改为数字运行通过,最后试下来=20大约相当于5号字的“首行缩进2字符”。
四、Word vba常用语句100句1、系统参数(01) Application.ActivePrinter ‘获取当前打印机 (02) Application.Height 当前应用程序文档的高度
(03) Application.Width ‘当前应用程序文档的宽度 (04) Application.Build ‘获取Word版本号和编译序号 (05) Application.Caption ‘当前应用程序名
(06) Application.DefaultSaveFormat 返回空字符串,表示Word文档 (07) Application.DisplayRecentFiles 返回是否显示最近使用的文档的状态
(08) Application.Documents.Count 返回当前打开的文档数 (09) Application.FontNames.Count ‘返回当前可用的字体数
(10) Application.Left ‘返回当前文档的水平位置 (11) Application.MacroContainer.FullName 返回当前文档名,包括所在路径
Application.MacroContainer.pach 返回当前文档路径 Application.ActiveDocument.Path ‘获得文件的相对路径
(12) Application.NormalTemplate.FullName 返回文档标准模板名称及所在位置 (13) Application.RecentFiles.Count 返回最近打开的文档数目
(14) Application.System.CountryRegion 返回应用程序所在的地区代码 (15) Application.System.FreeDiskSpace ‘返回应用程序所在磁盘可用空间
(16) Application.System.HorizontalResolution 返回显示器的水平分辨率 (17) Application.System.VerticalResolution 返回显示器的垂直分辨率
(18) Application.System.LanguageDesignation 返回系统所使用的语言 (19) Application.System.MathCoprocessorInstalled ‘返回系统是否安装了数学协处理器
(20) Application.System.OperatingSystem ‘返回当前操作系统名 (21) Application.System.ProcessorType 返回计算机处理器名
(22) Application.System.Version ‘返回操作系统的版本号 (23) Application.Templates.Count 返回应用程序所使用的模板数
(24) Application.UserName 返回应用程序用户名 (25) Application.Version ‘返回应用程序的版本号 2、Documents/Document对象
(26) ActiveDocument.AttachedTemplate.FullName 返回当前文档采用的模板名及模板所在位置 (27) ActiveDocument.Bookmarks.Count 返回当前文档中的书签数
(28) ActiveDocument.Characters.Count 返回当前文档的字符数 (29) ActiveDocument.CodeName ‘返回当前文档的代码名称
(30) ActiveDocument.Comments.Count ‘ 返回当前文档中的评论数 (31) ActiveDocument.Endnotes.Count 返回当前文档中的尾注数
(32) ActiveDocument.Fields.Count 返回当前文档中的域数目 (33) ActiveDocument.Footnotes.Count ‘返回当前文档中的脚注数
(34) ActiveDocument.FullName 返回当前文档的全名及所在位置 (35) ActiveDocument.HasPassword 当前文档是否有密码保护 (36) ActiveDocument.Hyperlinks.Count 返回当前文档中的链接数
(37) ActiveDocument.Indexes.Count 返回当前文档中的索引数 (38) ActiveDocument.ListParagraphs.Count 返回当前文档中项目编号或项目符号数
(39) ActiveDocument.ListTemplates.Count 返回当前文档中使用的列表模板数 (40) ActiveDocument.Paragraphs.Count 返回当前文档中的段落数
(41) ActiveDocument.Password=XXX 设置打开文件使用的密码 (42) ActiveDocument.ReadOnly 获取当前文档是否为只读属性
(43) ActiveDocument.Saved 当前文档是否被保存 (44) ActiveDocument.Sections.Count 当前文档中的节数 (45) ActiveDocument.Sentences.Count ‘当前文档中的语句数
(46) ActiveDocument.Shapes.Count 当前文档中的形状数 ,图形? (47) ActiveDocument.Styles.Count 当前文档中的样式数
(48) ActiveDocument.Tables.Count ‘当前文档中的表格数 (49) ActiveDocument.TablesOfAuthorities.Count ‘返回当前文档中的引文目录数
(50) ActiveDocument.TablesOfAuthoritiesCategories.Count ‘返回当前文档中引文目录类别数 (51) ActiveDocument.TablesOfContents.Count ‘返回当前文档中的目录数
(52) ActiveDocument.TablesOfFigures.Count 返回当前文档中的图表目录数 3、Paragraphs/Paragraph对象 (53) Selection.Paragraphs.Count 返回所选区域的段落数
(54) Selection.Paragraphs.First 返回所选区域中的第一段 (55) ActiveDocument.Paragraphs(1).LeftIndent 返回当前文档中第一段的左缩进值
(56) ActiveDocument.Paragraphs(1).LineSpacing 返回当前文档中第一段的行距 (57) ActiveDocument.Paragraphs(1).OutlineLevel ‘返回或设置当前文档中第一段的大纲级别
.OutlineLevel = wdOutlineLevel2 ‘2级 .OutlineLevel = wdOutlineLevel3 ‘3级 (58) ActiveDocument.Paragraphs(1).RightIndent ‘返回当前文档中第一段的右缩进量
(59) ActiveDocument.Paragraphs(1).SpaceBefore 返回当前文档中第一段的段前间距 (60) ActiveDocument.Paragraphs(1).SpaceAfter ‘返回当前文档中第一段的段后间距
(61) ActiveDocument.Paragraphs(1).Range.Text 返回当前文档中第一段的内容 (62) ActiveDocument.Paragraphs(1).Range.Style.NameLocal 返回当前文档中第一段应用的样式名
(63) ActiveDocument.Paragraphs(1).Range.Style.Description 返回当前文档中第一段所应用样式的详细描述 (64) ActiveDocument.Paragraphs(1).Range.Style.Font.Name 返回当前文档中第一段所应用样式的字体名
(65) ActiveDocument.Paragraphs(1).Range.Style.Font.NameFarEast 返回或设置一种东亚字体名 (66) ActiveDocument.Paragraphs(1).Range.Style.Font.Size 返回或设置当前文档中第一段所应用样式的字体大小
(67) ActiveDocument.Paragraphs(1).Range.Style.Font.Spacing 返回或设置字符间距 (68) Selection.Words.Count 所选区域的字数 Sentences对象
(69) Selection.Sentences.Item(1) 所选区域中的第一句的内容 Words对象 (71) ActiveDocument.Words(1).Select 选择当前文档中的第一个词
(72) ActiveDocument.Range.Words(1).InsertAfter "我爱你!" 在当前文档中的第一个词后插入“我爱你” 4、Characters对象 (73) Selection.Characters.Count 当前文档中所选区域的字符数
(74) ActiveDocument.Paragraphs(1).Range.InsertParagraphAfter在当前文档的第一段之后插入一个新段落 5、Sections/Section对象
(75) ActiveDocument.Sections.First 当前文档的第一节 (76) ActiveDocument.Sections.First.PageSetup.BottomMargin 当前文档第一节所在页的底边距
(77) ActiveDocument.Sections.First.PageSetup.LeftMargin 当前文档第一节所在页的左边距 (78) ActiveDocument.Sections.First.PageSetup.RightMargin 当前文档第一节所在页的右边距
(79) ActiveDocument.Sections.First.PageSetup.TopMargin 当前文档第一节所在页的顶边距 (80) ActiveDocument.Sections.First.PageSetup.PaperSize 返回或设置当前文档第一节所在页的大小
(81) ActiveDocument.Sections.First.PageSetup.PageHeight 返回或设置当前文档第一节所在页的高度 (82) ActiveDocument.Sections.First.PageSetup.PageWidth 返回或设置当前文档第一节所在页的宽度
(83) ActiveDocument.Sections.Add Range:=myRange 在当前文档中添加新节 (84) ActiveDocument.Sections.Item(2) 当前文档中的第二节
(85) ActiveDocument.Sections.Last.Range.InsertAfter "文档结束!" 在当前文档中最后一节的结尾添加文字“文档结束!” 6、Range对象 (86) ActiveDocument.Range(Start:=0, End:=10) 表示当前文档前10个字符所组成的一个Range对象
(87)Set myRange = ActiveDocument.Range(Start:=ActiveDocument.Paragraphs(2).Range.Start, _ End:=ActiveDocument.Paragraphs(4).Range.End) 将当前文档第2段至第4段设置为一个Range对象
(88) ActiveDocument.Paragraphs(1).Range.Copy 复制当前文档中的第一段 (89) Selection.Copy Documents.Add.Content.Paste 复制所选内容到新文档中
(90) ActiveDocument.Bookmarks("Book1").Copy Name:="Book2" 将Book2书签复制Book1书签标记的位置 (91) Selection.GoTo What:=wdGoToLine, Which:=wdGoToAbsolute, Count:=4 将所选内容移至文档中的第4行
(92) Selection.GoTo What:=wdGoToTable, Which:=wdGoToNext 将所选内容移至下一个表格的第1个单元格 (93) Selection.Range.AutoFormat 为所选内容套用格式
(94) ActiveDocument.Content.Font.Name = "Arial" 将当前文档的字体设置为斜体 (95) ActiveDocument.Content.Select Selection.Delete 将当前文档中的内容删除其它
(96) Documents.Add 添加一个新文档 (97) Set myTable = ActiveDocument.Tables.Add(Selection.Range, 2, 2) 在当前文档所选区域添加一个2行2列的表格
7、文件读写(98) Open "C:\my.txt" For Input As #1 打开一个用于输入的文件并令其编号为1 (99) Line Input #1, TextLine 读取被打开用于输入且编号为1的文件
(100) Close #1 关闭编号为1的文件
亲爱的读者们,感谢您花时间阅读本文。如果您对本文有任何疑问或建议,请随时联系我。我非常乐意与您交流。
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。