首页 > 解决方案 > 在 Word 中的页面范围内进行 VBA 搜索

问题描述

我正在尝试从第 5 页开始在 Word 文档中进行 Excel VBA 搜索,一旦找到特定关键字,它应该只针对第一个遇到的表格,并将 Word 表格中的一些单元格返回 Excel,如下面的代码所示。我正在尝试在其中介绍您的 Option1,但目前我不能。知道为什么吗?

 Option Explicit

    Sub Testt()

            Dim ws As Worksheet
            Dim Selection As Object
            Dim objWord As Word.Application
            Dim i As Integer
            Dim strValue As String
            Dim wdDoc As Word.Document
            Dim wdFileName As Variant
            Dim TableNo As Integer 'table number in Word
            Dim myTableRange As Word.Range ' formerly variable 'a'
            Dim oWordApp As Object, oWordDoc As Object
        Dim pgNo As Long
        Dim FlName As String
        Dim SearchText As String
        Dim IopenedWord As Boolean


        Const wdMainTextStory As Integer = 1
    Const wdActiveEndPageNumber As Integer = 3
    Const wdStory As Integer = 6
    Const wdFindContinue As Integer = 1







            Set objWord = New Word.Application
            Set wdDoc = objWord.Documents.Open("C:\Users\Nigel\Desktop\Testt.docx")


            objWord.Visible = True


    With wdDoc.StoryRanges(wdMainTextStory)
                With .Find
                    .Forward = True
                    .ClearFormatting
                    .MatchWholeWord = True
                    .MatchCase = True
                    .Wrap = wdFindContinue
                    .Text = "Test"
                    .Execute


                    Do While objWord.Selection.Find.Execute = True
                '~~> Get the page number
                pgNo = objWord.Selection.Information(wdActiveEndPageNumber)
                '~~> Check if the page number is >= 5
                If pgNo >= 5 Then
                    Debug.Print "Search text found in page " & pgNo
                End If
            Loop



                End With


                If .Find.Found Then


                    MsgBox "Found"
                Else
                    MsgBox "Not found"
                    Exit Sub


      End If

                Set myTableRange = .Duplicate.Next(unit:=wdTable)


        Dim rowNb As Long
        Dim ColNb As Long
        Dim x As Long
        Dim y As Long
        x = 8
        y = 1

        With myTableRange.Tables(1)
            For rowNb = 1 To 1 '
                For ColNb = 2 To 2


                    Cells(x, y) = WorksheetFunction.Clean(.Cell(rowNb, ColNb).Range.Text)

                    y = y + 1
                Next ColNb

                y = 1
                x = x + 1
            Next rowNb
        End With

        x = x + 2
     End With



    End Sub

标签: excelvbams-word

解决方案


在上面的评论中,我提到了实现你想要的 3 种方法。我相信还有其他方法可以给猫剥皮。

这是一个关于如何从页面开始搜索文本的示例(选项 1 )。5我已经评论了代码。不过,如果您不明白,请随时发表评论,如果我能回复,我会的。

Option Explicit

Const wdMainTextStory As Integer = 1
Const wdActiveEndPageNumber As Integer = 3
Const wdStory As Integer = 6
Const wdFindContinue As Integer = 1

Sub Sample()
    Dim oWordApp As Object, oWordDoc As Object
    Dim pgNo As Long
    Dim FlName As String
    Dim SearchText As String
    Dim IopenedWord As Boolean

    '~~> Establish an Word application object
    On Error Resume Next
    Set oWordApp = GetObject(, "Word.Application")

    If Err.Number <> 0 Then
        Set oWordApp = CreateObject("Word.Application")
        IopenedWord = True
    End If
    Err.Clear
    On Error GoTo 0

    oWordApp.Visible = True

    '~~> Sample File
    FlName = "C:\Users\routs\Desktop\Sample.Docm"

    Set oWordDoc = oWordApp.Documents.Open(FlName)

    '~~> Search Text. Change as applicable
    SearchText = "Siddharth"

    '~~> Move to the begining of the document
    oWordDoc.Bookmarks("\StartOfDoc").Select

    oWordApp.Selection.Find.ClearFormatting

    With oWordApp.Selection.Find
        .Text = SearchText
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False

        '~~> Loop and find the search text
        Do While oWordApp.Selection.Find.Execute = True
            '~~> Get the page number
            pgNo = oWordApp.Selection.Information(wdActiveEndPageNumber)
            '~~> Check if the page number is >= 5
            If pgNo >= 5 Then
                Debug.Print "Search text found in page " & pgNo
            End If
        Loop
    End With

    oWordDoc.Close (False)

    If IopenedWord = True Then oWordApp.Quit
End Sub

输出

在此处输入图像描述

如果我改变

If pgNo >= 5 Then
    Debug.Print "Search text found in page " & pgNo
End If

Debug.Print "Search text found in page " & pgNo

然后我得到这个

在此处输入图像描述


推荐阅读