简体   繁体   中英

VBA Search within page range in Word

I am trying to make the Excel VBA search in a Word Document from page 5 onwards and once it finds the specific keyword it should target just the 1st encountered table and get some cells from the Word Table back to Excel as the code below will display. I am trying to introduce your Option1 in it but at the moment I can't. Any idea why?

 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

In the comments above, I mentioned 3 ways to achieve what you want. I am sure there are other ways as well to skin a cat.

Here is an example ( Option 1 ) on how to search for a text from page 5 onwards. I have commented the code. Still if you do not understand then feel free to leave a comment and if I can reply, I will.

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

Output

在此处输入图片说明

And if I change

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

to

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

Then I get this

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM