【问题标题】:Remove all text between 2 headers Word 2010, using VBA使用 VBA 删除 2 个标题 Word 2010 之间的所有文本
【发布时间】: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 的新手。带来不便敬请谅解。我已经编辑并添加了代码。它只是没有做任何事情。我确实从网上发现的类似问题中借用了代码。这不是我尝试过的唯一他们,但我尝试过的其他事情也没有成功。这段代码在我看来是最合乎逻辑的,但我肯定遗漏了一些东西。
  • 这段代码与我的建议非常相似。给我一分钟,我会调整它。
  • 好的,我的回答没有我想象的那么好,但希望它有所帮助。我不知道如何让它找到尖括号,所以我的解决方案是方括号。无论如何,它至少应该为您指明正确的方向;我希望它有帮助:)
  • 您需要对多少份文件执行此操作?

标签: vba text replace ms-word


【解决方案1】:

Word VBA 不是我的专业领域,但它似乎与我几天前回答的一个问题相似。

事实证明,通配符匹配并没有达到我希望的效果,或者至少它不可靠。另外,我在使用尖括号时遇到了一些麻烦,所以这里使用方括号。我怀疑 word 将尖括号视为标记/语法,因此不会将它们解释为 Find 对象中的文本。可能有办法解决这个问题,但 Word VBA 不是我的专长。可能还有一个更优雅的解决方案,但同样,Word VBA 不是我的专长:)

试试这样的:

Option Explicit
Sub Test()
Dim doc As Document
Dim txtRange As Range
Dim startTag As String
Dim endTag As String
Dim s As Long
Dim e As Long

startTag = "[EmbeddedReport]"
endTag = "[/EmbeddedReport]"
Set doc = ActiveDocument
Set txtRange = doc.Content
'Find the opening tag
With txtRange.Find
    .Text = startTag
    .Forward = True
    .Execute
    If .Found Then
        s = txtRange.Start
    Else
        GoTo EarlyExit
    End If
End With
'Find the closing tag
Set txtRange = doc.Range(txtRange.End, doc.Content.End)
With txtRange.Find
    .Text = endTag
    .Forward = True
    .Execute
    If .Found Then
        e = txtRange.End
    Else
        GoTo EarlyExit
    End If
End With
Set txtRange = doc.Range(s, e)
txtRange.Text = startTag & "text goes here" & endTag

Exit Sub


EarlyExit:
MsgBox "Header not found in this document!", vbInformation

End Sub

一开始需要一些时间才能弄清楚,但学习浏览 VBA 的 object model reference 文档将使这些任务在未来更容易解决。

【讨论】:

  • David Zemens...这看起来很有希望。它似乎正在工作!我需要做进一步的测试。我可能会在今晚晚些时候或明天测试后发表评论。感谢您指引我朝着正确的方向前进。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多