gpt4 book ai didi

vba - 使用 Content.Find 对象搜索文本并恢复找到的文本

转载 作者:行者123 更新时间:2023-12-04 05:39:16 25 4
gpt4 key购买 nike

我想在整个 MSWord 文档中搜索带有通配符的文本并恢复找到的字符串。

类似的东西:

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

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 - 使用 Content.Find 对象搜索文本并恢复找到的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11480421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com