【发布时间】:2013-10-29 08:54:48
【问题描述】:
我的 RTF 文档中有 2 个标题或标记。在我的示例中,我展示了一个句子,而实际上它将是多个句子或段落。我使用括号而不是小于和大于符号,因为它们在我的问题中消失了。我要做的就是用下面的句子“文本在此处”替换两个标记之间的文本,不带引号。
[EmbeddedReport]大量文字,数千个字符,多个段落[/EmbeddedReport]
我想将两个标记之间的所有文本替换为“文本在此处”。
它最终会看起来像这样......
"[EmbeddedReport]text goes here[/EmbeddedReport]"
我确实花了 2 天时间试图解决这个问题。任何帮助将不胜感激。
这是我尝试的最后一件事......
Sub RemoveReport()
Dim c As Range
Dim StartWord As String, EndWord As String
Selection.HomeKey Unit:=wdStory
StartWord = "<ImageTable>"
EndWord = "</ImageTable>"
Set c = ActiveDocument.Content
c.Find.ClearFormatting
c.Find.Replacement.ClearFormatting
With c.Find
.Text = StartWord & "*" & EndWord
' MsgBox (.Text)
.Replacement.Text = "<ImageTable>text goes here</ImageTable>"
.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
【问题讨论】:
-
有关您编写的代码问题的问题必须描述具体问题——并包括有效的代码来重现它。要求代码的问题必须表明对要解决的问题的最低理解。包括尝试的解决方案、为什么它们不起作用以及预期的结果。 我的解决方案是使用 Find 对象并将 MatchWildcards 属性设置为
True。试一试,如果您无法弄清楚,我们可以提供帮助。 -
@David Zemens...我是 StackOverFlow.com 的新手。带来不便敬请谅解。我已经编辑并添加了代码。它只是没有做任何事情。我确实从网上发现的类似问题中借用了代码。这不是我尝试过的唯一他们,但我尝试过的其他事情也没有成功。这段代码在我看来是最合乎逻辑的,但我肯定遗漏了一些东西。
-
这段代码与我的建议非常相似。给我一分钟,我会调整它。
-
好的,我的回答没有我想象的那么好,但希望它有所帮助。我不知道如何让它找到尖括号,所以我的解决方案是方括号。无论如何,它至少应该为您指明正确的方向;我希望它有帮助:)
-
您需要对多少份文件执行此操作?