首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

VBA专题06-2:利用Excel中的数据自动化构建Word文档—熟悉Word VBA

VBA是一种通用编程语言,适用于任何内置有VBA的应用程序,因此Word VBA与Excel VBA的语法一样,只是处理的对象模型不同。下面,我们通过一些示例语句及其作用描述来熟悉Word VBA。(注:这些语句来源于作者早期发表的VBA语句集,长期关注作者的朋友可能会有印象)

Application对象

(1) Application.ActivePrinter ‘获取当前打印机

(2) Application.Height '当前应用程序文档的高度

(3) Application.Width ‘当前应用程序文档的宽度

(4) Application.Build ‘获取Word版本号和编译序号

(5)Application.Caption ‘当前应用程序名

(6)Application.DefaultSaveFormat '返回空字符串,表示Word文档

(7)Application.DisplayRecentFiles '返回是否显示最近使用的文档的状态

(8)Application.Documents.Count '返回当前打开的文档数

(9)Application.FontNames.Count ‘返回当前可用的字体数

(10) Application.Left‘返回当前文档的水平位置

(11) Application.MacroContainer.FullName '返回当前文档名,包括所在路径

(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 ‘返回应用程序的版本号

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'返回当前文档中的图表目录数

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 ‘返回或设置当前文档中第一段的大纲级别

(58)ActiveDocument.Paragraphs(1).RightIndent ‘返回当前文档中第一段的右缩进量

(59) ActiveDocument.Paragraphs(1).SpaceBefore'返回当前文档中第一段的段前间距

(60)ActiveDocument.Paragraphs(1).SpaceAfter ‘返回当前文档中第一段的段后间距

(61) ActiveDocument.Paragraphs(1).Range.Text'返回当前文档中第一段的内容

代码语言:javascript
复制
(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"我爱你!" '在当前文档中的第一个词后插入“我爱你”

Characters对象

(73)Selection.Characters.Count '当前文档中所选区域的字符数

(74)ActiveDocument.Paragraphs(1).Range.InsertParagraphAfter'在当前文档的第一段之后插入一个新段落

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.AddRange:=myRange '在当前文档中添加新节

(84)ActiveDocument.Sections.Item(2) '当前文档中的第二节

(85) ActiveDocument.Sections.Last.Range.InsertAfter"文档结束!" '在当前文档中最后一节的结尾添加文字“文档结束!”

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.GoToWhat:=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)SetmyTable = ActiveDocument.Tables.Add(Selection.Range, 2, 2) '在当前文档所选区域添加一个2行2列的表格

(98)Open"C:\my.txt" For Input As #1 '打开一个用于输入的文件并令其编号为1

(99)LineInput #1, TextLine '读取被打开用于输入且编号为1的文件

(100)Close#1 '关闭编号为1的文件

示例:给文档中的代码添加行号

下面的代码会给文档中所选择的代码添加行号:

Sub AddRowNum()

'变量声明

Dim lRowNum As Long

Dim strRowNum As String

Dim rng As Range

Dim i As Long

'所选择的文字区域

Set rng = Selection.Range

'遍历所选区域的每段

For lRowNum = 1 To rng.Paragraphs.Count

'行号数值转换成文本

strRowNum = str(lRowNum)

'去除前后空格

strRowNum = Trim(strRowNum)

'不足三位在前面补0

For i = 1 To (3 - Len(strRowNum))

strRowNum = "0" +strRowNum

Next i

'在后面添加空格

strRowNum = strRowNum + " "

'在每段的前面插入行号

rng.Paragraphs(lRowNum).Range.InsertBefore (strRowNum)

Next lRowNum

End Sub

如果选择上面的代码,然后运行上面的过程,结果如下图5所示。

图5:给代码添加行号

下一篇
举报
领券