Microsoft Word 对象(15) If Selection. Information (wdWithInTable) = True ThenMsgBox "Columns = " & Selection. Information (wdMaximumNumberOfColumns) _& VBCr & "Rows = " & Selection. Information (wdMaximumNumberOfRows)End If要获得可以随Information属性一起使用的常量的完整列表和说明,请参阅“帮助”中的“Information属性”。判断文字是否被选定可以使用Type属性来设置或返回选定内容在文档中被指定的方式。例如,可以使用wdSelectionBlock常量来判断一个文字块是否被选定。如果选定内容是一个插入点,那么以下的示例选定包含该插入点的段落。If Selection. Type = wdSelectionIP ThenSelection. Paragraphs(1). Range. SelectEnd If运用Find和Replacement对象可以使用Find和Replacement对象来查找并且替换文档中文字的特定范围。Find对象可以在Selection对象或Range对象中使用(根据Find对象是从Selection对象还是从Range对象返回的,查找操作也略有不同)。使用Selection.Find如果是在Selection对象中使用Find对象,那么当找到符合选择条件的文本后选定内容将会改变。以下的示例选定下一次出现的单词“Hello”。如果在找到单词“Hello”之前已经到达了文档的末尾,那么停止搜索。With Selection. Find.Forward = True.Wrap = wdFindStop.Text = "Hello".ExecuteEnd WithFind对象包含与“查找”和“替换”对话框(在“编辑”菜单中)里的选项有关的属性。用户既可对Find对象的属性逐一进行设置,也可以随同Execute方法使用参数来进行设置,如果以下示例所示。Selection. Find. Execute FindText :="Hello", Forward :=True, Wrap :=wdFindStop使用Range.Find如果是在Range对象中使用Find对象,选定内容不会改变,但是当找到符合选择条件的文本时范围会被重新定义。以下的示例确定活动文档中第一次出现的单词“blue”的位置。如果查找操作成功,将重新定义范围并且设置单词“blue”的格式为粗体。With ActiveDocument. Content. Find.Text = "blue".Forward = True.ExecuteIf .Found = True Then .Parent.Bold = TrueEnd With以下的示例执行了和上述示例相同的操作,只是使用了Execute方法的参数。Set myRange = ActiveDocument. ContentmyRange. Find. Execute FindText :="blue", Forward :=TrueIf myRange. Find. Found = True Then myRange. Bold = True使用Replacement对象Replacement对象表示查找-替换操作的替换条件。Replacement对象的属性和方法对应于“查找”和“替换”对话框(在“编辑”菜单中)里的选项。可以在Find对象中使用Replacement对象。以下的示例将所有出现单词“hi”的地方替换为“hello”。当找到符合选择条件的文本时,选定内容将会改变,因为代码从Selection对象返回Find对象。