使用VBA在Word中搜索带有Content.Find对象的文本,并恢复找到的文本 [英] Search text with Content.Find object in Word with VBA and recover the text found
本文介绍了使用VBA在Word中搜索带有Content.Find对象的文本,并恢复找到的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
类似于:
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
?
解决方案
Sub Sample()
Dim c As Range
Dim StartWord As String ,EndWord As String
$ b $ StartWord =Start:EndWord =End
Set c = ActiveDocument.Content
c.Find.ClearFormatting
c.Find.Replacement.ClearFormatting
用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
而c.Find.Found
Debug.Print c.Text
'~~>我假设开始词和结束词将只有
'~~>分别在开始和结束,不在中间
Debug.Print替换(替换(c.Text,StartWord,),EndWord,)
c.Find.Execute
Wend
End Sub
I want to search an entire MSWord document for a text with wildcards and recover the strings found.
something like that:
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
but the method c.Find.TextFound
does not exists. Is there any way to recover the text without recurring to Selection.Text
?
解决方案
Try this.
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
这篇关于使用VBA在Word中搜索带有Content.Find对象的文本,并恢复找到的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文