【问题标题】:Delete Section based on user input根据用户输入删除部分
【发布时间】:2015-08-14 08:05:39
【问题描述】:

我想根据用户输入删除 word 文档的整个部分/段落。

文档有“1.1”、“1.1.1”和“1.2”等部分。用户输入“1.1.1”,宏会删除该部分。是否可以让宏搜索“1.1.1”并删除从 1.1.1 开始的所有内容,但不包括 1.2?

我是从这个开始的,但需要创建一个起点和终点才能删除。

Sub DeleteParagraphContainingString()

Dim search As String
search = "1.1.1"

Dim para As Paragraph
For Each para In ActiveDocument.Paragraphs

    Dim txt As String
    txt = para.Range.Text

    If InStr(LCase(txt), search) Then
        para.Range.Delete
    End If

Next

End Sub

【问题讨论】:

    标签: vba ms-word


    【解决方案1】:

    您可以使用 Word 的Find 功能,然后选择整个部分并将其删除。我给你一些代码 sn-ps,你可以将它们编织在一起,应用检查等等:

    ActiveDocument.Range(0, 0).Select               ' go to beginning of document
    
    Selection.Find.ClearFormatting
    With Selection.Find
        .text = yourFindString
        .Replacement.text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = True
        .MatchWholeWord = True
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    
    While (Selection.Find.Execute)
        Selection.Bookmarks("\HeadingLevel").Select         ' select whole Section
        title = Selection.Paragraphs(1).Range.text          ' get Section Title (text of first paragraph)
        Selection.Delete
    Wend
    

    我希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 2015-07-06
      • 2014-02-06
      • 1970-01-01
      • 2017-02-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多