【问题标题】:Word Macro To Delete Pages With Specific TextWord 宏删除具有特定文本的页面
【发布时间】:2015-10-11 21:08:38
【问题描述】:

我正在尝试删除任何包含特定文本的页面,例如下面的句子 (strSearch =)。但是当我尝试运行宏时出现 5904 错误...有什么线索吗?

  Sub DeletePages()
    Dim strSearch As String
    Dim rgeStart As Range
    Set rgeStart = Selection.Range

    strSearch = "Report the content of the ""StatusBar"" status bar message to the results."

    With ActiveDocument.Range.Find
      .Text = strSearch
      Do While .Execute
        With .Parent
          .Select With Selection
          .Bookmarks("\Page").Range.Delete
        End With
      End With
    Loop
  End With

  rgeStart.Select
  Application.Browser.Target = wdBrowsePage
End Sub

【问题讨论】:

  • 正确的格式有助于发现问题...
  • 哪行代码触发了错误?错误信息是什么?您是否尝试过单步执行代码以确保它符合您的预期?整个 Find 部分对我来说没有多大意义,但是您没有提供任何 cmets,因此不清楚您期望它做什么......

标签: string vba ms-word find selection


【解决方案1】:

我在 .Select With Selection 上遇到语法错误,并且这一行没有智能感知...似乎对象无法访问属性和方法,因为 with 语句相互嵌入。

以下内容对我有用...我不得不以不同的方式删除最后一页

 Sub DeletePages()
'source1 http://stackoverflow.com/questions/13465709/repeating-microsoft-word-vba-until-no-search-results-found
'source2 https://msdn.microsoft.com/en-us/library/bb208876(v=office.12).aspx

    Dim strSearch As String

    strSearch = "GoodBye"

    With Selection.Find
        .Forward = True
        .Wrap = wdFindStop
        .Text = strSearch
        .Execute
    End With


    Do While Selection.Find.Found = True And iCount < 1000
        iCount = iCount + 1
        Selection.HomeKey Unit:=wdStory
        Selection.Find.Execute
        If Selection.Find.Found Then
            'Get Current page
            CurPage = Selection.Information(wdActiveEndAdjustedPageNumber)

            'Check if current page is the last page
            If CurPage = FindLastPage Then
                ActiveDocument.Range(Selection.Start, ActiveDocument.Range.End).Delete
            Else
                ActiveDocument.Bookmarks("\Page").Range.Delete
            End If
        End If
    Loop

End Sub
Function FindLastPage()
    'Source https://support.microsoft.com/en-us/kb/293861
    'Iterate each section in the document to retrieve the end page of the
    'document and compute the page count in that section. The results are
    'displayed in the Immediate window.
    Dim oSec As Object
    Dim nStartPg As Integer, nEndPg As Integer, nSecPages As Integer
    Dim NumSections As Integer
    NumSections = ActiveDocument.Sections.Count
    nStartPg = 1
    For Each oSec In ActiveDocument.Sections
       nEndPg = oSec.Range.Information(3) - 1  'wdActiveEndPageNumber=3
       'Account for the last page.
       If oSec.Index = NumSections Then nEndPg = nEndPg + 1
       nSecPages = nEndPg - nStartPg + 1
       FindLastPage = nSecPages
    Next

End Function

【讨论】:

  • 抱歉,当我试图正确措辞时,评论超时... 我不明白为什么你需要 FindLastPage 函数,因为无论你是否删除似乎都没有取决于部分?为什么不使用 ActiveDocument.ComputeStatistics(wdStatisticPages)?我不明白你为什么使用 AND iCount
  • 吹毛求疵:1) FindLastPage 中的注释不准确:代码不包含任何 Debug.Print 语句。 2) 您应该将 Document 对象传递给 FindLastPage 以确保它与调用过程在同一个文档上工作。 3) 将 nSecPages 分配给 FindLastPage 应遵循 Next 语句:无需在每个循环中分配值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-17
  • 1970-01-01
相关资源
最近更新 更多