荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: Jobs (温少), 信区: Visual
标  题: Microsoft Word 对象--运用Find和Replacement对象
发信站: BBS 荔园晨风站 (Sun Jan 16 18:14:45 2000), 转信





运用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"

.Execute

End With

Find对象包含与“查找”和“替换”对话框(在“编辑”菜单中)里的选项有关的
属性。用户既可对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

.Execute

If .Found = True Then .Parent.Bold = True

End With

以下的示例执行了和上述示例相同的操作,只是使用了Execute方法的参数。

Set myRange = ActiveDocument. Content

myRange. Find.  Execute FindText :="blue", Forward :=True

If myRange. Find. Found = True Then myRange. Bold = True

使用Replacement对象

Replacement对象表示查找-替换操作的替换条件。Replacement对象的属性和方法
对应于“查找”和“替换”对话框(在“编辑”菜单中)里的选项。

可以在Find对象中使用Replacement对象。以下的示例将所有出现单词“hi”的地
方替换为“hello”。当找到符合选择条件的文本时,选定内容将会改变,因为代
码从Selection对象返回Find对象。

With Selection. Find

.ClearFormatting

.Text = "hi"

.Replacement. ClearFormatting

.Replacement. Text = "hello"

.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 :="", ReplaceWith :="", Format :=True, Replace
:=wdReplaceAll

End With



--


   我想超越这平凡的生活,注定我暂时漂泊!

   我无法停止我内心的狂热,对未来的执着!

※ 来源:·BBS 荔园晨风站 bbs.szu.edu.cn·[FROM: 192.168.11.111]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店