使用Content.Find对象搜索文本并恢复找到的文本 [英] Search text with Content.Find object and recover the text found
本文介绍了使用Content.Find对象搜索文本并恢复找到的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在整个MSWord文档中搜索带有通配符的文本,并恢复找到的字符串.
I want to search an entire MSWord document for a text with wildcards and recover the strings found.
类似的东西:
Sub Macro1()
Dim c As Range
Set c = ActiveDocument.Contentsdf
c.Find.ClearFormatting
c.Find.Replacement.ClearFormatting
With c.Find
.Text = "start[abcde]end"
.Replacement.Text = ""
.Forward = True
.wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False '
End With
c.Find.Execute
While c.Find.Found
Debug.Print c.Find.TextFound
c.Find.Execute
Wend
End Sub
,但方法c.Find.TextFound
不存在.有什么方法可以恢复文本而无需重复Selection.Text
?
but the method c.Find.TextFound
does not exists. Is there any way to recover the text without recurring to Selection.Text
?
推荐答案
尝试一下.
Sub Sample()
Dim c As Range
Dim StartWord As String, EndWord As String
StartWord = "Start": EndWord = "End"
Set c = ActiveDocument.Content
c.Find.ClearFormatting
c.Find.Replacement.ClearFormatting
With c.Find
.Text = StartWord & "*" & EndWord
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False '
End With
c.Find.Execute
While c.Find.Found
Debug.Print c.Text
'~~> I am assuming that the start word and the end word will only
'~~> be in the start and end respectively and not in the middle
Debug.Print Replace(Replace(c.Text, StartWord, ""), EndWord, "")
c.Find.Execute
Wend
End Sub
这篇关于使用Content.Find对象搜索文本并恢复找到的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文