首页 > 解决方案 > 如何在Word文档中快速查找和复制包含特定文本的段落?(或者,在Word文档中留下包含特定文本的段落?)

问题描述

我想在 Word VBA 中编写一个代码来快速查找和复制 Word 文档中包含特定文本的段落(或者,在 Word 文档中留下包含特定文本的段落),但有一行不完整。这是与我的问题非常相似的代码,但我想剪切/添加段落而不是删除它们,有没有人知道如何编写它?提前谢谢了!

https://www.datanumen.com/blogs/quickly-find-delete-paragraphs- contains-specific-texts-word-document/

标签: vbams-word

解决方案


我更改了一条线以剪切并将所选段落添加到尖峰(我不知道它存在!)

然而,是/否消息框的存在并不能真正让您查看该段落,但希望它对您有用。

Sub SpikeParagraphsContainingSpecificTexts()
  Dim strFindTexts As String
  Dim strButtonValue As String
  Dim nSplitItem As Long
  Dim objDoc As Document

  strFindTexts = InputBox("Enter text strings to be found here, and use commas to separate them: ", "Text strings to be found")
  nSplitItem = UBound(Split(strFindTexts, ","))
  With Selection
    .HomeKey Unit:=wdStory

    ' Find the entered texts one by one.
    For nSplitItem = 0 To nSplitItem
      With Selection.Find
        .ClearFormatting
        .Text = Split(strFindTexts, ",")(nSplitItem)
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchWholeWord = False
        .MatchCase = False
        .MatchSoundsLike = False
        .MatchWildcards = False
        .MatchAllWordForms = False
        .Execute
      End With

      Do While .Find.Found = True
        ' Expand the selection to the entire paragraph.
        Selection.Expand Unit:=wdParagraph
        strButtonValue = MsgBox("Click yes to cut/spike graf, no to do nothing", vbYesNo)
        If strButtonValue = vbYes Then
             NormalTemplate.AutoTextEntries.AppendToSpike Range:=Selection.Range

        End If
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    Next
  End With

  MsgBox ("Word has finished finding all entered text strings.")
  Set objDoc = Nothing
End Sub

希望能帮助到你。


推荐阅读