Access VBA to Find Row and Col for found text in MS Word (1 Viewer)

sxschech

Registered User.
Local time
Today, 03:31
Joined
Mar 2, 2010
Messages
793
I am using the following to find the table number for a word phrase in an MS Word Table. This allows me to determine a reference point to tell the code what cell or cells should be located to extract from word into an access table as in some files the table number is <x> and in other files, the data will be found in table number <y>.

Code:
Function FindWordTable(WordDoc As Object, pFindTxt As String, Optional NoTrim As Boolean, Optional SearchForward As Boolean = True)
'Find the table number containing the search term
'https://stackoverflow.com/questions/67059729/vba-search-a-word-document-for-a-table-containing-a-specific-string-and-return
'and FindInWordDoc from DocControl.accdb for use as a called function
'20231024
    Dim tno As Integer

    With WordDoc
        For tno = 1 To .tables.Count
            With .tables(tno).Range.Find
                .Text = pFindTxt '"Daily Report"
                .Execute
                If .found Then
                    FindWordTable = tno
                    Exit Function
                End If
            End With
        Next
    End With
End Function

This was sufficient until today when I started working with files that had a different format/table layout than what I originally coded for. Now need to know more than just the table number, need to know the Row and possibly the col so that my code can then locate the proper cell to extract from Word into an Access table. I was hoping that since the above code can find the word and identify the table, that it could also tell me the Row and Col number for the cell containing the found item. Not sure about the syntax. Most of the searches I've done keep bringing up excel examples for excel tables, so my search terms must not be right as it is taking the keywords out of context. I also tried prefixing my search with "MS Word" and it still didn't seem to give too many relevant results - or ones that I can make sense of. The ones I found talked about the selection in word and since I am doing from Access, to my knowledge, I'm not actually clicking anything in word for the cursor to find relative to where the selection is.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 05:31
Joined
Feb 28, 2001
Messages
27,186
The syntax for table cells is in COM-compatible programs (VBA & Excel, Word, Access) is ActiveDocument.Table( x ).Rows( y ).Columns( z ) so you can do some searching. Might be tedious. Here is the Word Object Model. From it you can find whatever objects you need to examine. If you have not seen that style before, the left-hand part is like a tree diagram. If you click on the ">" it switches to "v" and lists the elements in it. Click on the "v" and it closes up the list and switches back to the ">" symbol.


EDITED because apparently, some emojis were triggered by the parenthetical references.
 
Last edited:

Edgar_

Active member
Local time
Today, 05:31
Joined
Jul 8, 2023
Messages
430
Here's a possibility, you can see some of the places where you can look to find the row and the column
Rich (BB code):
Function FindText(WordDoc As Object, pFindTxt As String) As String
    Dim t As Long
    Dim c As Cell
    For t = 1 To WordDoc.Tables.Count
        For Each c In WordDoc.Tables(t).Range.Cells
            If InStr(1, LCase(c.Range.Text), LCase(pFindTxt)) > 0 Then
                Debug.Print _
                    "Table num:" & t & ", " & _
                    "Row: " & c.RowIndex & ", " & _
                    "Col:" & c.ColumnIndex & ", " & _
                    "Full text:" & c.Range.Text
            End If
        Next c
    Next t
End Function

You can find this and more if you use the debugger.
 
Last edited:

sxschech

Registered User.
Local time
Today, 03:31
Joined
Mar 2, 2010
Messages
793
Edgar, Thank you for the code. I did a test and looks like it will meet the need. Have some appointments today so will work on this later on and let you know.

The Doc Man, I did look on the MS site for reference/example before and couldn't quite put into practice.
 

Users who are viewing this thread

Top Bottom