活动文档中第一次出现的单词“blue”的位置。如果查找操作成功,将重新定义范围并且设置单词“blue”的格式为粗体。
With ActiveDocument. Content. Find .Text = \.Forward = True .Execute
If .Found = True Then .Parent.Bold = True End With
以下的示例执行了和上述示例相同的操作,只是使用了Execute方法的参数。
Set myRange = ActiveDocument. Content
myRange. Find. Execute FindText :=\If myRange. Find. Found = True Then myRange. Bold = True 使用Replacement对象
Replacement对象表示查找-替换操作的替换条件。Replacement对象的属性和方法对应于“查找”和“替换”对话框(在“编辑”菜单中)里的选项。
可以在Find对象中使用Replacement对象。以下的示例将所有出现单词“hi”的地方替换为“hello”。当找到符合选择条件的文本时,选定内容将会改变,因为代码从Selection对象返回Find对象。
With Selection. Find
.ClearFormatting .Text = \
.Replacement. ClearFormatting .Replacement. Text = \
.Execute Replace :=wdReplaceAll, Forward :=True, Wrap :=wdFindContinue
End With
以下的示例去除了活动文档中所有的粗体格式。Bold属性对Find对象为True(真),对Replacement属性为False(假)。要查找和替换格式,需设置查找和替换文本为空字符串(“”),并且设置Execute方法的Format参数为True(真)。选定内容保持不变,因为代码在Range对象中返回Find对象(Content属性返回一个Range对象)。
With ActiveDocument. Content. Find .ClearFormatting .Font. Bold = True With .Replacement .ClearFormatting .Font. Bold = False End With
.Execute FindText :=\Replace :=wdReplaceAll
End With
运用Table、Column、Row和Cell对象
Word对象模型包含了表格对象,也包含了表格中各种不同元素的对象。可以随同Document对象、Range对象、Selection对象使用Table属性来返回Table集合。Table(index)返回了一个单独的Table对象,在这里index是表格的索引号。索引号代表在选定内容、范围或文档中表格的位置。以下的示例将选定内容中的第一个表格转换为文本。
If Selection. Tables. Count >= 1 Then
Selection.Tables(1).ConvertToText Separator:=wdSeparateByTabs
End If
可以随同Column对象、 Range 对象、Row对象或 Selection对象使用Cells属性来返回Cells集合。用户可以通过使用Table对象的Cell方法或是索引Cells集合来获得一个Cell对象。以下的两条语句都能够设置myCell为一个Cell 对象,该Cell对象代表活动文档中表格一的第一个单元格。
Set myCell = ActiveDocument. Tables(1). Cell(Row :=1, Column :=1)
Set myCell = ActiveDocument. Tables(1). Columns(1). Cells(1)
注释 要在一个表格的一个单元格中插入文字,可以使用Text属性、InsertAfter方法或者随Range对象使用InsertBefore方法。
可以随Cell对象使用Range属性来返回一个Range对象。以下的示例在表格一的每一个单元格中插入连续的单元格序号。
i = 1
For Each c In ActiveDocument. Tables(1). Range. Cells c. Range. InsertBefore Text :=\i = i + 1 Next c
可以随同Table对象、Range对象或Selection对象使用Column属性来返回Columns集合。Columns(index)返回了一个单独的Column对象,在这里index是索引号。以下的示例选择了表格一的第一列。
ActiveDocument. Tables(1). Columns(1). Select
可以随同Table对象、Range对象或Selection对象使用Row属性来返回Rows集合。Rows(index)返回了一个单独的Row对象,在这里index是索引号。以下的示例给表格一的第一行加底纹。
ActiveDocument. Tables(1). Rows(1). Shading. Texture = wdTexture10Percent
修改图形表格的行与列
当用户试图使用在一个图形表格(或者任何表格,其中有两个以上相邻的单元格被合并,但是行与列没有统一)中某一单独的行或列时,就可能出现一个运行时错误。如果活动文档中的第一个表格的每列含有不一致的行号,以下的示例就会出错。
ActiveDocument. Tables(1). Rows(1). Borders.Enable =
False
用户可以首先使用SelectColumn或SelectRow方法来选定某个特定行或列的单元格,以此避免这样的错误。当用户选好了行或列,再随Selection对象使用Cells属性。以下的示例选定了活动文档中表格一的第一行。该示例使用Cells属性返回所选的单元格(在第一行中所有单元格),以便可以删除边框。
If ActiveDocument. Tables(1). Uniform = False ActiveDocument. Tables(1). Cell(1, 1). Select With Selection .SelectRow
.Cells. Borders. Enable = False End With End If
以下的示例选定了表格一的第一列。该示例使用了一个For Each...Next循环来向选定内容(第一列的所有单元格)中的每个单元格添加文字。
If ActiveDocument. Tables(1). Uniform = False ActiveDocument. Tables(1). Cell(1, 1). Select Selection. SelectColumn i = 1
For Each oCell In Selection. Cells oCell.Range. Text = \