【问题标题】:Attempting to delete a page in Microsoft Word (VBA) that contains specific text in a textbox尝试在 Microsoft Word (VBA) 中删除包含文本框中特定文本的页面
【发布时间】:2021-02-01 03:45:28
【问题描述】:

我一直在使用 VBA 为 Microsoft Word 开发一个宏,它应该在文本框(形状)中找到某些文本,然后删除包含该文本的文本框所在的页面。这是我的宏:

Sub DeletePagesWithSpecificTextBoxText()
    Dim shp As Shape
    Dim FoundOnPageNumber As Integer
    
    For Each shp In ActiveDocument.Shapes
        If shp.Type = msoTextBox Then
            shp.Select
            With Selection.Find
                .ClearFormatting
                .Text = "delete this page"
                .Forward = True
                .Wrap = wdFindContinue
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = False
                .MatchSoundsLike = False
                .MatchAllWordForms = False
                .Execute
                If .Found Then
                    FoundOnPageNumber = Selection.ShapeRange.Anchor.Information(wdActiveEndPageNumber)
                    Selection.GoTo wdGoToPage, wdGoToAbsolute, FoundOnPageNumber
                    ActiveDocument.Bookmarks("\Page").Range.Delete
                End If
            End With
        End If
    Next
End Sub

为了测试这个宏 - 我有一个基本的十页文档,我在其中按从 1 到 10 的顺序标记了每个页面。每个页面都有一个 TextBox,其中包含文本“删除此页面”(这是文本宏正在寻找)。

运行宏后,文档包含所有偶数页(即 2、4、6、8 和 10),但奇数页(1、3、5、7 和 9)已被删除。

谁能提供任何关于为什么它只会删除奇数页的见解?

编辑: 用户 ma​​cropod 极大地帮助了它正常工作。完整的工作宏如下所示:

Sub DeletePagesWithSpecificTextBoxText()

    Dim TextFoundOnThisPage As Integer
    Dim DeleteLastPage As Boolean

    Application.ScreenUpdating = False
    
    Dim s As Long
    With ActiveDocument
        For s = .Shapes.Count To 1 Step -1
            With .Shapes(s)
                If .Type = msoTextBox Then
                    If InStr(.TextFrame.TextRange.Text, "delete this page") > 0 Then
                        TextFoundOnThisPage = .Anchor.Information(wdActiveEndPageNumber)
                        
                        If TextFoundOnThisPage = ActiveDocument.ActiveWindow.Panes(1).Pages.Count And DeleteLastPage = False Then
                            DeleteLastPage = True
                        End If
                        
                        .Delete
                        Selection.GoTo wdGoToPage, wdGoToAbsolute, TextFoundOnThisPage
                        ActiveDocument.Bookmarks("\Page").Range.Delete
                    End If
                End If
            End With
        Next
    End With
    
    If DeleteLastPage Then
        Selection.GoTo wdGoToPage, wdGoToAbsolute, ActiveDocument.ActiveWindow.Panes(1).Pages.Count
        Selection.TypeBackspace
        Selection.TypeBackspace
    End If
    
    Application.ScreenUpdating = True
    
End Sub

DeleteLastPage 标志是必需的,以确保文档末尾没有空白页如果在最后一页上找到了一个文本框。

【问题讨论】:

    标签: vba ms-word textbox shapes


    【解决方案1】:

    你应该向后循环形状;否则循环会在删除后跳过下一个形状。也不需要选择任何东西:

    Sub Demo()
    Application.ScreenUpdating = False
    Dim s As Long
    With ActiveDocument
      For s = .Shapes.Count To 1 Step -1
        With .Shapes(s)
          If .Type = msoTextBox Then
            If InStr(.TextFrame.TextRange.Text, "delete this page") > 0 Then
              .Anchor.Bookmarks("\Page").Range.Delete
            End If
          End If
        End With
      Next
    End With
    Application.ScreenUpdating = True
    End Sub
    

    【讨论】:

    • 非常感谢macropod的帮助。尽管我今天早上早些时候遇到了一个错误,但向后迭代形状是有意义的:运行时错误“5941”集合的请求成员不存在。我发现这个错误出现了因为我选择了文本框,我需要取消选择它才能使这一行工作:.Anchor.Bookmarks("\Page").Range.Delete
    • 很可能您在同一页面上有两个或多个形状。您可以获取页面的形状计数,以便在删除页面时正确减少 s 值,也可以简单地使用 On Error Resume Next。
    • 再次非常感谢。我的测试文档每页只包含一个文本框。我会做更多的实验,看看我是否可以让你的宏工作,并将我的结果发回这里:)
    • 这不仅仅是您的文档在一个页面上有多少个文本框的问题,而是一个页面上有多少形状对象的问题。
    • 经过大量的尝试后,我设法让它在各种文档上始终如一地工作。我不确定我应该如何将代码上传到堆栈溢出 - 我应该将它添加到我原来的问题中还是应该将其添加为答案?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-31
    • 1970-01-01
    • 2018-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多